Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[BUG][JAVA][RestTemplate] Specified Accept header values generates uncompilable code #4222

Closed
crosbyb opened this issue Oct 22, 2019 · 6 comments · Fixed by #7101
Closed

Comments

@crosbyb
Copy link

crosbyb commented Oct 22, 2019

Description
openapi-generator version

4.1.0, 4.1.3

OpenAPI declaration file content or url

See the attached swagger contract for the full example.

        "parameters": [
          {
            "in": "header",
            "name": "Accept",
            "schema": {
              "enum": [
                "application/vnd.geo+json",
                "text/csv"
              ]
            },
            "required": true
          }
        ],
Command line used for generation

swagger-client-config.json:

{
  "modelPackage": "sc.v0.client.model",
  "apiPackage": "svc.v0.client",
  "library": "resttemplate",
  "groupId": "api.clients",
  "artifactId": "svc-client",
  "artifactVersion": "0.0.1"
}

java -jar ./codegen.jar generate -c ./swagger-client-config.json -i ./swagger.json -g java -o client/

Steps to reproduce
mkdir client
java -jar ./codegen.jar generate -c ./swagger-client-config.json  -i ./swagger.json -g java -o client/
cd client/ && mvn install  -q -Dmaven.test.skip=true -Dmaven.javadoc.skip=true && cd ..

You'll see the following error when Maven tries to compile:

[ERROR] /Users/brad/tmp/openapi-accept/client/src/main/java/svc/v0/client/ServiceApi.java:[77,31] variable accept is already defined in method getAllThings(java.lang.String)

There is already a local variable in the method named accept, so adding the accept header as an argument to the method creates the conflict.

@auto-labeler
Copy link

auto-labeler bot commented Oct 22, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@wing328
Copy link
Member

wing328 commented Oct 22, 2019

[ERROR] /Users/brad/tmp/openapi-accept/client/src/main/java/svc/v0/client/ServiceApi.java:[77,31] variable accept is already defined in method getAllThings(java.lang.String)

Looks like this can be fixed by prefixing the variable name with localVar (e.g. localVarAccept) like we've done in other generators.

I wonder if you've time to file a PR with the suggested fix.

@fermartel
Copy link

Any update on this issue?

@ogerardin
Copy link

I just ran into the same problem.
@crosbyb did you use a workaround ?

@mikesaurus
Copy link
Contributor

@fermartel @ogerardin
You can workaround this issue by creating a local copy of the Java/resttemplate/api.mustache template and using the templateDirectory or templateResourcePath config properties to override the default. I just submitted a PR to update the template in the main codebase, so hopefully they will merge this in for the next release. In the meantime, you can copy the api.mustache template from my PR and use it with the version you are currently running.

@wing328 #7101

@wing328
Copy link
Member

wing328 commented Aug 4, 2020

Thanks for the fix by @mikesaurus

Please pull the latest master or snapshot to give it a try.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants