Skip to content

Commit 9f3def0

Browse files
committed
Fix annotationLibrary option being ignored in spring generator
1 parent 37cac71 commit 9f3def0

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
@@ -988,6 +988,11 @@ public CodegenModel fromModel(String name, Schema model) {
988988
codegenModel.imports.remove("ApiModel");
989989
}
990990

991+
if (getAnnotationLibrary() != AnnotationLibrary.SWAGGER2) {
992+
// remove swagger imports
993+
codegenModel.imports.remove("Schema");
994+
}
995+
991996
return codegenModel;
992997
}
993998

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 {
@@ -5759,4 +5760,72 @@ public void testApiVersion() throws IOException {
57595760
.assertMethodAnnotations()
57605761
.containsWithNameAndDoesContainAttributes("RequestMapping", List.of("version"));
57615762
}
5763+
5764+
@Test
5765+
public void annotationLibraryDoesNotCauseImportConflictsInSpring() throws IOException {
5766+
Map<String, Object> properties = new HashMap<>();
5767+
properties.put("documentationProvider", "source");
5768+
properties.put("annotationLibrary", "none");
5769+
5770+
File output = Files.createTempDirectory("test").toFile();
5771+
output.deleteOnExit();
5772+
5773+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/java/native/issue21991.yaml");
5774+
5775+
SpringCodegen codegen = new SpringCodegen();
5776+
codegen.setLibrary(SPRING_BOOT);
5777+
codegen.setOpenAPI(openAPI);
5778+
codegen.setOutputDir(output.getAbsolutePath());
5779+
codegen.additionalProperties().putAll(properties);
5780+
5781+
ClientOptInput input = new ClientOptInput()
5782+
.openAPI(openAPI)
5783+
.config(codegen);
5784+
5785+
DefaultGenerator generator = new DefaultGenerator();
5786+
5787+
Map<String, File> files = generator.opts(input).generate().stream()
5788+
.collect(Collectors.toMap(File::getName, Function.identity()));
5789+
5790+
File apiFile = files.get("Schema.java");
5791+
assertNotNull(apiFile);
5792+
5793+
JavaFileAssert.assertThat(apiFile).fileDoesNotContain(
5794+
"import io.swagger.v3.oas.annotations.media.Schema;"
5795+
);
5796+
}
5797+
5798+
@Test
5799+
public void annotationLibraryDoesNotCauseImportConflictsInSpringWithAnnotationLibrary() throws IOException {
5800+
Map<String, Object> properties = new HashMap<>();
5801+
properties.put("documentationProvider", "source");
5802+
properties.put("annotationLibrary", "swagger2");
5803+
5804+
File output = Files.createTempDirectory("test").toFile();
5805+
output.deleteOnExit();
5806+
5807+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/java/native/issue21991.yaml");
5808+
5809+
SpringCodegen codegen = new SpringCodegen();
5810+
codegen.setOpenAPI(openAPI);
5811+
codegen.setLibrary(SPRING_BOOT);
5812+
codegen.setOutputDir(output.getAbsolutePath());
5813+
codegen.additionalProperties().putAll(properties);
5814+
5815+
ClientOptInput input = new ClientOptInput()
5816+
.openAPI(openAPI)
5817+
.config(codegen);
5818+
5819+
DefaultGenerator generator = new DefaultGenerator();
5820+
5821+
Map<String, File> files = generator.opts(input).generate().stream()
5822+
.collect(Collectors.toMap(File::getName, Function.identity()));
5823+
5824+
File apiFile = files.get("Schema.java");
5825+
assertNotNull(apiFile);
5826+
5827+
JavaFileAssert.assertThat(apiFile).fileContains(
5828+
"import io.swagger.v3.oas.annotations.media.Schema;"
5829+
);
5830+
}
57625831
}

0 commit comments

Comments
 (0)