swagger-api / swagger-api/swagger-codegen

Integer Enum in Array is not generated correctly in 3.0.7

Open
#9,358 3 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

There was very similar issue when generating Array of STRING Enums:
https://github.com/swagger-api/swagger-codegen/issues/7377

In 3.0.7 Swagger-Codegen version with 1.0.7 Swagger-Codegen-Generators the List is generated correctly for String type of Enum, but not for Integer type.
If I specify type of enum "integer" or "number", the enum values in Java Class still are generated as String values. See below
If I specify enum in yaml:

TbProductFilter:
  type: object
  properties:
    productType:
      type: array
      items:
        type: integer
        enum:
          - 0
          - 1
          - 2
          - 3

Enum in Java Class:

public enum ProductTypeEnum {
    _0("0"),
    
    _1("1"),
    
    _2("2"),
    
    _3("3");

    private Integer value;

    ProductTypeEnum(Integer value) {
      this.value = value;
    }

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

    @JsonCreator
    public static ProductTypeEnum fromValue(String text) {
      for (ProductTypeEnum b : ProductTypeEnum.values()) {
        if (String.valueOf(b.value).equals(text)) {
          return b;
        }
      }
      return null;
    }
  }
  @JsonProperty("productType")
  @Valid
  private List<ProductTypeEnum> productType = null;

And ofcourse the code doesn't compile - values are strings, but everywhere else Integers are expected.

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 provided OpenAPI YAML and inspect the generated Java ProductTypeEnum for the array property. Compare integer enum handling with the referenced string-enum issue and trace the generator templates or entry point responsible for Java enum values. Done means integer enum constants compile with integer values while preserving the expected generated behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.