OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.45k stars 6.49k forks source link

[BUG][JAVA] Java-Jersey2 generator creates "Accept:null" header #6882

Open 40rn05lyv opened 4 years ago

40rn05lyv commented 4 years ago
Description

I use Jersey2 as generator for my REST, including DELETE operations that don't "accept" any body. As a result, I see that the generated code executes HTTP requests with header "Accept:null" that is not a valid header (because of "null" string).

openapi-generator version

I use openapi-generator-maven-plugin 4.2.3, but from what I see in source code this bug should be reproducible in the master too.

OpenAPI declaration file content or url
paths:
  /v2/something/{name}:
    delete:
      tags:
      - Repository
      summary: Delete something
      operationId: something_delete
      parameters:
      - name: paramone
        in: path
        required: true
        schema:
          type: string
      responses:
        "204":
          description: delete success or nothing to delete
        "401":
          description: not authenticated / invalid credentials
        "403":
          description: not authorized, the current user does not have the "repository"
            privilege
        "500":
          description: internal server error
Command line used for generation

pom.xml config:

                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>4.2.3</version>
...
                            <generatorName>java</generatorName>
                            <configOptions>
                                <java8>true</java8>
                                <library>jersey2</library>
                            </configOptions>
...
Steps to reproduce

See description, it's quite obvious.

Related issues/PRs

Not found

Suggest a fix

In file openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache change line from

Invocation.Builder invocationBuilder = target.request().accept(accept);

to

Invocation.Builder invocationBuilder = accept != null ? target.request().accept(accept) : target.request();

Also, need to check other generator for this problem.

wing328 commented 4 years ago

@40rn05lyv thanks for reporting the issue. Please file a PR so that we can review more easily.

S2021Git commented 2 years ago

@40rn05lyv Hello, I am working on this issue. Can you please clarify how to reproduce this issue? Thank you.

40rn05lyv commented 2 years ago

@S2021Git

  1. Generate Jersey2 code for the example DELETE endpoint that I wrote
  2. Run the generated code Expected result: Generated code doesn't send "Accept" header. Actual result: Generated code sends "Accept:Null"
S2021Git commented 2 years ago

@40rn05lyv thank you for the response. Below are the steps I did but I got an error message, what did I do wrong here?

  1. I copied your snippet to a new .yaml file I created.
  2. I run: java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \ -i ...(mydiectory)/openapi-generator-master/issue_6882.yaml \ -g java -o ...(mydirectory)/openapi-generator-master/6882output
  3. But I got this error message after I run -g java Screen Shot 2021-12-01 at 10 20 13 PM

What should I do here?

S2021Git commented 2 years ago

@40rn05lyv Hi, just checking to see if you saw the message I posted above. Thank you!

40rn05lyv commented 2 years ago

@S2021Git Sorry, I don't have time for this. I already suggested a fix in my first message here, and it's working in my own private fork.