add Imports for oneOf & anyOf schemas (#13553)

This commit is contained in:
bgong-mdsol
2022-10-06 13:01:58 -04:00
committed by GitHub
parent a37d18a7e4
commit 4ae11f8a0f
3 changed files with 55 additions and 0 deletions

View File

@@ -1297,9 +1297,21 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
if ("BigDecimal".equals(codegenModel.dataType)) {
codegenModel.imports.add("BigDecimal");
}
// additional import for different cases
addAdditionalImports(codegenModel, codegenModel.oneOf);
addAdditionalImports(codegenModel, codegenModel.anyOf);
return codegenModel;
}
private void addAdditionalImports(CodegenModel model, Set<String> dataTypeSet) {
for (String dataType : dataTypeSet) {
if (null != importMapping().get(dataType)) {
model.imports.add(dataType);
}
}
}
@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
if (model.getIsClassnameSanitized() && additionalProperties.containsKey(JACKSON)) {

View File

@@ -855,6 +855,20 @@ public class AbstractJavaCodegenTest {
Assert.assertEquals(fakeJavaCodegen.getTestFolder(), "src/test/java");
}
@Test
public void testOneOfModelImports() throws Exception {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/oneOf_nonPrimitive.yaml");
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
codegen.preprocessOpenAPI(openAPI);
Schema<?> schema = openAPI.getComponents().getSchemas().get("Example");
CodegenModel cm = codegen.fromModel("Example", schema);
Assert.assertEquals(cm.imports.size(), 3);
Assert.assertTrue(cm.imports.contains("BigDecimal"));
Assert.assertTrue(cm.imports.contains("Date"));
Assert.assertTrue(cm.imports.contains("UUID"));
}
private static Schema<?> createObjectSchemaWithMinItems() {
return new ObjectSchema()
.addProperties("id", new IntegerSchema().format("int32"))

View File

@@ -0,0 +1,29 @@
openapi: 3.0.1
info:
version: 1.0.0
title: Example - oneOf data type
license:
name: MIT
servers:
- url: http://api.example.xyz/v1
paths:
/example:
get:
operationId: list
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/Example"
components:
schemas:
Example:
oneOf:
- type: string
format: uuid
- type: string
format: date-time
- type: integer
- type: number