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 4ebed00b41b..6d704fe9509 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 @@ -1593,9 +1593,13 @@ public class DefaultCodegen implements CodegenConfig { public String getSchemaType(Schema schema) { if (schema instanceof ComposedSchema) { // composed schema ComposedSchema cs = (ComposedSchema) schema; + // Get the interfaces, i.e. the set of elements under 'allOf', 'anyOf' or 'oneOf'. List schemas = ModelUtils.getInterfaces(cs); List names = new ArrayList<>(); + // Build a list of the schema types under each interface. + // For example, if a 'allOf' composed schema has $ref children, + // add the type of each child to the list of names. for (Schema s : schemas) { names.add(getSingleSchemaType(s)); } @@ -1637,7 +1641,8 @@ public class DefaultCodegen implements CodegenConfig { } 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)); + 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); } } @@ -1670,6 +1675,12 @@ public class DefaultCodegen implements CodegenConfig { return "oneOf<" + String.join(",", names) + ">"; } + /** + * Return a string representation of the schema type, resolving aliasing and references if necessary. + * + * @param schema + * @return the string representation of the schema type. + */ private String getSingleSchemaType(Schema schema) { Schema unaliasSchema = ModelUtils.unaliasSchema(this.openAPI, schema); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index a539cddb1fd..3872b11d872 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -791,15 +791,36 @@ public class ModelUtils { return getSchemaFromContent(response.getContent()); } + /** + * Return the first Schema from a specified OAS 'content' section. + * + * For example, given the following OAS, this method returns the schema + * for the 'application/json' content type because it is listed first in the OAS. + * + * responses: + * '200': + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/XYZ' + * application/xml: + * ... + * + * @param content a 'content' section in the OAS specification. + * @return the Schema. + */ private static Schema getSchemaFromContent(Content content) { if (content == null || content.isEmpty()) { return null; } + Map.Entry entry = content.entrySet().iterator().next(); if (content.size() > 1) { - LOGGER.warn("Multiple schemas found in content, returning only the first one"); + // Other content types are currently ignored by codegen. If you see this warning, + // reorder the OAS spec to put the desired content type first. + LOGGER.warn("Multiple schemas found in the OAS 'content' section, returning only the first one ({})", + entry.getKey()); } - MediaType mediaType = content.values().iterator().next(); - return mediaType.getSchema(); + return entry.getValue().getSchema(); } /**