OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Generating enum NULL ends up as NULL("null")

Open
#6,592 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

For the following spec:

    Position:
      type: string
      enum:
        - NULL
        - LEFT
        - RIGHT
        - CENTER
        - TOP
        - BOTTOM
        - REJECT
        - FRONT
        - REAR

The openapi-Generator creates the following code:

public enum Position {
  
  NULL("null"),
  
  LEFT("LEFT"),
  
  RIGHT("RIGHT"),
  
  CENTER("CENTER"),
  
  TOP("TOP"),
  
  BOTTOM("BOTTOM"),
  
  REJECT("REJECT"),
  
  FRONT("FRONT"),
  
  REAR("REAR");

  private String value;

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

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

  @JsonCreator
  public static Position fromValue(String value) {
    for (Position b : Position.values()) {
      if (b.value.equals(value)) {
        return b;
      }
    }
    throw new IllegalArgumentException("Unexpected value '" + value + "'");
  }
}

Which ends in and IllegalArgumentException getting thrown if a value "position":"NULL" is submitted, since the values must match exactly (case sensitive).

openapi-generator version

4.3.0

OpenAPI declaration file content or url
    Position:
      type: string
      enum:
        - NULL
        - LEFT
        - RIGHT
        - CENTER
        - TOP
        - BOTTOM
        - REJECT
        - FRONT
        - REAR
Command line used for generation
    generatorName.set("jaxrs-spec")
    inputSpec.set("$rootDir/src/main/resources/openapi.yaml")
    outputDir.set("$buildDir/generated")
    apiPackage.set("at.o2xfs.xfs.web.api")
    modelPackage.set("at.o2xfs.xfs.web.model.cdm")
    configOptions.set(mapOf("dateLibrary" to "java8", "interfaceOnly" to "true", "returnResponse" to "true"))

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 reproducing the issue with the supplied Position enum, the jaxrs-spec generator, and the shown command-line configuration. Trace the Java enum generation path and compare the generated NULL constant with the other values. Done means a generated enum accepts the input value "NULL" without throwing IllegalArgumentException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
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.