[Java/Core] Adds properties to ComposedSchema models when they exist (#4482)

* Adds comment in java test where we need to check the fruit model

* Adds oneOf test to DefaultCodegenTest.java, adds code to include properties in composed schema
This commit is contained in:
Justin Black 2019-12-03 18:43:24 -08:00 committed by Jim Schubert
parent e774db05b6
commit c882338ef1
3 changed files with 49 additions and 0 deletions

View File

@ -1788,6 +1788,17 @@ public class DefaultCodegen implements CodegenConfig {
Map<String, Schema> allProperties = new LinkedHashMap<String, Schema>(); Map<String, Schema> allProperties = new LinkedHashMap<String, Schema>();
List<String> allRequired = new ArrayList<String>(); List<String> allRequired = new ArrayList<String>();
// if schema has properties outside of allOf/oneOf/anyOf also add them to m
if (schema.getProperties() != null) {
addVars(m, unaliasPropertySchema(schema.getProperties()), schema.getRequired(), null, null);
}
// uncomment this when https://github.com/swagger-api/swagger-parser/issues/1252 is resolved
// if schema has additionalproperties outside of allOf/oneOf/anyOf also add it to m
// if (schema.getAdditionalProperties() != null) {
// addAdditionPropertiesToCodeGenModel(m, schema);
// }
// parent model // parent model
final String parentName = ModelUtils.getParentName(composed, allDefinitions); final String parentName = ModelUtils.getParentName(composed, allDefinitions);
final List<String> allParents = ModelUtils.getAllParentsName(composed, allDefinitions); final List<String> allParents = ModelUtils.getAllParentsName(composed, allDefinitions);

View File

@ -268,6 +268,41 @@ public class DefaultCodegenTest {
Assert.assertEquals(type, "oneOf<ObjA,ObjB>"); Assert.assertEquals(type, "oneOf<ObjA,ObjB>");
} }
@Test
public void testComposedSchemaOneOfWithProperties() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/oneOf.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
final Schema schema = openAPI.getComponents().getSchemas().get("fruit");
codegen.setOpenAPI(openAPI);
CodegenModel fruit = codegen.fromModel("Fruit", schema);
Set<String> oneOf = new TreeSet<String>();
oneOf.add("Apple");
oneOf.add("Banana");
Assert.assertEquals(fruit.oneOf, oneOf);
Assert.assertEquals(fruit.optionalVars.size(), 3);
Assert.assertEquals(fruit.vars.size(), 3);
// make sure that fruit has the property color
boolean colorSeen = false;
for (CodegenProperty cp : fruit.vars) {
if (cp.name.equals("color")) {
colorSeen = true;
break;
}
}
Assert.assertTrue(colorSeen);
colorSeen = false;
for (CodegenProperty cp : fruit.optionalVars) {
if (cp.name.equals("color")) {
colorSeen = true;
break;
}
}
Assert.assertTrue(colorSeen);
}
@Test @Test
public void testEscapeText() { public void testEscapeText() {
final DefaultCodegen codegen = new DefaultCodegen(); final DefaultCodegen codegen = new DefaultCodegen();

View File

@ -22,6 +22,9 @@ components:
oneOf: oneOf:
- $ref: '#/components/schemas/apple' - $ref: '#/components/schemas/apple'
- $ref: '#/components/schemas/banana' - $ref: '#/components/schemas/banana'
# additionalProperties:
# type: string
# uncomment this when https://github.com/swagger-api/swagger-parser/issues/1252 is resolved
apple: apple:
title: apple title: apple
type: object type: object