OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.5k stars 6.51k forks source link

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

Open siddhsql opened 5 months ago

siddhsql commented 5 months ago

Bug Report Checklist

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.

wing328 commented 5 months ago

for use case like this one, what about using the enum name mapping option: https://github.com/openapitools/openapi-generator/blob/master/docs/customization.md#name-mapping ?