swagger-api / swagger-api/swagger-codegen

[JAVA] Generated enums are not immutable

Open
#10,195 0 comments 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
Swagger-codegen version

Using version 2.3.1
Not a regression and does not seem to be changed in future versions.

Swagger declaration file content or url
definitions:
  productType:
    type: string
    enum: [Paid, Free]
    description: ProductType - Paid or Free
Command line used for generation

Using swagger-codegen-maven-plugin to generate the Java classes

Steps to reproduce

Using swagger-codegen-maven-plugin to generate the Java classes

Related issues/PRs

None found

Suggest a fix/enhancement

The variable value in the following generated code should be final.

public enum ProductType {
  
  PAID("Paid"),
  
  FREE("Free"),

  private String value;

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

  @JsonValue
  public String getValue() {
    return value;
  }

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

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

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

Locate the Java generation template used by swagger-codegen-maven-plugin for the enum shown in the issue, then compare its output with the provided ProductType example. Verify the generated enum field is final by regenerating the sample definition and inspecting the resulting Java class.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.