swagger-api / swagger-api/swagger-codegen

Error generating model using array of enum with type format int32 or int64

Open
#7,346 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

When a definition contains an array of enums, and the items format value is int32, the generated java model is invalid.

Swagger-codegen version

version 2.x

Swagger declaration file content or url
swagger: '2.0'
info:
  description: 'My Service'
  version: 1.0.0
  title: My Service
schemes:
  - http
paths:
  /templates:
    get:
      summary: Returns all
      description: ''
      operationId: getAll
      produces:
        - application/json
      responses:
        '200':
          description: successful operation
          schema:
            type: array
            items:
              $ref: '#/definitions/Parent'
definitions:
  Parent:
    discriminator: id
    properties:
      id:
        type: string
      status:
        type: array
        items:
          type: integer
          format: int32
          enum:
            - 1
            - 2
            - 3
    required:
      - id
  Child:
    allOf:
      - $ref: "#/definitions/Parent"
      - type: object
        properties:
          subProperty:
            type: string
Command line used for generation

java -jar swagger-codegen-cli-2.3.0.jar generate -i codegen-test.yaml -l java -o codegen-test

Steps to reproduce

Run code generation,

The generated Parent class:

public class Parent {
// ....
public enum StatusEnum {
    _1("1"),
    _2("2"),
    _3("3");
    private Integer value;

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

    public Integer getValue() {
      return value;
    }

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

    public static StatusEnum fromValue(String text) {
      for (StatusEnum b : StatusEnum.values()) {
        if (String.valueOf(b.value).equals(text)) {
          return b;
        }
      }
      return null;
    }
 // ....
}
//...
}
Related issues/PRs

#3147

Suggest a fix/enhancement

The constructor is expecting Integer.
change to number_1(1);

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 running the provided swagger-codegen-cli 2.3.0 command with the YAML declaration and inspect the generated Parent Java model, especially StatusEnum. Trace the Java model-generation entry point or template responsible for array enum values with int32/int64 formats. Done means the generated model is valid Java and its enum values preserve the numeric values shown in the declaration.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
backend-api-design, tooling
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.