[Java][Spring] fix ParameterObject import for Spring Boot 3 (#14454)

fix #14077
This commit is contained in:
Oleh Kurpiak 2023-01-14 18:00:32 +02:00 committed by GitHub
parent be87382fde
commit 27137e75ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -485,6 +485,9 @@ public class SpringCodegen extends AbstractJavaCodegen
importMapping.put("DateTimeFormat", "org.springframework.format.annotation.DateTimeFormat");
importMapping.put("ApiIgnore", "springfox.documentation.annotations.ApiIgnore");
importMapping.put("ParameterObject", "org.springdoc.api.annotations.ParameterObject");
if (isUseSpringBoot3()) {
importMapping.put("ParameterObject", "org.springdoc.core.annotations.ParameterObject");
}
if (useOptional) {
writePropertyBack(USE_OPTIONAL, useOptional);

View File

@ -25,6 +25,7 @@ import static org.openapitools.codegen.languages.SpringCodegen.INTERFACE_ONLY;
import static org.openapitools.codegen.languages.SpringCodegen.REQUEST_MAPPING_OPTION;
import static org.openapitools.codegen.languages.SpringCodegen.RESPONSE_WRAPPER;
import static org.openapitools.codegen.languages.SpringCodegen.SPRING_BOOT;
import static org.openapitools.codegen.languages.SpringCodegen.USE_SPRING_BOOT3;
import static org.openapitools.codegen.languages.features.DocumentationProviderFeatures.DOCUMENTATION_PROVIDER;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
@ -1436,6 +1437,35 @@ public class SpringCodegenTest {
.assertMethod("getWithMapOfStrings").hasReturnType("ResponseEntity<Map<String, String>>");
}
@Test
public void paramObjectImportForDifferentSpringBootVersions_issue14077() throws Exception {
Map<String, Object> additionalProperties = new HashMap<>();
additionalProperties.put(SpringCodegen.USE_TAGS, "true");
additionalProperties.put(DOCUMENTATION_PROVIDER, "springdoc");
additionalProperties.put(SpringCodegen.INTERFACE_ONLY, "true");
additionalProperties.put(SpringCodegen.SKIP_DEFAULT_INTERFACE, "true");
Map<String, File> files = generateFromContract("src/test/resources/2_0/petstore-with-spring-pageable.yaml", SPRING_BOOT, additionalProperties);
JavaFileAssert.assertThat(files.get("PetApi.java"))
.hasImports("org.springdoc.api.annotations.ParameterObject")
.assertMethod("findPetsByStatus")
.hasParameter("pageable").withType("Pageable")
.assertParameterAnnotations()
.containsWithName("ParameterObject");
// different import for SB3
additionalProperties.put(USE_SPRING_BOOT3, "true");
files = generateFromContract("src/test/resources/2_0/petstore-with-spring-pageable.yaml", SPRING_BOOT, additionalProperties);
JavaFileAssert.assertThat(files.get("PetApi.java"))
.hasImports("org.springdoc.core.annotations.ParameterObject")
.assertMethod("findPetsByStatus")
.hasParameter("pageable").withType("Pageable")
.assertParameterAnnotations()
.containsWithName("ParameterObject");
}
@Test
public void shouldSetDefaultValueForMultipleArrayItems() throws IOException {
Map<String, Object> additionalProperties = new HashMap<>();