Skip to content

fix(java): do not map 'format: enum' schemas to 'Enum' - #25008

Open
Natarajan717 wants to merge 2 commits into
OpenAPITools:masterfrom
Natarajan717:feature/issue24950-enum-format-fix
Open

Natarajan717 wants to merge 2 commits into
OpenAPITools:masterfrom
Natarajan717:feature/issue24950-enum-format-fix

Conversation

@Natarajan717

@Natarajan717 Natarajan717 commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

…en (fixes #24950)

fixes #24950

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

In PR #24812, typeMapping.put("enum", "Enum") was added to AbstractJavaCodegen to resolve common discriminator types for oneOf interface methods across enum subtypes. However, because DefaultCodegen.getSchemaType checks typeMapping for format matches, schemas specifying format: enum were mapped to type "Enum", causing invalid Java code generation (private Enum value; and invalid Enum.valueOf(...) calls).

This PR:

  • Introduces protected String getCommonEnumType() in DefaultCodegen (overridden in AbstractJavaCodegen to return "Enum").
  • Replaces direct typeMapping.get("enum") lookups in discriminator resolution with getCommonEnumType().
  • Removes typeMapping.put("enum", "Enum") from AbstractJavaCodegen, preventing the format collision.
  • Adds a unit test in JavaClientCodegenTest verifying that schemas with format: enum generate valid String backing fields and constructors without invalid Enum.valueOf(...) or imports.
    cc: @wing328

Summary by cubic

Fixes Java code generation for schemas with format: enum so they no longer produce invalid Enum types.

  • Adds a getCommonEnumType() hook in DefaultCodegen; the base implementation keeps the typeMapping["enum"] lookup and falls back to the mapped object type when absent.
  • Overrides the hook in AbstractJavaCodegen to return "Enum", and removes typeMapping.put("enum", "Enum") so format: enum strings are no longer resolved to Java's Enum.
  • Adds a unit test verifying format: enum schemas generate String-backed enums without Enum.valueOf(...), invalid Enums fields, or Enum imports.

Written for commit b362993. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@Natarajan717 Natarajan717 reopened this Sep 25, 2026

@Sekator778 Sekator778 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built it and ran the new test, it fails on master. Also generated native, okhttp-gson and Spring code from a format: enum schema: on master it references a missing Enum class and doesn't compile, with this it's a proper enum. The oneOf enum samples from #24812 come out unchanged. LGTM, thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [JAVA] Broken code generation when using format: enum

2 participants