mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-10-13 16:03:43 +00:00
[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
This commit is contained in:
parent
2f69ad9f26
commit
fe6da71327
@ -1088,6 +1088,10 @@ public class JavaClientCodegen extends AbstractJavaCodegen
|
||||
}
|
||||
}
|
||||
|
||||
if (!AnnotationLibrary.SWAGGER2.equals(getAnnotationLibrary())) {
|
||||
codegenModel.imports.remove("Schema");
|
||||
}
|
||||
|
||||
return codegenModel;
|
||||
}
|
||||
|
||||
|
@ -3481,6 +3481,64 @@ public class JavaClientCodegenTest {
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void annotationLibraryDoesNotCauseImportConflicts() throws IOException {
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
properties.put("annotationLibrary", "none");
|
||||
|
||||
File output = Files.createTempDirectory("test").toFile();
|
||||
output.deleteOnExit();
|
||||
|
||||
final CodegenConfigurator configurator = new CodegenConfigurator()
|
||||
.setGeneratorName(JAVA_GENERATOR)
|
||||
.setLibrary(JavaClientCodegen.NATIVE)
|
||||
.setAdditionalProperties(properties)
|
||||
.setInputSpec("src/test/resources/3_0/java/native/issue21991.yaml")
|
||||
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
|
||||
|
||||
final ClientOptInput clientOptInput = configurator.toClientOptInput();
|
||||
DefaultGenerator generator = new DefaultGenerator();
|
||||
|
||||
Map<String, File> files = generator.opts(clientOptInput).generate().stream()
|
||||
.collect(Collectors.toMap(File::getName, Function.identity()));
|
||||
|
||||
File apiFile = files.get("Schema.java");
|
||||
assertNotNull(apiFile);
|
||||
|
||||
JavaFileAssert.assertThat(apiFile).fileDoesNotContain(
|
||||
"import io.swagger.v3.oas.annotations.media.Schema;"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void annotationLibraryGeneratesCorrectImports() throws IOException {
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
properties.put("annotationLibrary", "swagger2");
|
||||
|
||||
File output = Files.createTempDirectory("test").toFile();
|
||||
output.deleteOnExit();
|
||||
|
||||
final CodegenConfigurator configurator = new CodegenConfigurator()
|
||||
.setGeneratorName(JAVA_GENERATOR)
|
||||
.setLibrary(JavaClientCodegen.NATIVE)
|
||||
.setAdditionalProperties(properties)
|
||||
.setInputSpec("src/test/resources/3_0/java/native/issue21991.yaml")
|
||||
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
|
||||
|
||||
final ClientOptInput clientOptInput = configurator.toClientOptInput();
|
||||
DefaultGenerator generator = new DefaultGenerator();
|
||||
|
||||
Map<String, File> files = generator.opts(clientOptInput).generate().stream()
|
||||
.collect(Collectors.toMap(File::getName, Function.identity()));
|
||||
|
||||
File apiFile = files.get("Schema.java");
|
||||
assertNotNull(apiFile);
|
||||
|
||||
JavaFileAssert.assertThat(apiFile).fileContains(
|
||||
"import io.swagger.v3.oas.annotations.media.Schema;"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This checks that the async client is not affected by this fix.
|
||||
@ -3924,4 +3982,4 @@ public class JavaClientCodegenTest {
|
||||
}
|
||||
assertTrue(speciesSeen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,29 @@
|
||||
openapi: 3.0.3
|
||||
info:
|
||||
title: Example Hello API
|
||||
description: ''
|
||||
version: v1
|
||||
servers:
|
||||
- url: http://localhost
|
||||
description: Global Endpoint
|
||||
paths:
|
||||
/v1/emptyResponse:
|
||||
get:
|
||||
operationId: empty
|
||||
description: returns an empty response
|
||||
responses:
|
||||
200:
|
||||
description: Successful operation
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Schema'
|
||||
204:
|
||||
description: Empty response
|
||||
components:
|
||||
schemas:
|
||||
Schema:
|
||||
type: object
|
||||
properties:
|
||||
name:
|
||||
type: string
|
Loading…
x
Reference in New Issue
Block a user