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

NA
1mo ago
+61-8

…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.