-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
fix(spring): Add Nullable import for array-type models (#22788) #22844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
6a5d888
9e47b88
1815a8e
2c985ad
feb87f5
554fc76
59c9b0c
40b0922
e07e395
bdc2ed5
328be02
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -968,11 +968,6 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert | |
| if (model.getVendorExtensions().containsKey("x-jackson-optional-nullable-helpers")) { | ||
| model.imports.add("Arrays"); | ||
| } | ||
|
|
||
| // to prevent inheritors (JavaCamelServerCodegen etc.) mistakenly use it | ||
| if (getName().contains("spring")) { | ||
| model.imports.add("Nullable"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -989,6 +984,8 @@ public CodegenModel fromModel(String name, Schema model) { | |
| codegenModel.imports.remove("Schema"); | ||
| } | ||
|
|
||
| addSpringNullableImport(codegenModel.imports); | ||
|
|
||
| return codegenModel; | ||
| } | ||
|
|
||
|
|
@@ -1052,11 +1049,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation | |
| codegenOperation.imports.addAll(provideArgsClassSet); | ||
| } | ||
|
|
||
| // to prevent inheritors (JavaCamelServerCodegen etc.) mistakenly use it | ||
| if (getName().contains("spring")) { | ||
| codegenOperation.allParams.stream().filter(CodegenParameter::notRequiredOrIsNullable).findAny() | ||
| .ifPresent(p -> codegenOperation.imports.add("Nullable")); | ||
| } | ||
| addSpringNullableImportForOperation(codegenOperation); | ||
|
|
||
| if (reactive) { | ||
| if (DocumentationProvider.SPRINGFOX.equals(getDocumentationProvider())) { | ||
|
|
@@ -1219,4 +1212,23 @@ public List<VendorExtension> getSupportedVendorExtensions() { | |
| extensions.add(VendorExtension.X_SPRING_API_VERSION); | ||
| return extensions; | ||
| } | ||
|
|
||
| private boolean isSpringCodegen() { | ||
| return getName().contains("spring"); | ||
| } | ||
|
|
||
| private void addSpringNullableImport(Set<String> imports) { | ||
| if (isSpringCodegen()) { | ||
| imports.add("Nullable"); | ||
| } | ||
| } | ||
|
|
||
| private void addSpringNullableImportForOperation(CodegenOperation codegenOperation) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add a docstring explaining what this function does
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added JavaDoc comment to addSpringNullableImportForOperation method |
||
| if (isSpringCodegen()) { | ||
| codegenOperation.allParams.stream() | ||
| .filter(CodegenParameter::notRequiredOrIsNullable) | ||
| .findAny() | ||
| .ifPresent(param -> codegenOperation.imports.add("Nullable")); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,7 @@ public virtual IActionResult FakeNullableExampleTest() | |
| //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... | ||
| // 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}"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you use windows wsl to update the samples?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And I have already resolved these WIndows WSL related issues. @wing328 |
||
|
|
||
| var example = exampleJson != null | ||
| ? JsonConvert.DeserializeObject<TestNullable>(exampleJson) | ||
|
|
||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for sharing more