Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[JAVA][SPRING-WEB] Compilation error if spec has a path parameter named "path" #12328

Closed
kodonnell-heila opened this issue Jan 25, 2024 · 1 comment

Comments

@kodonnell-heila
Copy link
Contributor

Description

I am generating a spring web client from an external API. The api has a path parameter that is named "path", which I believe is valid in the openapi spec. I get a compilation error because the generated code tries to create a new variable called path when there is already a method argument called path.

Swagger-codegen version

swagger-codegen-maven-plugin.version is 2.4.39

Swagger declaration file content or url
{
  "info": {
    "title": "swagger-codegen-java-path-bug",
    "version": "0.0.1"
  },
  "swagger": "2.0",
  "basePath": "/ota",
  "paths": {
  "/myobj/{path}": {
      "parameters": [
        {
          "name": "path",
          "in": "path",
          "required": true,
          "type": "string"
        }
      ],
      "get": {
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }
    }
  }
}
Command line used for generation

I am using the maven plugin.
pom.xml: https://gist.github.com/kodonnell-heila/581a7fa40ecae5dd529251bb0fbd9130
command: mvn clean install

Steps to reproduce
  1. Using java 21 and maven version >= 3.2, be in a directory with the above pom.xml
  2. run mvn clean install
  3. Observe compilation error:
[ERROR] src/test/java/io/swagger/client/api/DefaultApi.java:[78,16] variable path is already defined in method myobjPathGetWithHttpInfo(java.lang.String)

You might also see this which has the same root cause:

[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] src/test/java/io/swagger/client/api/DefaultApiTest.java:[30,19] cannot find symbol
  symbol:   class DefaultApi
  location: class io.swagger.client.api.DefaultApiTest
Related issues/PRs

None that I know of.

Suggest a fix/enhancement

Should probably check if your variable names conflict with method parameters on generation. Might be less readable/not great, but you could just add numbers or characters after your variable name until it doesn't conflict with any method parameter names. Or just overwrite it by not redeclaring the variable.

@kodonnell-heila
Copy link
Contributor Author

This is nowhere in the docs, but the answer was to add this in my in the pom:

<localVariablePrefix>gen_</localVariablePrefix>

Where the prefix can be whatever needed to avoid collisions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant