feat: add optional getters for nullable fields
JO
4d ago+18104+18104+01234567890123456789012345678901234567890123456789-222-222-012345678901234567890123456789
Closes: #24002
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)./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. - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
@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) @wing328
Summary by cubic
Adds the opt-in optionalGettersForNullableFieldsOnly option, which makes getters return Optional<T> for non-required, non-nullable fields in the Java restclient, resttemplate, and webclient libraries and the JavaSpring generator (closes #24002). The option requires Jackson 3 (useJackson3, which requires useSpringBoot4) and is off by default; fields and setters keep their raw types, and nullable and required fields are unaffected.
New Features
- Adds the option to
AbstractJavaCodegenand gates it behind a support check so only generators that implement it advertise it; other Java-based generators opt out. - Requires Jackson 3: enabling the option with Jackson 2 fails with a clear validation error, and the
resttemplateJackson 3 branch builds its JSON converter from a fully configuredJsonMapper. - Adds Spring Boot 4 + Jackson 3 + JSpecify sample configs, updated docs, Java/Spring tests, CI workflow paths, and Java 17/21 JDKs in the Travis CI matrix for Spring Boot 4 samples.
Bug Fixes
- Fixes indentation in generated nullable getters.
- Propagates the discriminator property to subtype models so discriminators are not wrapped in
Optionalwhen the option is enabled. - Drops only the Lombok
@Getterflag when the option is on so Lombok does not emit raw getters that bypassOptional<T>, and annotates raw nullable getters with@NullableinJavaSpring.