swagger-api / swagger-api/swagger-codegen

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

Open
#7,038 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Java generation path using DefaultGenerator and CodegenConfigurator, including the MODEL_PROPERTY_NAMING setting, and reproduce the issue with a minimal Swagger definition containing both accessToken and access_token. Done means generated model code preserves distinct valid attribute names without duplicate Java fields, with a regression test covering the case.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.