Improve "allOf" support (#1169)

* Add test case which reproduces issue340

* Take the first $ref we find in the allOf-List
This commit is contained in:
Akihito Nakano 2018-10-06 00:28:35 +09:00 committed by William Cheng
parent aa31e42ce7
commit a1d242595e
3 changed files with 17 additions and 7 deletions

View File

@ -4215,13 +4215,13 @@ public class DefaultCodegen implements CodegenConfig {
protected String getParentName(ComposedSchema composedSchema, Map<String, Schema> allSchemas) { protected String getParentName(ComposedSchema composedSchema, Map<String, Schema> allSchemas) {
if (composedSchema.getAllOf() != null && !composedSchema.getAllOf().isEmpty()) { if (composedSchema.getAllOf() != null && !composedSchema.getAllOf().isEmpty()) {
Schema schema = composedSchema.getAllOf().get(0); for (Schema schema : composedSchema.getAllOf()) {
String ref = schema.get$ref(); String ref = schema.get$ref();
if (StringUtils.isBlank(ref)) { if (!StringUtils.isBlank(ref)) {
return null;
}
return ModelUtils.getSimpleRef(ref); return ModelUtils.getSimpleRef(ref);
} }
}
}
return null; return null;
} }

View File

@ -453,6 +453,16 @@ public class DefaultCodegenTest {
verifyPersonDiscriminator(personModel.discriminator); verifyPersonDiscriminator(personModel.discriminator);
} }
@Test
public void testParentName() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/allOf.yaml", null, new ParseOptions()).getOpenAPI();
DefaultCodegen codegen = new DefaultCodegen();
Schema child = openAPI.getComponents().getSchemas().get("Child");
CodegenModel childModel = codegen.fromModel("Child", child, openAPI.getComponents().getSchemas());
Assert.assertEquals(childModel.parentSchema, "Person");
}
@Test @Test
public void testCallbacks() { public void testCallbacks() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/callbacks.yaml", null, new ParseOptions()).getOpenAPI(); final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/callbacks.yaml", null, new ParseOptions()).getOpenAPI();

View File

@ -53,9 +53,9 @@ components:
Child: Child:
description: A representation of a child description: A representation of a child
allOf: allOf:
- $ref: '#/components/schemas/Person'
- type: object - type: object
properties: properties:
age: age:
type: integer type: integer
format: int32 format: int32
- $ref: '#/components/schemas/Person'