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

[JAVA] enums with underscores are trimmed #10876

Open jspaceface opened 3 years ago

jspaceface commented 3 years ago
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.

mivanilov commented 3 years ago

Here is a workaround that at least can help to solve serialisation/deserialisation issue. If you are using Jackson for json serialisation/deserialisation, try to enable it in the plugin <configuration> by adding this property <additionalProperties>jackson=true</additionalProperties>, or if you are using custom Java code generator, you can do it in the code generator constructor by adding this bit of code additionalProperties.put("jackson", "true");. It will generate enums with Jackson annotation @JsonValue (used for serialisation) on the getValue() method and @JsonCreator (used for deserialisation) on the fromValue(String text) method. As a result e.g. "TEST_AVAILABLE" would be deserialised into StatusEnum.AVAILABLE using fromValue(String text) method, and StatusEnum.AVAILABLE would be serialised into "TEST_AVAILABLE" using getValue() method.

McBruno712 commented 1 year ago

Hi, @mivanilov! Any updates on this bug?

martin-thoma commented 1 year ago

I think I've just experienced the same for JavaScript

spyro2000 commented 9 months ago

Lead to huge problems for us also as most enums now can't be serialzed anymore as the names do not match. The suggested work around did not work for me. Why are the enum names trimmed anyway?