Skip to content

[BUG][kotlin-spring] missing JsonInclude import for optional+nullable-only models when using jsonNullable#24368

Open
Picazsoo wants to merge 5 commits into
OpenAPITools:masterfrom
Picazsoo:bugfix/missing-import-com.fasterxml.jackson.annotation.JsonInclude
Open

[BUG][kotlin-spring] missing JsonInclude import for optional+nullable-only models when using jsonNullable#24368
Picazsoo wants to merge 5 commits into
OpenAPITools:masterfrom
Picazsoo:bugfix/missing-import-com.fasterxml.jackson.annotation.JsonInclude

Conversation

@Picazsoo

@Picazsoo Picazsoo commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

fixes #24365

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 missing com.fasterxml.jackson.annotation.JsonInclude import in Kotlin Spring models whose only field is optional + nullable when jsonNullable is enabled, preventing compile errors from @field:JsonInclude(NON_ABSENT). Also ensures org.openapitools.jackson.nullable.JsonNullable is imported only when nullable fields are present.

  • Bug Fixes
    • Kotlin Spring: add JsonInclude to model imports when openApiNullable=true and a property is optional + nullable (including optional+nullable-only models).
    • Spring generators: add JsonNullable import whenever a property is nullable and remove the unconditional import from JavaSpring/model.mustache.
    • Add regression test and minimal spec for the optional-nullable-only case; update petstore YAML and generated samples, removing unused JsonNullable imports.

Written for commit ee2e636. Summary will update on new commits.

Review in cubic

@Picazsoo
Picazsoo marked this pull request as ready for review July 20, 2026 17:20

@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.

1 issue found across 12 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/server/petstore/springboot-sort-validation/src/main/java/org/openapitools/model/OptionalNullableOnlyModel.java">

<violation number="1" location="samples/server/petstore/springboot-sort-validation/src/main/java/org/openapitools/model/OptionalNullableOnlyModel.java:10">
P3: Duplicate `JsonNullable` import in the generated Java model. The import `org.openapitools.jackson.nullable.JsonNullable` appears twice (lines 12 and 19). While Java permits duplicate imports without a compilation error, this dead import is unnecessary and suggests the generator's import-dedup logic can produce a double entry when both the main code path and the `JsonInclude`-fix path independently add the same import. Removing the second occurrence keeps the generated code clean.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.Arrays;
import java.util.UUID;
import org.openapitools.jackson.nullable.JsonNullable;

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.

P3: Duplicate JsonNullable import in the generated Java model. The import org.openapitools.jackson.nullable.JsonNullable appears twice (lines 12 and 19). While Java permits duplicate imports without a compilation error, this dead import is unnecessary and suggests the generator's import-dedup logic can produce a double entry when both the main code path and the JsonInclude-fix path independently add the same import. Removing the second occurrence keeps the generated code clean.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/server/petstore/springboot-sort-validation/src/main/java/org/openapitools/model/OptionalNullableOnlyModel.java, line 10:

<comment>Duplicate `JsonNullable` import in the generated Java model. The import `org.openapitools.jackson.nullable.JsonNullable` appears twice (lines 12 and 19). While Java permits duplicate imports without a compilation error, this dead import is unnecessary and suggests the generator's import-dedup logic can produce a double entry when both the main code path and the `JsonInclude`-fix path independently add the same import. Removing the second occurrence keeps the generated code clean.</comment>

<file context>
@@ -0,0 +1,99 @@
+import com.fasterxml.jackson.annotation.JsonCreator;
+import java.util.Arrays;
+import java.util.UUID;
+import org.openapitools.jackson.nullable.JsonNullable;
+import org.springframework.lang.Nullable;
+import java.util.NoSuchElementException;
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is existing issue. I will eventually fix it, but I want to focus on the core issue here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

for duplicate import, i think we can create a lambda to remove duplicated lines in a text block. not the best approach but i think it should work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can fix the root cause in this case. It is just that this very PR fixes very similar oversight where the import was not added at all. I am worried that I might introduce another regression. But I will take a stab at it.

@Picazsoo Picazsoo Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I fixed the issue with the duplicate JsonNullable import. It was removed from A LOT of samples.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So in general @wing328 , I think this can be merged whenever you feel like it.

@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 1560 files

Note: This PR contains a large number of files. cubic only reviews up to 200 files per PR, so some files may not have been reviewed. cubic prioritizes the most important files to review.

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.

[BUG][kotlin-spring] missing JsonInclude import for optional+nullable-only models when using jsonNullable

2 participants