OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] Generator incorrectly altering enum values

Open
#19,204 5 comments 5 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
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

There are 2 places where i observed this bug. One of which was with an array of camelCase values being changed to snake_case and the other with an enum value that contains a number.

S4A -> S4_A but expected S4A
or
middleAgeFemale -> MIDDLE_AGE_FEMALE but expected MIDDLEAGEFEMALE

openapi-generator version

version >7.4.0. This issue does not occur in 7.4.0

OpenAPI declaration file content or url
    Schema1
      type: string
      enum:
        - S4A
        - ENUM_VALUE_ONE
        - ENUM_VALUE_TWO
        - ENUM_VALUE_THREE

This produces enum values S4_A, ENUM_VALUE_ONE, ENUM_VALUE_TWO, ENUM_VALUE_THREE
or

    Schema1:
      type: string
      example: "middleAgeFemale"
      enum:
        - "middleAgeFemale"
        - "middleAgeMale"
        - "seniorFemale"
        - "seniorMale"
        - "teenageBoy"
        - "teenageGirl"
        - "youngAdultFemale"
        - "youngAdultMale"

This produces enum values MIDDLE_AGE_FEMALE, MIDDLE_AGE_MALE ...

Generation Details

Java OpenAPI Generator in Spring Boot application

Steps to reproduce
  1. Create a schema with an enum value of type string.
  2. Include a number in one of the enum values or create camelCase values only
  3. run the code generator
  4. results for the number will look like this:
    S4A -> S4_A but expected S4A
    or
    middleAgeFemale -> MIDDLE_AGE_FEMALE but expected MIDDLEAGEFEMALE
Related issues/PRs
Suggest a fix

My work around for this was to use x-enum-varnames to manually fix these 2 cases

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

Run the Java OpenAPI Generator against the two schemas in the report and compare the generated enum identifiers with the declared values. Trace the generator entry point that converts string enum values, then verify that S4A and camelCase values remain unchanged while already-uppercase underscore values are preserved.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.