[Java][Spring] useRecords: oneOf members as records

Merged
SE
1mo ago
+2102-220

Third step towards #10490, after #25238. With useRecords, a oneOf member now becomes a record that implements its interface (sealed with useSealed) and gets the discriminator getter of each interface above it, nested oneOf included. A member stays a class for the reasons already logged, or when a discriminator property is missing or nullable. With useRecords off the output is unchanged.

@cachescrubber @welshm @MelleD @atextor @manedev79 @javisst @borsch @banlevente @Zomzog @martin-mfg @KannaKim

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.

Summary by cubic

With useRecords, members of a oneOf interface are now generated as Java records that implement the (sealed) interface, including an accessor for each discriminator getter declared by the interfaces above them.

  • The accessor is annotated with @JsonProperty so Jackson serializes the discriminator once under its JSON name, including for enum discriminators and nested oneOf interfaces.
  • A member stays a class for the existing reasons (inheritance, additionalProperties, byte[] properties, property names a record can't have, builder clashes, request-parameter binding, component count) and when a discriminator property is missing or nullable under openApiNullable.
  • If two interfaces in the chain declare the same getter name for different properties, the member stays a class as well.
  • Added tests and regenerated samples; the spring-boot-records petstore sample now includes Animal and Zoo oneOf examples.