[Java][Spring] useRecords: records with additionalProperties

Merged
SE
15d ago
+1950-10

Fifth step towards #10490. With useRecords and useSpringBoot4, a model with additionalProperties becomes a record whose last component is the map, under @JsonAnySetter and @JsonAnyGetter. Jackson reads such a component only from 2.18, and Spring Boot 3 brings 2.17, so on Boot 3 these models stay classes and the reason is logged. Unlike the class, the map keeps keys in reverse document order.

@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 on Spring Boot 4, a model with additionalProperties is now generated as a record whose map of undeclared properties is its last component, annotated with @JsonAnySetter and @JsonAnyGetter. On Spring Boot 3 the model stays a class and the reason is logged, because reading the map through a record constructor requires Jackson 2.18 and the generated Boot 3 project brings 2.17.

  • The compact constructor gives an absent map an empty LinkedHashMap, and getAdditionalProperty(key) accesses entries.
  • Builders accept and copy the map; toBuilder keeps the map's entries.
  • Models bound from request parameters and models with a property named additionalProperties also stay classes.
  • Adds the spring-boot-4-records sample and its tests.

A model generated this way keeps map keys in reverse document order, unlike the class.