Skip to content

Commit

Permalink
[Java/Core] Adds properties to ComposedSchema models when they exist (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
spacether authored and jimschubert committed Dec 4, 2019
1 parent e774db0 commit c882338
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,17 @@ public CodegenModel fromModel(String name, Schema schema) {
Map<String, Schema> allProperties = new LinkedHashMap<String, Schema>();
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
final String parentName = ModelUtils.getParentName(composed, allDefinitions);
final List<String> allParents = ModelUtils.getAllParentsName(composed, allDefinitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,41 @@ public void testGetSchemaTypeWithComposedSchemaWithOneOf() {
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
public void testEscapeText() {
final DefaultCodegen codegen = new DefaultCodegen();
Expand Down
3 changes: 3 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/oneOf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ components:
oneOf:
- $ref: '#/components/schemas/apple'
- $ref: '#/components/schemas/banana'
# additionalProperties:
# type: string
# uncomment this when https://github.com/swagger-api/swagger-parser/issues/1252 is resolved
apple:
title: apple
type: object
Expand Down

0 comments on commit c882338

Please sign in to comment.