Skip to content

[KOTLIN-SPRING;JAVA-SPRING] bugfix: update JsonInclude annotations for optional+nullable fields to handle serialization correctly when openApiNullable = true#24185

Open
Picazsoo wants to merge 4 commits into
OpenAPITools:masterfrom
Picazsoo:bugfix/open-api-nullable-json-include-non-absent-for-JsonNullable
Open

[KOTLIN-SPRING;JAVA-SPRING] bugfix: update JsonInclude annotations for optional+nullable fields to handle serialization correctly when openApiNullable = true#24185
Picazsoo wants to merge 4 commits into
OpenAPITools:masterfrom
Picazsoo:bugfix/open-api-nullable-json-include-non-absent-for-JsonNullable

Conversation

@Picazsoo

@Picazsoo Picazsoo commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Fix: @JsonInclude(NON_ABSENT) for JsonNullable fields in kotlin-spring and java-spring generators

Issue

When openApiNullable=true, optional+nullable fields are generated as JsonNullable<T> but without any @JsonInclude annotation. This means JsonNullable.undefined() (the "absent" state) could be serialized into the JSON output, defeating the purpose of the three-state wrapper.

Fix

Added @field:JsonInclude(JsonInclude.Include.NON_ABSENT) to any field where x-is-jackson-optional-nullable is true, in dataClassOptVar.mustache. This ensures:

State Serialized?
JsonNullable.undefined() ❌ excluded
JsonNullable.of(null) ✅ included as null
JsonNullable.of("value") ✅ included as "value"

This is a nasty bug that should be fixed - because someone using the generated clients for patch operations might be unknowingly deleting resources even when setting JsonNullable as undefined.

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. - spring: @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); kotlin: @karismann (2019/03) @Zomzog (2019/04) @andrewemery (2019/10) @4brunu (2019/11) @yutaka0m (2020/03) @stefankoppier (2022/06) @e5l (2024/10) @dennisameling (2026/02), @wing328

Summary by cubic

Fixes serialization of optional+nullable fields for kotlin-spring and java-spring when openApiNullable=true by adding @JsonInclude(JsonInclude.Include.NON_ABSENT) to org.openapitools.jackson.nullable.JsonNullable fields. Undefined values are omitted; nulls and concrete values are included.

  • Bug Fixes
    • kotlin-spring: emit @field:JsonInclude(JsonInclude.Include.NON_ABSENT) in dataClassOptVar.mustache when x-is-jackson-optional-nullable is true.
    • java-spring: emit @JsonInclude(JsonInclude.Include.NON_ABSENT) in JavaSpring/pojo.mustache; ensure JsonInclude import in SpringCodegen.postProcessModelProperty for optional+nullable fields when openApiNullable=true.
    • Tests: expanded Java and Kotlin generator tests for openApiNullable true/false; added Kotlin runtime tests to verify undefined/null/value serialization.
    • Samples: regenerated Spring Boot and Spring HTTP Interface models to include NON_ABSENT on JsonNullable fields.

Written for commit 000a348. Summary will update on new commits.

Review in cubic

@Picazsoo Picazsoo marked this pull request as ready for review July 2, 2026 10:41
@Picazsoo Picazsoo marked this pull request as draft July 2, 2026 10:41

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

Re-trigger cubic

@Picazsoo Picazsoo changed the title [KOTLIN-SPRING] fix: update JsonInclude annotations for optional+nullable fields to handle serialization correctly when openApiNullable = true [KOTLIN-SPRING;JAVA-SPRING] fix: update JsonInclude annotations for optional+nullable fields to handle serialization correctly when openApiNullable = true Jul 2, 2026
@Picazsoo Picazsoo marked this pull request as ready for review July 2, 2026 13:40
@Picazsoo Picazsoo changed the title [KOTLIN-SPRING;JAVA-SPRING] fix: update JsonInclude annotations for optional+nullable fields to handle serialization correctly when openApiNullable = true [KOTLIN-SPRING;JAVA-SPRING] bugfix: update JsonInclude annotations for optional+nullable fields to handle serialization correctly when openApiNullable = true Jul 2, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 7 files

Re-trigger cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant