forked from loafle/openapi-generator-original
* fix(spring): Add Nullable import for array-type models (#22788) * refactor(spring): Extract helper methods for Nullable import logic (DRY) * chore: Regenerate Spring samples after Nullable import fix * Update sample files after regeneration * Fix phpunit.xml.dist: replace literal backslashes with forward slashes in directory paths * Add JavaDoc comment for addSpringNullableImportForOperation method * Fix line endings: replace CRLF (\r\n) with LF (\n) in exampleJson string literals * Remove unused Nullable imports from PR-related Spring sample files * Revert date/time example value changes (regeneration artifacts) * Fix: Skip Nullable import for enum models
This commit is contained in:
@@ -968,11 +968,6 @@ public class SpringCodegen extends AbstractJavaCodegen
|
||||
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,11 @@ public class SpringCodegen extends AbstractJavaCodegen
|
||||
codegenModel.imports.remove("Schema");
|
||||
}
|
||||
|
||||
// Only add Nullable import for non-enum models that may have nullable fields
|
||||
if (!Boolean.TRUE.equals(codegenModel.isEnum)) {
|
||||
addSpringNullableImport(codegenModel.imports);
|
||||
}
|
||||
|
||||
return codegenModel;
|
||||
}
|
||||
|
||||
@@ -1052,11 +1052,7 @@ public class SpringCodegen extends AbstractJavaCodegen
|
||||
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 +1215,26 @@ public class SpringCodegen extends AbstractJavaCodegen
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds Spring Nullable import if any parameter is nullable or optional.
|
||||
*/
|
||||
private void addSpringNullableImportForOperation(CodegenOperation codegenOperation) {
|
||||
if (isSpringCodegen()) {
|
||||
codegenOperation.allParams.stream()
|
||||
.filter(CodegenParameter::notRequiredOrIsNullable)
|
||||
.findAny()
|
||||
.ifPresent(param -> codegenOperation.imports.add("Nullable"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6223,4 +6223,37 @@ public class SpringCodegenTest {
|
||||
));
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user