OpenAPITools / OpenAPITools/openapi-generator

[BUG] Broken enum code generation when uppercase and lowercase enums are given

Open
#18,426 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report Checklist
  • [x ] Have you provided a full/minimal spec to reproduce the issue?
  • [ x] Have you validated the input using an OpenAPI validator (example)?
  • [ x] Have you tested with the latest master to confirm the issue still exists?
  • [ x] Have you searched for related issues/PRs?
  • [ x] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

OpenAPI enums are case-sensitive [1]. To cope up with this I am declaring both uppercase and lowercase enum values like this:

components:
  schemas:
    DocumentType:
      type: string
      enum: [txt, TXT, html, HTML, pdf, PDF, doc, DOC, xls, XLS, ppt, PPT]

When I run the code generator on above it outputs following:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import jakarta.annotation.Generated;
import jakarta.validation.constraints.*;
import java.util.*;

/** Gets or Sets DocumentType */
@Generated(
    value = "org.openapitools.codegen.languages.SpringCodegen",
    date = "2024-04-18T08:48:15.084178-07:00[America/Los_Angeles]",
    comments = "Generator version: 7.4.0")
public enum DocumentType {
  TXT("txt"),

  TXT2("TXT"),

  HTML("html"),

  HTML2("HTML"),

  PDF("pdf"),

  PDF2("PDF"),

  DOC("doc"),

  DOC2("DOC"),

  XLS("xls"),

  XLS2("XLS"),

  PPT("ppt"),

  PPT2("PPT");

  private String value;

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

  @JsonValue
  public String getValue() {
    return value;
  }

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

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

However this does not help when user passes txt in their REST request. Spring Framework calls Enum.valueOf to try to convert txt to a well-known enum and that call fails

java.lang.IllegalArgumentException: No enum constant xxx.openapi.model.DocumentType.txt

because as per docs:

The name must match exactly an identifier used to declare an enum constant in this type

The right code generation will look like following:

public enum DocumentType {
  txt("txt"),

  TXT("TXT"),

// and so on...
openapi-generator version

7.4.0

OpenAPI declaration file content or url
components:
  schemas:
    DocumentType:
      type: string
      enum: [txt, TXT, html, HTML, pdf, PDF, doc, DOC, xls, XLS, ppt, PPT]
Generation Details

using openapi-generator-maven-plugin

Steps to reproduce

please see above

Related issues/PRs
Suggest a fix

please see above for the right code that should be generated.

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 supplied OpenAPI enum and reproduce generation through openapi-generator-maven-plugin, then trace the Java SpringCodegen path that produces the DocumentType enum. Done means mixed-case values generate distinct enum constants that preserve their original casing, with regression coverage for the supplied declaration.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.