forked from loafle/openapi-generator-original
[codegen] Add code comments and improve "first content" warning (#5184)
* Add code comments and improve warning
This commit is contained in:
parent
c8cd255ad3
commit
6f1ce5645e
@ -1593,9 +1593,13 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
public String getSchemaType(Schema schema) {
|
public String getSchemaType(Schema schema) {
|
||||||
if (schema instanceof ComposedSchema) { // composed schema
|
if (schema instanceof ComposedSchema) { // composed schema
|
||||||
ComposedSchema cs = (ComposedSchema) schema;
|
ComposedSchema cs = (ComposedSchema) schema;
|
||||||
|
// Get the interfaces, i.e. the set of elements under 'allOf', 'anyOf' or 'oneOf'.
|
||||||
List<Schema> schemas = ModelUtils.getInterfaces(cs);
|
List<Schema> schemas = ModelUtils.getInterfaces(cs);
|
||||||
|
|
||||||
List<String> names = new ArrayList<>();
|
List<String> 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) {
|
for (Schema s : schemas) {
|
||||||
names.add(getSingleSchemaType(s));
|
names.add(getSingleSchemaType(s));
|
||||||
}
|
}
|
||||||
@ -1637,7 +1641,8 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
} else if (names.size() == 1) {
|
} else if (names.size() == 1) {
|
||||||
return names.get(0);
|
return names.get(0);
|
||||||
} else {
|
} 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);
|
return names.get(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1670,6 +1675,12 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
return "oneOf<" + String.join(",", names) + ">";
|
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) {
|
private String getSingleSchemaType(Schema schema) {
|
||||||
Schema unaliasSchema = ModelUtils.unaliasSchema(this.openAPI, schema);
|
Schema unaliasSchema = ModelUtils.unaliasSchema(this.openAPI, schema);
|
||||||
|
|
||||||
|
@ -791,15 +791,36 @@ public class ModelUtils {
|
|||||||
return getSchemaFromContent(response.getContent());
|
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) {
|
private static Schema getSchemaFromContent(Content content) {
|
||||||
if (content == null || content.isEmpty()) {
|
if (content == null || content.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
Map.Entry<String, MediaType> entry = content.entrySet().iterator().next();
|
||||||
if (content.size() > 1) {
|
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 entry.getValue().getSchema();
|
||||||
return mediaType.getSchema();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user