swagger-api / swagger-api/swagger-codegen

[JAVA] Snake case field name gets converted to camel case with codegen

Open
#12,133 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

Our POJO has some fields which look like this

@Getter 
@Setter
public class Error {
    String errorName;
    String errorCode;
    String error_code;
}

Using mvn plugin we generate the swagger document (say api-service.json) and it creates the fields(errorCode and error_code) as expected like this (Adding just a snippet)

....
          "errorCode" : {
            "type" : "string"
          },
          "errorName" : {
            "type" : "string"
          },
          "error_code" : {
            "type" : "string"
          },
....

Now, when the client uses codegen (command below), the field error_code and its corresponding getters and setters are converted to errorCode and clashes with the already existing field , thus creating compilation error.

Generated Java file

....
  @SerializedName("errorCode")
  private String errorCode = null;

  @SerializedName("error_code")
  private String errorCode = null;
....
Swagger-codegen version

3.0.35

Command line used for generation
swagger-codegen generate \
  -i /Users/sh/api-service.json \
  --api-package com.company.services \
  --model-package com.company.services.dto \
  --invoker-package com.company.services.app \
  --group-id com.company.services \
  --artifact-id service-api \
  --artifact-version 0.0.1-SNAPSHOT \
  -l java \
  -o output-april-2023

How do I retain the original field name of error_code in the generated code after running swagger-codegen ?

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

Reproduce the issue with the provided swagger-codegen generate command and the sample schema containing both errorCode and error_code. Inspect the generated Java model and the codegen naming behavior; done means the two properties produce distinct valid Java fields, getters, and setters so the generated project compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.