Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
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

}

@Override
Expand All @@ -989,6 +984,11 @@ public CodegenModel fromModel(String name, Schema model) {
codegenModel.imports.remove("Schema");
}

// Add Nullable import for all models including array-type models (issue #22788)
if (getName().contains("spring")) {
codegenModel.imports.add("Nullable");
}

return codegenModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6223,4 +6223,37 @@ public void testExtensionsOnSchema_issue9183() throws IOException {
));
}

@Test
public void shouldAddNullableImportForArrayTypeModels() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

final OpenAPI openAPI = TestUtils.parseFlattenSpec(
"src/test/resources/3_0/spring/petstore-with-fake-endpoints-models-for-testing-with-spring-pageable.yaml");
final SpringCodegen codegen = new SpringCodegen();
codegen.setOpenAPI(openAPI);
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(INTERFACE_ONLY, "true");
codegen.additionalProperties().put(CodegenConstants.GENERATE_ALIAS_AS_MODEL, "true");

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
generator.setGenerateMetadata(false);
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");

Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));

// AnimalFarm is an array-type model with no properties (issue #22788)
JavaFileAssert.assertThat(files.get("AnimalFarm.java"))
.hasImports("org.springframework.lang.Nullable");
JavaFileAssert.assertThat(files.get("Pet.java"))
.hasImports("org.springframework.lang.Nullable");
}

}
Loading