[Java] fix invalid `valueOf()` usage in enum templates
PR checklist
- Read the contribution guidelines.
- Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
- Run the following to build the project and update samples:
(For Windows users, please run the script in Git BASH)./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. - File the PR against the correct branch:
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks) - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
Technical Committee
@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08)
Description
This supersedes #21055, which had become unmergeable (conflicting with master) and whose author is no longer available. As requested by @wing328 (comment), this is a fresh PR based on the current master, with all conflicts resolved and the CI failures fixed. Full credit to @timon-sbr and @martin-mfg (kept as co-authors of the commit).
Problem
The Java enum templates wrapped every enum value with {{dataType}}.valueOf(...):
{{{name}}}({{^isUri}}{{dataType}}.valueOf({{/isUri}}{{{value}}}{{^isUri}}){{/isUri}})
valueOf() is not available for every possible data type, which produces code that does not compile:
-
BigDecimalhas noBigDecimal.valueOf(BigDecimal)— e.g.type: numberenums. -
Objecthas novalueOfat all — e.g. an inlineenum:without an explicittype, or atype: stringenum withadditionalProperties: false, which currently map toObject:public enum OrderStatusEnum { PENDING(Object.valueOf("PENDING")), // does not compile PROCESSING(Object.valueOf("PROCESSING"));
Fix
valueOf() is now only emitted for types that actually provide it, i.e. it is skipped for isUri, isNumeric and isFreeFormObject:
{{{name}}}({{^isUri}}{{^isNumeric}}{{^isFreeFormObject}}{{dataType}}.valueOf({{/isFreeFormObject}}{{/isNumeric}}{{/isUri}}{{{value}}}{{^isUri}}{{^isNumeric}}{{^isFreeFormObject}}){{/isFreeFormObject}}{{/isNumeric}}{{/isUri}})
The wrapper was originally added to fix Boolean enums (#19815), and Boolean/String/Integer keep using valueOf(), so this is non-breaking.
Templates updated:
Java/modelInnerEnum.mustacheJava/libraries/microprofile/enumClass.mustache(both thewithXmland non-withXmlbranches)JavaJaxRS/spec/enumClass.mustacheJavaJaxRS/cxf/enumClass.mustacheJavaJaxRS/cxf-cdi/enumClass.mustacheJavaJaxRS/cxf-ext/enumClass.mustachejava-helidon/client/libraries/mp/enumClass.mustachejava-helidon/client/libraries/se/enumClass.mustachejava-helidon/server/libraries/mp/enumClass.mustache
Differences vs. #21055
- Rebuilt on current
master, so no merge conflicts. Java/libraries/okhttp-gson/modelInnerEnum.mustacheno longer needs a change:masteralready emits{{{name}}}({{{value}}})there.- Fixed the unbalanced parentheses in
Java/libraries/microprofile/enumClass.mustachethat were introduced in #21055 (the closing)was split into two separate mustache sections, producingString.valueOf("A")))). This was the cause of the CI failure reported in that PR. - Extended the fix to the
JavaJaxRS/*templates, which were still emitting the invalidObject.valueOf(...)for the newly added test case. There is now noObject.valueOfleft anywhere undersamples/.
Test coverage
Added to the petstore test specs (kept from #21055):
Order.paymentMethod— a numeric (type: number) enum, covering theBigDecimalcase.Order.OrderStatus— atype: stringenum withadditionalProperties: false, covering theObjectcase.enum_form_integer/enum_form_doubleform parameters ontestEnumParameters.
All 800 sample configurations regenerate successfully.
This PR closes #20188 and replaces #21055.
Summary by cubic
Fixes invalid valueOf() usage in Java enum templates so generated enums compile for all data types. Previously every enum value was wrapped with {{dataType}}.valueOf(...), which breaks for BigDecimal (no valueOf(BigDecimal)) and Object (no valueOf at all). Now valueOf() is only emitted for types that actually have it, skipping isUri, isNumeric, and isFreeFormObject values. The Boolean enum fix from #19815 is preserved.
Details
- Updates
Java/modelInnerEnum.mustache,Java/libraries/microprofile/enumClass.mustache,JavaJaxRS(spec,cxf,cxf-cdi,cxf-ext), andjava-helidon(clientmp/se, servermp) enum templates. - Adds petstore test specs covering a numeric enum (
Order.paymentMethod), a string enum withadditionalProperties: false(Order.OrderStatus), and integer/double form parameters.