better handling of allOf (composition) (#1757)

This commit is contained in:
William Cheng 2018-12-27 16:01:18 +08:00 committed by GitHub
parent 5d98fc6beb
commit 177deb918a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1371,53 +1371,40 @@ public class DefaultCodegen implements CodegenConfig {
ComposedSchema cs = (ComposedSchema) schema; ComposedSchema cs = (ComposedSchema) schema;
List<Schema> schemas = ModelUtils.getInterfaces(cs); List<Schema> schemas = ModelUtils.getInterfaces(cs);
if (cs.getAllOf() != null) { if (cs.getAllOf() != null) {
for (Schema s : cs.getAllOf()) { List<String> names = new ArrayList<>();
if (s != null) { for (Schema s : schemas) {
//schema = s; names.add(getSingleSchemaType(s));
}
//LOGGER.info("ALL OF SCHEMA: {}", s);
} }
LOGGER.info("Composed schema not yet supported: {}", cs); if (names.size() == 0) {
// get the model (allOf) LOGGER.error("allOf has no member defined: {}. Default to ERROR_ALLOF_SCHEMA", cs);
return getAlias("UNKNOWN_COMPOSED_SCHMEA"); return "ERROR_ALLOF_SCHEMA";
} else if (cs.getAnyOf() != null) { // anyOf } else if (names.size() == 1) {
List<String> names = new ArrayList<String>(); return names.get(0);
for (Schema s : schemas) { } else {
if (StringUtils.isNotBlank(s.get$ref())) { // reference to another definition/schema LOGGER.warn("allOf with multiple schemas defined. Using only the first one: {}. To fully utilize allOf, please use $ref instead of inline schema definition", names.get(0));
String schemaName = ModelUtils.getSimpleRef(s.get$ref()); return names.get(0);
if (StringUtils.isNotEmpty(schemaName)) {
names.add(getAlias(schemaName));
} else {
LOGGER.warn("Error obtaining the datatype from ref:" + schema.get$ref() + ". Default to 'object'");
return "object";
}
} else {
// primitive type or model
names.add(getAlias(getPrimitiveType(s)));
}
return "anyOf<" + String.join(",", names) + ">";
} }
} else if (cs.getOneOf() != null) { // oneOf } else if (cs.getAnyOf() != null) { // anyOf
List<String> names = new ArrayList<String>(); List<String> names = new ArrayList<>();
for (Schema s : schemas) { for (Schema s : schemas) {
if (StringUtils.isNotBlank(s.get$ref())) { // reference to another definition/schema names.add(getSingleSchemaType(s));
String schemaName = ModelUtils.getSimpleRef(s.get$ref()); }
if (StringUtils.isNotEmpty(schemaName)) { return "anyOf<" + String.join(",", names) + ">";
names.add(getAlias(schemaName)); } else if (cs.getOneOf() != null) { // oneOf
} else { List<String> names = new ArrayList<>();
LOGGER.warn("Error obtaining the datatype from ref:" + schema.get$ref() + ". Default to 'object'"); for (Schema s : schemas) {
return "object"; names.add(getSingleSchemaType(s));
}
} else {
// primitive type or model
names.add(getAlias(getPrimitiveType(s)));
}
} }
return "oneOf<" + String.join(",", names) + ">"; return "oneOf<" + String.join(",", names) + ">";
} }
} }
return getSingleSchemaType(schema);
}
private String getSingleSchemaType(Schema schema) {
Schema unaliasSchema = ModelUtils.unaliasSchema(globalSchemas, schema); Schema unaliasSchema = ModelUtils.unaliasSchema(globalSchemas, schema);
if (StringUtils.isNotBlank(unaliasSchema.get$ref())) { // reference to another definition/schema if (StringUtils.isNotBlank(unaliasSchema.get$ref())) { // reference to another definition/schema