Skip to content

Commit fe6da71

Browse files
authored
[java] Fix annotationLibrary option being ignored (#21992)
* Only register the Swagger annotation imports for the corresponding annotationLibrary * Fix the issue in a specific generator * Add unit test
1 parent 2f69ad9 commit fe6da71

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,10 @@ public CodegenModel fromModel(String name, Schema model) {
10881088
}
10891089
}
10901090

1091+
if (!AnnotationLibrary.SWAGGER2.equals(getAnnotationLibrary())) {
1092+
codegenModel.imports.remove("Schema");
1093+
}
1094+
10911095
return codegenModel;
10921096
}
10931097

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3481,6 +3481,64 @@ public void callNativeServiceWithEmptyResponseSync() throws IOException {
34813481
);
34823482
}
34833483

3484+
@Test
3485+
public void annotationLibraryDoesNotCauseImportConflicts() throws IOException {
3486+
Map<String, Object> properties = new HashMap<>();
3487+
properties.put("annotationLibrary", "none");
3488+
3489+
File output = Files.createTempDirectory("test").toFile();
3490+
output.deleteOnExit();
3491+
3492+
final CodegenConfigurator configurator = new CodegenConfigurator()
3493+
.setGeneratorName(JAVA_GENERATOR)
3494+
.setLibrary(JavaClientCodegen.NATIVE)
3495+
.setAdditionalProperties(properties)
3496+
.setInputSpec("src/test/resources/3_0/java/native/issue21991.yaml")
3497+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
3498+
3499+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
3500+
DefaultGenerator generator = new DefaultGenerator();
3501+
3502+
Map<String, File> files = generator.opts(clientOptInput).generate().stream()
3503+
.collect(Collectors.toMap(File::getName, Function.identity()));
3504+
3505+
File apiFile = files.get("Schema.java");
3506+
assertNotNull(apiFile);
3507+
3508+
JavaFileAssert.assertThat(apiFile).fileDoesNotContain(
3509+
"import io.swagger.v3.oas.annotations.media.Schema;"
3510+
);
3511+
}
3512+
3513+
@Test
3514+
public void annotationLibraryGeneratesCorrectImports() throws IOException {
3515+
Map<String, Object> properties = new HashMap<>();
3516+
properties.put("annotationLibrary", "swagger2");
3517+
3518+
File output = Files.createTempDirectory("test").toFile();
3519+
output.deleteOnExit();
3520+
3521+
final CodegenConfigurator configurator = new CodegenConfigurator()
3522+
.setGeneratorName(JAVA_GENERATOR)
3523+
.setLibrary(JavaClientCodegen.NATIVE)
3524+
.setAdditionalProperties(properties)
3525+
.setInputSpec("src/test/resources/3_0/java/native/issue21991.yaml")
3526+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
3527+
3528+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
3529+
DefaultGenerator generator = new DefaultGenerator();
3530+
3531+
Map<String, File> files = generator.opts(clientOptInput).generate().stream()
3532+
.collect(Collectors.toMap(File::getName, Function.identity()));
3533+
3534+
File apiFile = files.get("Schema.java");
3535+
assertNotNull(apiFile);
3536+
3537+
JavaFileAssert.assertThat(apiFile).fileContains(
3538+
"import io.swagger.v3.oas.annotations.media.Schema;"
3539+
);
3540+
}
3541+
34843542

34853543
/**
34863544
* This checks that the async client is not affected by this fix.
@@ -3924,4 +3982,4 @@ public void oneOfWithInnerModelTest() {
39243982
}
39253983
assertTrue(speciesSeen);
39263984
}
3927-
}
3985+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Example Hello API
4+
description: ''
5+
version: v1
6+
servers:
7+
- url: http://localhost
8+
description: Global Endpoint
9+
paths:
10+
/v1/emptyResponse:
11+
get:
12+
operationId: empty
13+
description: returns an empty response
14+
responses:
15+
200:
16+
description: Successful operation
17+
content:
18+
application/json:
19+
schema:
20+
$ref: '#/components/schemas/Schema'
21+
204:
22+
description: Empty response
23+
components:
24+
schemas:
25+
Schema:
26+
type: object
27+
properties:
28+
name:
29+
type: string

0 commit comments

Comments
 (0)