From 177deb918a62f9765b7e2ed8a493e72a77d82f5b Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 27 Dec 2018 16:01:18 +0800 Subject: [PATCH] better handling of allOf (composition) (#1757) --- .../openapitools/codegen/DefaultCodegen.java | 63 ++++++++----------- 1 file changed, 25 insertions(+), 38 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index ea6501e551..32a16ec7f6 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -1371,53 +1371,40 @@ public class DefaultCodegen implements CodegenConfig { ComposedSchema cs = (ComposedSchema) schema; List schemas = ModelUtils.getInterfaces(cs); if (cs.getAllOf() != null) { - for (Schema s : cs.getAllOf()) { - if (s != null) { - //schema = s; - } - //LOGGER.info("ALL OF SCHEMA: {}", s); + List names = new ArrayList<>(); + for (Schema s : schemas) { + names.add(getSingleSchemaType(s)); } - LOGGER.info("Composed schema not yet supported: {}", cs); - // get the model (allOf) - return getAlias("UNKNOWN_COMPOSED_SCHMEA"); - } else if (cs.getAnyOf() != null) { // anyOf - List names = new ArrayList(); - for (Schema s : schemas) { - if (StringUtils.isNotBlank(s.get$ref())) { // reference to another definition/schema - String schemaName = ModelUtils.getSimpleRef(s.get$ref()); - 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) + ">"; + if (names.size() == 0) { + LOGGER.error("allOf has no member defined: {}. Default to ERROR_ALLOF_SCHEMA", cs); + return "ERROR_ALLOF_SCHEMA"; + } else if (names.size() == 1) { + return names.get(0); + } else { + 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)); + return names.get(0); } - } else if (cs.getOneOf() != null) { // oneOf - List names = new ArrayList(); + } else if (cs.getAnyOf() != null) { // anyOf + List names = new ArrayList<>(); for (Schema s : schemas) { - if (StringUtils.isNotBlank(s.get$ref())) { // reference to another definition/schema - String schemaName = ModelUtils.getSimpleRef(s.get$ref()); - 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))); - } + names.add(getSingleSchemaType(s)); + } + return "anyOf<" + String.join(",", names) + ">"; + } else if (cs.getOneOf() != null) { // oneOf + List names = new ArrayList<>(); + for (Schema s : schemas) { + names.add(getSingleSchemaType(s)); } return "oneOf<" + String.join(",", names) + ">"; } } + return getSingleSchemaType(schema); + + } + + private String getSingleSchemaType(Schema schema) { Schema unaliasSchema = ModelUtils.unaliasSchema(globalSchemas, schema); if (StringUtils.isNotBlank(unaliasSchema.get$ref())) { // reference to another definition/schema