swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.97k stars 6.04k forks source link

[SPRING] Fields starting with _ are generated as duplicates #10093

Open jordanwalsh23 opened 4 years ago

jordanwalsh23 commented 4 years ago
Description

When generating spring code using swagger-codegen-cli, fields starting with an _ appear in the generated response twice.

Swagger-codegen version

swagger-codegen-cli v3.0.18

Swagger declaration file content or url
defaultmodel:
    description: Default response for the api
    properties:
      api_name:
        type: string
        example: "My Wonderful API"
      api_version:
        type: string
        example: v1.0
      api_released:
        type: string
        example: "2020-02-03"
      api_status:
        type: string
        example: "active"
      _links:
        type: array
        items:
          properties:
            rel:
              type: string
              example: organisation-units
            href:
              type: string
              example: /v1.0/organisation-units
            method:
              type: string
              example: GET
Command line used for generation
java -jar swagger-codegen-cli.jar generate -i ..\api.yaml -l spring -o ..\mock
Steps to reproduce

e.g.

{
  "_links": [
    {}
  ],
  "api_name": "My Wonderful API",
  "api_released": "2020-02-03",
  "api_status": "active",
  "api_version": "v1.0",
  "links": [
    {}
  ]
}
Related issues/PRs

Can't find any.

Suggest a fix/enhancement

I've reviewed the generated code and can't see this extra parameter in any of the models. It seems to be within the core swagger codegen project.

jordanwalsh23 commented 4 years ago

Please note this is also an issue when there is only a single letter prior to the underscore.

E.g. the bug is also present here in a field called x_links, but it works perfectly with fields called xx_links.

phxnsharp commented 4 years ago

We have seen this too. Looks like the bug is actually in Springfox. Note that Springfox also doesn't support OpenAPI 3.0, so code generated from OpenAPI 3.0 works correctly, but presents a 2.0 rendition of the API on the swagger documentation page.

muka commented 4 years ago

I have a similar issue. In #4805 there is an attemp to fix, maybe exposing a property like modelPropertyNaming=original for java may help in that case ? Thanks

daxsorbito commented 4 years ago

Hi, any update/workaround on this? I ran into this problem. If your field has an underscore, the output model would look like this:

 @JsonProperty("_link")
 private ResultLink _link = null;

This would give me a duplicate field, the one with the underscore and the other without.

But if I would change the private variable to just link (retain the JsonProperty to use '_link'):

 @JsonProperty("_link")
 private ResultLink link = null;

This would not give me a duplicate fields.