[Java][Spring] useRecords: fix object defaults of model-typed properties

Merged
SE
10d ago
+327-2

Fixes a bug of mine in #25200: with useRecords, a model-typed property with an object default (default: {}) generated new Sub() or new Sub().x("a"), which does not compile when Sub is a record. A model that such a default constructs now stays a class, with the reason logged. With useRecords off nothing changes.

@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

Fixes a bug in useRecords where a model-typed property with an object default (for example default: {}) generated new Sub() or new Sub().x("a"), which does not compile when Sub is a record. A model that such a default constructs now stays a class, with the reason logged. With useRecords off nothing changes.

  • The check covers defaults that construct nested models and models inside array defaults.
  • A model without properties still becomes a record, since its record's no-argument constructor compiles.