fix(spring): Add Nullable import for array-type models (#22788)#22844
fix(spring): Add Nullable import for array-type models (#22788)#22844wing328 merged 11 commits intoOpenAPITools:masterfrom
Conversation
|
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) |
|
thanks for the pr please follow step 3 to update the samples |
|
Could you please check again, @wing328? I updated by following steps. Regards |
|
@wing328 could you please approve ci pipelines for my update? Regards |
There was a problem hiding this comment.
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.
…s in directory paths
| } | ||
| } | ||
|
|
||
| private void addSpringNullableImportForOperation(CodegenOperation codegenOperation) { |
There was a problem hiding this comment.
please add a docstring explaining what this function does
There was a problem hiding this comment.
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}"; |
There was a problem hiding this comment.
did you use windows wsl to update the samples?
There was a problem hiding this comment.
Yes, samples were regenerated on Windows, which explains the CRLF line endings in the string literals.
There was a problem hiding this comment.
And I have already resolved these WIndows WSL related issues. @wing328
|
@wing328 Could you please approve CI for my update? So sorry for inditacing directly. I fixed all the details following your comment. Regards |
|
@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 |
|
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"); | ||
| } |
There was a problem hiding this comment.
@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.
|
thanks for the contribution. let's give it a try |
Description
Fix missing
Nullableimport in array-type models generated by the Spring codegen. This resolves a compilation error where models using@Nullableannotation intoIndentedString()method were missing the required import.Fixes #22788
Problem
Array-type models (schemas with
type: arraywithout explicit properties) were generated without theorg.springframework.lang.Nullableimport, causing compilation failure:Root Cause
The
Nullableimport was being added inpostProcessModelProperty(), 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
Nullableimport logic frompostProcessModelProperty()tofromModel(), which is called for all models regardless of whether they have properties.Changes
modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.javaNullableimport logic frompostProcessModelProperty()Nullableimport logic tofromModel()modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.javashouldAddNullableImportForArrayTypeModels()test casePR checklist
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.
Written for commit 328be02. Summary will update on new commits.