swagger-api / swagger-api/swagger-codegen

Enum definition of type integer generates invalid Java code

Open
#6,806 5 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

The output of the following swagger generates invalid Java code.
The code that is generated by default with the Java language uses Google GSON JsonReader for parsing JSON objects. The generated code uses

jsonReader.nextInteger();

for the read method but it should be

jsonReader.nextInt(); 

as per the GSON docs: https://google.github.io/gson/apidocs/com/google/gson/stream/JsonReader.html#nextInt--

I'm not sure what the actual issue is, it could be an issue with a old GSON version.
Any help would be appreciated

Swagger-codegen version

2.3.3

Swagger declaration file content or url

Swagger doc:

swagger: '2.0'
paths: {}
definitions:
  suit:
    type: integer
    description: A description of suit types.
    format: int32
    x-enumNames:
      - hearts
      - spades
      - clubs
      - diamonds
    enum:
      - 0
      - 1
      - 2
      - 3

Generated Java Code:

public enum Suit {
  
  NUMBER_0(0),
  NUMBER_1(1),
  NUMBER_2(2),
  NUMBER_3(3);

  private Integer value;

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

  public Integer getValue() {
    return value;
  }

  ......

  public static class Adapter extends TypeAdapter<Suit> {
    @Override
    public void write(final JsonWriter jsonWriter, final Suit enumeration) throws IOException {
      jsonWriter.value(enumeration.getValue());
    }

    @Override
    public Suit read(final JsonReader jsonReader) throws IOException {
      Integer value = jsonReader.nextInteger();
      return Suit.fromValue(String.valueOf(value));
    }
  }
}
Command line used for generation

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

Using:
gson-2.8.1-sources.jar

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

Search the Java generator templates for the emitted nextInteger() call and compare it with the Gson JsonReader API cited in the issue. Reproduce generation with the supplied Swagger YAML and verify that the generated enum adapter uses nextInt() and produces compilable Java code.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.