Skip to content

Commit 9298110

Browse files
committed
Fix annotationLibrary option being ignored in spring generator
1 parent ddb15d4 commit 9298110

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,15 @@ public void processOpts() {
595595
}
596596

597597
// imports for pojos
598-
importMapping.put("ApiModelProperty", "io.swagger.annotations.ApiModelProperty");
599-
importMapping.put("ApiModel", "io.swagger.annotations.ApiModel");
600-
importMapping.put("Schema", "io.swagger.v3.oas.annotations.media.Schema");
598+
if (AnnotationLibrary.SWAGGER1.equals(getAnnotationLibrary())) {
599+
importMapping.put("ApiModelProperty", "io.swagger.annotations.ApiModelProperty");
600+
importMapping.put("ApiModel", "io.swagger.annotations.ApiModel");
601+
}
602+
603+
if (AnnotationLibrary.SWAGGER2.equals(getAnnotationLibrary())) {
604+
importMapping.put("Schema", "io.swagger.v3.oas.annotations.media.Schema");
605+
}
606+
601607
importMapping.put("BigDecimal", "java.math.BigDecimal");
602608
importMapping.put("JsonDeserialize", "com.fasterxml.jackson.databind.annotation.JsonDeserialize");
603609
importMapping.put("JsonProperty", "com.fasterxml.jackson.annotation.JsonProperty");

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import static org.openapitools.codegen.languages.features.DocumentationProviderFeatures.ANNOTATION_LIBRARY;
6666
import static org.openapitools.codegen.languages.features.DocumentationProviderFeatures.DOCUMENTATION_PROVIDER;
6767
import static org.testng.Assert.assertEquals;
68+
import static org.testng.Assert.assertNotNull;
6869
import static org.testng.Assert.fail;
6970

7071
public class SpringCodegenTest {
@@ -5738,4 +5739,72 @@ public void testOneOfInterfaceWithAnnotation() throws IOException {
57385739
.isInterface()
57395740
.assertTypeAnnotations().containsWithName("SuppressWarnings");
57405741
}
5742+
5743+
@Test
5744+
public void annotationLibraryDoesNotCauseImportConflictsInSpring() throws IOException {
5745+
Map<String, Object> properties = new HashMap<>();
5746+
properties.put("documentationProvider", "source");
5747+
properties.put("annotationLibrary", "none");
5748+
5749+
File output = Files.createTempDirectory("test").toFile();
5750+
output.deleteOnExit();
5751+
5752+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/java/native/issue21991.yaml");
5753+
5754+
SpringCodegen codegen = new SpringCodegen();
5755+
codegen.setLibrary(SPRING_BOOT);
5756+
codegen.setOpenAPI(openAPI);
5757+
codegen.setOutputDir(output.getAbsolutePath());
5758+
codegen.additionalProperties().putAll(properties);
5759+
5760+
ClientOptInput input = new ClientOptInput()
5761+
.openAPI(openAPI)
5762+
.config(codegen);
5763+
5764+
DefaultGenerator generator = new DefaultGenerator();
5765+
5766+
Map<String, File> files = generator.opts(input).generate().stream()
5767+
.collect(Collectors.toMap(File::getName, Function.identity()));
5768+
5769+
File apiFile = files.get("Schema.java");
5770+
assertNotNull(apiFile);
5771+
5772+
JavaFileAssert.assertThat(apiFile).fileDoesNotContain(
5773+
"import io.swagger.v3.oas.annotations.media.Schema;"
5774+
);
5775+
}
5776+
5777+
@Test
5778+
public void annotationLibraryDoesNotCauseImportConflictsInSpringWithAnnotationLibrary() throws IOException {
5779+
Map<String, Object> properties = new HashMap<>();
5780+
properties.put("documentationProvider", "source");
5781+
properties.put("annotationLibrary", "swagger2");
5782+
5783+
File output = Files.createTempDirectory("test").toFile();
5784+
output.deleteOnExit();
5785+
5786+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/java/native/issue21991.yaml");
5787+
5788+
SpringCodegen codegen = new SpringCodegen();
5789+
codegen.setOpenAPI(openAPI);
5790+
codegen.setLibrary(SPRING_BOOT);
5791+
codegen.setOutputDir(output.getAbsolutePath());
5792+
codegen.additionalProperties().putAll(properties);
5793+
5794+
ClientOptInput input = new ClientOptInput()
5795+
.openAPI(openAPI)
5796+
.config(codegen);
5797+
5798+
DefaultGenerator generator = new DefaultGenerator();
5799+
5800+
Map<String, File> files = generator.opts(input).generate().stream()
5801+
.collect(Collectors.toMap(File::getName, Function.identity()));
5802+
5803+
File apiFile = files.get("Schema.java");
5804+
assertNotNull(apiFile);
5805+
5806+
JavaFileAssert.assertThat(apiFile).fileContains(
5807+
"import io.swagger.v3.oas.annotations.media.Schema;"
5808+
);
5809+
}
57415810
}

0 commit comments

Comments
 (0)