swagger-api / swagger-api/swagger-codegen

[JAVA] Incorrect enum generation for integer values

Open
#11,317 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

Java compilation is failed for a model which is generated with enum schema for integer values.

Swagger-codegen version

3.0.30

Swagger declaration file content or url
 Status:
        type: integer
        enum: [0, 1, 2]
        description: > 
          Statuses :
          * 0 Unknown
          * 1 New
          * 2 Finished
Command line used for generation

java -Dmodels -jar swagger-codegen-cli-3.0.30.jar generate -i model.yaml -l java -DmodelPackage=some.custom.package

Steps to reproduce
  1. Create yaml specification with enum based on integer values
  2. Generate java models with command line explained above
    Expected result:
    Generator produces compilable java entity
    Actual result:
    Generated java entity is not compilable
Java code generated by swagger-codegen
 public static class Adapter extends TypeAdapter<Status> {
    @Override
    public void write(final JsonWriter jsonWriter, final Status enumeration) throws IOException {
      jsonWriter.value(String.valueOf(enumeration.getValue()));
    }

    @Override
    public Status read(final JsonReader jsonReader) throws IOException {
      Object value = jsonReader.nextInt();
      return Status.fromValue(()(value));
    }
  }

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

Reproduce the issue with the supplied model.yaml and swagger-codegen-cli-3.0.30 command, then inspect the generated Java enum's Adapter and the generator templates that produce it. Confirm the generated model compiles for integer enum values, while preserving valid output for other enum types.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.