[kotlin-spring, java-spring] - gate @JsonSetter on openApiNullable for optional non-nullable fields
This PR fixes #23976 by making @JsonSetter generation respect the openApiNullable decision agreed in the issue discussion.
Summary of changes
-
For optional, non-nullable properties (
required: false,nullable: false):openApiNullable=falsenow generates@JsonSetter(nulls = Nulls.SKIP), so explicit JSONnullis ignored and existing/default values are protected.openApiNullable=truekeeps/generates@JsonSetter(nulls = Nulls.FAIL), so explicit JSONnullis rejected for stricter PATCH-style semantics.
-
The Kotlin Spring generator was updated to stop unconditionally emitting
Nulls.FAILfor optional non-nullable fields. -
Java Spring was aligned for the
openApiNullable=falsecase by adding@JsonSetter(nulls = Nulls.SKIP)to generated setters, including the required Jackson imports. This is especially important for optional non-nullable attributes with defaults where it was possible to overwrite the expected default. -
Tests and sample outputs were updated to cover:
- Kotlin Spring behavior with and without
openApiNullable - default-value protection via
Nulls.SKIP - Jackson 3 import behavior
- regenerated Kotlin Spring and Java Spring samples
- Kotlin Spring behavior with and without
-
Kotlin Spring documentation for
openApiNullablewas expanded to explain the new strict-vs-lenient null handling behavior.Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com
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. - tagging: @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08) and @karismann (2019/03) @Zomzog (2019/04) @andrewemery (2019/10) @4brunu (2019/11) @yutaka0m (2020/03) @stefankoppier (2022/06) @e5l (2024/10) @dennisameling (2026/02)
It would be really good to get some java-spring feedback for the Nulls.SKIP for optional non-nullable.
Summary by cubic
Gate @JsonSetter on openApiNullable and add @JsonInclude(NON_NULL) for optional, non-nullable fields in Kotlin Spring and Java Spring. Annotations are only generated when Jackson is enabled.
- Bug Fixes
- Optional non-nullable fields now respect
openApiNullable: false →@JsonSetter(nulls = Nulls.SKIP); true →@JsonSetter(nulls = Nulls.FAIL). - Added
@JsonInclude(JsonInclude.Include.NON_NULL)so nulls are omitted from JSON output. - Java Spring aligns with
@JsonSetter(nulls = Nulls.SKIP)foropenApiNullable=falseand mapsJsonSetter/Nullstocom.fasterxml.jackson.annotation; gated by Jackson config. Updated templates, tests, samples, and Kotlin Spring docs.
- Optional non-nullable fields now respect