fix(java): do not map 'format: enum' schemas to 'Enum'
NA
1mo ago+61+61+01234567890123456789-8-8-0123456789
…en (fixes #24950)
fixes #24950
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
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()inDefaultCodegen(overridden inAbstractJavaCodegento return"Enum"). - Replaces direct
typeMapping.get("enum")lookups in discriminator resolution withgetCommonEnumType(). - Removes
typeMapping.put("enum", "Enum")fromAbstractJavaCodegen, preventing the format collision. - Adds a unit test in
JavaClientCodegenTestverifying that schemas withformat: enumgenerate validStringbacking fields and constructors without invalidEnum.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 inDefaultCodegen; the base implementation keeps thetypeMapping["enum"]lookup and falls back to the mapped object type when absent. - Overrides the hook in
AbstractJavaCodegento return"Enum", and removestypeMapping.put("enum", "Enum")soformat: enumstrings are no longer resolved to Java'sEnum. - Adds a unit test verifying
format: enumschemas generate String-backed enums withoutEnum.valueOf(...), invalidEnumsfields, orEnumimports.