Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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");
}
}

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

addSpringNullableImport(codegenModel.imports);

return codegenModel;
}

Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -1219,4 +1212,26 @@ 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");
}
}

/**
* Adds Spring Nullable import if any parameter is nullable or optional.
*/
private void addSpringNullableImportForOperation(CodegenOperation codegenOperation) {
Copy link
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
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

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
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");
}

}