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.94k stars 6.03k forks source link

Duplicated model attribute is generated when json has two attributes; one with underscore #7038

Open hunzai opened 6 years ago

hunzai commented 6 years ago
Description

I am trying to generate code (models and apis) from swagger definition responses. For compatibility reasons we have a defined one attribute twice but technically the key is different.

See following response.

{ "**accessToken**": "<TOKEN>, "**access_token**": "<TOKEN>", "user": { "id": "23444", "firstName": "Test", "lastName": "De", "primaryEmailAddress": "example@gmail.com", "emailAddresses": [ { "address": "example@gmail.com" } ],

When code is generated using swagger-codegen then underscore is replaces and attribute is generated twice, see following model snippet

@SerializedName("accessToken")
 private String accessToken = null;

 @SerializedName("access_token")
 private String accessToken = null;
Swagger-codegen version

2.3.0-SNAPSHOT (build locally based on master branch)

Swagger declaration file content or url

for security reasons I can not share exact code. But if needed I will create a sample

Swagger declaration file content or url

I am using swagger code generator from within Java code. I found an option to tell MODEL_PROPERTY_NAMING but it didn't work.

See following code

 public void generate(String swaggerDocUrl)  {

        DefaultGenerator generator = new DefaultGenerator();

        CodegenConfigurator configurator = new CodegenConfigurator();
        configurator.setLang("java");
        configurator.setInputSpec(swaggerDocUrl);

        configurator.addSystemProperty(CodegenConstants.API_TESTS, "false");
        configurator.addSystemProperty(CodegenConstants.API_DOCS, "false");
        configurator.addSystemProperty(CodegenConstants.MODEL_DOCS, "false");
        configurator.addSystemProperty(CodegenConstants.MODEL_TESTS, "false");
        configurator.addSystemProperty(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.original.toString());

        configurator.addAdditionalProperty("dateLibrary", "java8-localdatetime");
        configurator.addAdditionalProperty("java8", "true");
        configurator.setLibrary("retrofit2");

        configurator.setRemoveOperationIdPrefix(true);
        generator.opts(configurator.toClientOptInput()).generate();

    }
Steps to reproduce
  1. build latest swagger-codegen
  2. Include in your pom
  3. Run above code
Related issues/PRs
Suggest a fix/enhancement

When attributes are defined with different conventions, for whatever reason, it should be respected. Don't replace underscore in variables.

dskvr commented 5 years ago

I'm having the same issue, but it's appending a 2 at the end of the model name. For example:

BigNumber BigNumber2

They are identical, and even the reference is identical. This issue popped up out of nowhere.