Skip to content

fix(spring): Add Nullable import for array-type models (#22788)#22844

Merged
wing328 merged 11 commits intoOpenAPITools:masterfrom
spider-yamet:fix/nullable-import-array-models
Feb 4, 2026
Merged

fix(spring): Add Nullable import for array-type models (#22788)#22844
wing328 merged 11 commits intoOpenAPITools:masterfrom
spider-yamet:fix/nullable-import-array-models

Conversation

@spider-yamet
Copy link
Copy Markdown
Contributor

@spider-yamet spider-yamet commented Jan 28, 2026

Description

Fix missing Nullable import in array-type models generated by the Spring codegen. This resolves a compilation error where models using @Nullable annotation in toIndentedString() method were missing the required import.

Fixes #22788

Problem

Array-type models (schemas with type: array without explicit properties) were generated without the org.springframework.lang.Nullable import, causing compilation failure:

Root Cause

The Nullable import was being added in postProcessModelProperty(), but this method is only called for models with properties. Array-type models have no properties, so the hook was never invoked.

Solution

Moved the Nullable import logic from postProcessModelProperty() to fromModel(), which is called for all models regardless of whether they have properties.

Changes

  • modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java
    • Removed Nullable import logic from postProcessModelProperty()
    • Added Nullable import logic to fromModel()
  • modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java
    • Added shouldAddNullableImportForArrayTypeModels() test case

PR checklist

  • Added test case to cover the change
  • Documented fix in code comments
  • Verified fix by generating code and confirming successful compilation

Summary by cubic

Fixes missing org.springframework.lang.Nullable import in Spring-generated array-type models, preventing compilation errors when @nullable is used in toIndentedString(). The import is now added for all non-enum models.

  • Bug Fixes
    • Moved import logic to fromModel and extracted helpers, ensuring array-type models receive the Nullable import while skipping enums.
    • Added test to verify the import exists for array-type models (AnimalFarm.java) and standard models (Pet.java).

Written for commit 328be02. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

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

Choose a reason for hiding this comment

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

No issues found across 2 files

@wing328
Copy link
Copy Markdown
Member

wing328 commented Jan 30, 2026

thanks for the PR

cc @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)

@wing328
Copy link
Copy Markdown
Member

wing328 commented Jan 30, 2026

thanks for the pr

please follow step 3 to update the samples

@spider-yamet
Copy link
Copy Markdown
Contributor Author

spider-yamet commented Jan 30, 2026

Could you please check again, @wing328? I updated by following steps.

Regards

@spider-yamet
Copy link
Copy Markdown
Contributor Author

@wing328 could you please approve ci pipelines for my update?

Regards

Copy link
Copy Markdown
Contributor

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

Choose a reason for hiding this comment

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

3 issues found across 63 files (changes from recent commits).

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="samples/client/petstore/php-nextgen/OpenAPIClient-php/phpunit.xml.dist">

<violation number="1" location="samples/client/petstore/php-nextgen/OpenAPIClient-php/phpunit.xml.dist:5">
P2: Coverage include directories now contain literal backslashes (`./src\/Api`, `./src\/Model`), which changes the filesystem path and can cause PHPUnit to skip these directories for coverage collection.</violation>
</file>

<file name="samples/client/echo_api/php-nextgen/phpunit.xml.dist">

<violation number="1" location="samples/client/echo_api/php-nextgen/phpunit.xml.dist:5">
P2: The added backslash becomes part of the path in XML, so PHPUnit will look for a non-existent `src\Api` directory and skip coverage for that folder.</violation>
</file>

<file name="samples/client/echo_api/php-nextgen-streaming/phpunit.xml.dist">

<violation number="1" location="samples/client/echo_api/php-nextgen-streaming/phpunit.xml.dist:5">
P2: Coverage include paths now contain a literal backslash (`./src\/Api`, `./src\/Model`), which makes them refer to different/nonexistent directories and can cause PHPUnit to skip source files for coverage.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread samples/client/petstore/php-nextgen/OpenAPIClient-php/phpunit.xml.dist Outdated
Comment thread samples/client/echo_api/php-nextgen/phpunit.xml.dist Outdated
Comment thread samples/client/echo_api/php-nextgen-streaming/phpunit.xml.dist Outdated
}
}

private void addSpringNullableImportForOperation(CodegenOperation codegenOperation) {
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.

please add a docstring explaining what this function does

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.

Added JavaDoc comment to addSpringNullableImportForOperation method

// return StatusCode(200, default);
string exampleJson = null;
exampleJson = "{\n \"nullableName\" : \"nullableName\",\n \"name\" : \"name\"\n}";
exampleJson = "{\r\n \"nullableName\" : \"nullableName\",\r\n \"name\" : \"name\"\r\n}";
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.

did you use windows wsl to update the 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.

Yes, samples were regenerated on Windows, which explains the CRLF line endings in the string literals.

Copy link
Copy Markdown
Contributor Author

@spider-yamet spider-yamet Feb 2, 2026

Choose a reason for hiding this comment

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

And I have already resolved these WIndows WSL related issues. @wing328

@spider-yamet
Copy link
Copy Markdown
Contributor Author

spider-yamet commented Feb 2, 2026

@wing328 Could you please approve CI for my update? So sorry for inditacing directly. I fixed all the details following your comment.

Regards

@spider-yamet
Copy link
Copy Markdown
Contributor Author

@wing328 Now I'm not sure about the node 2 fails, I think my Spring Codegen fix is checked by node 0 (already passed), could you please re-run the pipelines if it is possible?

Regards

@wing328
Copy link
Copy Markdown
Member

wing328 commented Feb 3, 2026

circleci failure (can be ignored) not related to this PR

// to prevent inheritors (JavaCamelServerCodegen etc.) mistakenly use it
if (getName().contains("spring")) {
model.imports.add("Nullable");
}
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.

cc @slobodator who made these changes.

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.

@wing328 Let me share my opinion
Why the code was moved (not removed)
The problem (Issue #22788):
Array-type models like ResultCodes (type: array) were missing the Nullable import
The code was in postProcessModelProperty, which is called per property
Array-type models may have no properties or not trigger postProcessModelProperty the same way
Result: array-type models didn't get the Nullable import → compilation error
The solution:
Move the code from postProcessModelProperty to fromModel
fromModel is called once per model, regardless of properties
This ensures all models (including array-type) get the Nullable import
Why not keep it in both places?
It would add the import twice for regular models
fromModel is the right place because it handles all models uniformly
Current state:
The code is in fromModel (line 987-990)
It includes the enum check to skip enum models
Array-type models now get the import correctly
The code wasn't removed; it was moved to fix the array-type model bug.

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.

thanks for sharing more

@wing328 wing328 merged commit d284a9a into OpenAPITools:master Feb 4, 2026
14 of 15 checks passed
@wing328
Copy link
Copy Markdown
Member

wing328 commented Feb 4, 2026

thanks for the contribution. let's give it a try

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [JAVA][SPRING] Missing @Nullable import for array-type models in 7.19.0

2 participants