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) {
|
||||
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<Schema> schemas = ModelUtils.getInterfaces(cs);
|
||||
|
||||
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) {
|
||||
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);
|
||||
|
||||
|
@ -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<String, MediaType> 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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user