swagger-api / swagger-api/swagger-codegen

[Java] Option to generate Enum values as public static final constants?

Open
#6,286 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Feature: Enum General: Discussion
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I'd like to introduce an option to the java codegen that would generate constants rather than enums where enums are defined in swagger.

So as an example, the following model has a simple enum:

              cat_gender:
                type: string
                enum:
                  - Male
                  - Female

This currently generates the following enum:

  @JsonProperty("cat_gender")
  private CatGenderEnum catGender = null;

  /**
   * Gets or Sets catGender
   */
  public enum CatGenderEnum {
    MALE("Male"),
    
    FEMALE("Female");

    private String value;

    CatGenderEnum(String value) {
      this.value = value;
    }

    @Override
    @JsonValue
    public String toString() {
      return String.valueOf(value);
    }

    @JsonCreator
    public static CatGenderEnum fromValue(String text) {
      for (CatGenderEnum b : CatGenderEnum.values()) {
        if (String.valueOf(b.value).equals(text)) {
          return b;
        }
      }
      return null;
    }
  }

I'd like to add a codegen option that would allow you to generate simply:

  @JsonProperty("cat_gender")
  private String catGender = null;

    public static final String CATGENDER_MALE = "Male";
    public static final String CATGENDER_FEMALE = "Female";

The advantage to building this as a String is flexibility, especially when swagger is generating client code.

With the existing generation where an enum in swagger is turned into a strict Enum in java, if the server API changes to add a new value in cat_gender that is neither male nor female (e.g. MALE_NEUTERED), any existing client implementation will just see a value of null (or if #5950 is implemented, this could be possibly an UNKNOWN enum value or just throwing an Exception).

If the enum on the client is interpreted as a String, then the value can still be captured and used (for example, in log/error messages).

I couldnt find any existing option, and for the sake of backwards compatibility I'd suggest adding this as a new code generation option (initially for client java generation) using CodegenConstants.

Any thoughts/comments?

Swagger-codegen version

Latest HEAD

Command line used for generation

swagger-codegen-cli.jar -l spring

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 by reading the Java codegen entry point and CodegenConstants, then run the documented swagger-codegen-cli command against a specification containing the cat_gender enum. Define how the new option should preserve current enum generation by default while producing a String field and public static final constants when enabled.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.