[Java] fix invalid `valueOf()` usage in enum templates

Closed
JO
2mo ago
+7650-566

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:
    ./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 Git BASH)
    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 (upcoming 7.x.0 minor 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:

  • BigDecimal has no BigDecimal.valueOf(BigDecimal) — e.g. type: number enums.

  • Object has no valueOf at all — e.g. an inline enum: without an explicit type, or a type: string enum with additionalProperties: false, which currently map to Object:

    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.mustache
  • Java/libraries/microprofile/enumClass.mustache (both the withXml and non-withXml branches)
  • JavaJaxRS/spec/enumClass.mustache
  • JavaJaxRS/cxf/enumClass.mustache
  • JavaJaxRS/cxf-cdi/enumClass.mustache
  • JavaJaxRS/cxf-ext/enumClass.mustache
  • java-helidon/client/libraries/mp/enumClass.mustache
  • java-helidon/client/libraries/se/enumClass.mustache
  • java-helidon/server/libraries/mp/enumClass.mustache

Differences vs. #21055

  • Rebuilt on current master, so no merge conflicts.
  • Java/libraries/okhttp-gson/modelInnerEnum.mustache no longer needs a change: master already emits {{{name}}}({{{value}}}) there.
  • Fixed the unbalanced parentheses in Java/libraries/microprofile/enumClass.mustache that were introduced in #21055 (the closing ) was split into two separate mustache sections, producing String.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 invalid Object.valueOf(...) for the newly added test case. There is now no Object.valueOf left anywhere under samples/.

Test coverage

Added to the petstore test specs (kept from #21055):

  • Order.paymentMethod — a numeric (type: number) enum, covering the BigDecimal case.
  • Order.OrderStatus — a type: string enum with additionalProperties: false, covering the Object case.
  • enum_form_integer / enum_form_double form parameters on testEnumParameters.

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), and java-helidon (client mp/se, server mp) enum templates.
  • Adds petstore test specs covering a numeric enum (Order.paymentMethod), a string enum with additionalProperties: false (Order.OrderStatus), and integer/double form parameters.