swagger-api / swagger-api/swagger-codegen

[JAVA] enums with underscores are trimmed

Open
#10,876 4 comments 5 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

When I generate models with enums that include underscores, the enum variable gets trimmed. This is causing other code dependent on the model to not compile since it expects the enums to include the underscores. I reproduced this using a slightly-altered version of the petstore example at https://petstore.swagger.io/v2/swagger.json. For example, I changed the status enum in the Pet object to

                "status": {
                    "type": "string",
                    "description": "pet status in the store",
                    "enum": ["TEST_AVAILABLE", "TEST_PENDING", "TEST_SOLD"]
                }

This generated the following java model:

  // ...
  public enum StatusEnum {
    AVAILABLE("TEST_AVAILABLE"),
    PENDING("TEST_PENDING"),
    SOLD("TEST_SOLD");
  //...

Notice that the enum variables are trimmed versions of the values (AVAILABLE instead of the expected TEST_AVAILABLE, even though TEST_AVAILABLE is a valid enum value).

Swagger-codegen version

3.0.24

Swagger declaration file content or url

https://gist.github.com/jspaceface/89016d7da7d3a84d91184d2e0d9ebf15

Command line used for generation

swagger-codegen generate -i ./swagger-test.json -l java -o ./swagger-out

Steps to reproduce
  1. Download the linked swagger-test.json declaration file
  2. Run the above codegen command
  3. Inspect ./swagger-out/src/main/java/io/swagger/client/model/Pet.java
Related issues/PRs

https://github.com/swagger-api/swagger-codegen/issues/4805

Suggest a fix/enhancement

I would expect the enum values to match how they are defined in the declaration file.

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 linked swagger-test.json and run the documented swagger-codegen command, then inspect Pet.java under the generated model path. Compare the generated StatusEnum constants with TEST_AVAILABLE, TEST_PENDING, and TEST_SOLD; done means the Java enum names retain the underscore-containing values from the declaration.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.