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

View File

@ -453,6 +453,16 @@ public class DefaultCodegenTest {
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
public void testCallbacks() {
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:
description: A representation of a child
allOf:
- $ref: '#/components/schemas/Person'
- type: object
properties:
age:
type: integer
format: int32
- $ref: '#/components/schemas/Person'