Skip to content

Commit b2fe2ce

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

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,11 @@ public CodegenModel fromModel(String name, Schema model) {
981981
codegenModel.imports.remove("ApiModel");
982982
}
983983

984+
if (getAnnotationLibrary() != AnnotationLibrary.SWAGGER2) {
985+
// remove swagger imports
986+
codegenModel.imports.remove("Schema");
987+
}
988+
984989
return codegenModel;
985990
}
986991

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)