Skip to content

Commit fb277f8

Browse files
authored
[java] [spring] Fix annotationLibrary option being ignored in spring generator (#22046)
* Fix annotationLibrary option being ignored in spring generator * Generate sample code * Sample generator weirdness * Revert "Sample generator weirdness"
1 parent 6b1b5cc commit fb277f8

File tree

26 files changed

+5870
-0
lines changed

26 files changed

+5870
-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
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
# Category
4+
5+
A category for a pet
6+
7+
## Properties
8+
9+
| Name | Type | Description | Notes |
10+
|------------ | ------------- | ------------- | -------------|
11+
|**id** | **Long** | | [optional] |
12+
|**name** | **String** | | [optional] |
13+
14+
15+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
# ModelApiResponse
4+
5+
Describes the result of uploading an image resource
6+
7+
## Properties
8+
9+
| Name | Type | Description | Notes |
10+
|------------ | ------------- | ------------- | -------------|
11+
|**code** | **Integer** | | [optional] |
12+
|**type** | **String** | | [optional] |
13+
|**message** | **String** | | [optional] |
14+
15+
16+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
# Order
4+
5+
An order for a pets from the pet store
6+
7+
## Properties
8+
9+
| Name | Type | Description | Notes |
10+
|------------ | ------------- | ------------- | -------------|
11+
|**id** | **Long** | | [optional] |
12+
|**petId** | **Long** | | [optional] |
13+
|**quantity** | **Integer** | | [optional] |
14+
|**shipDate** | **OffsetDateTime** | | [optional] |
15+
|**status** | [**StatusEnum**](#StatusEnum) | Order Status | [optional] |
16+
|**complete** | **Boolean** | | [optional] |
17+
18+
19+
20+
## Enum: StatusEnum
21+
22+
| Name | Value |
23+
|---- | -----|
24+
| PLACED | &quot;placed&quot; |
25+
| APPROVED | &quot;approved&quot; |
26+
| DELIVERED | &quot;delivered&quot; |
27+
28+
29+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
# Pet
4+
5+
A pet for sale in the pet store
6+
7+
## Properties
8+
9+
| Name | Type | Description | Notes |
10+
|------------ | ------------- | ------------- | -------------|
11+
|**id** | **Long** | | [optional] |
12+
|**category** | [**Category**](Category.md) | | [optional] |
13+
|**name** | **String** | | |
14+
|**photoUrls** | **List&lt;String&gt;** | | |
15+
|**tags** | [**List&lt;Tag&gt;**](Tag.md) | | [optional] |
16+
|**status** | [**StatusEnum**](#StatusEnum) | pet status in the store | [optional] |
17+
18+
19+
20+
## Enum: StatusEnum
21+
22+
| Name | Value |
23+
|---- | -----|
24+
| AVAILABLE | &quot;available&quot; |
25+
| PENDING | &quot;pending&quot; |
26+
| SOLD | &quot;sold&quot; |
27+
28+
29+

0 commit comments

Comments
 (0)