better handling of allOf in request/response (#17964)

This commit is contained in:
William Cheng 2024-02-28 11:19:11 +08:00 committed by GitHub
parent a8efb8eea8
commit 0b02734b93
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 9 deletions

View File

@ -7107,6 +7107,19 @@ public class DefaultCodegen implements CodegenConfig {
LOGGER.debug("debugging fromRequestBodyToFormParameters= {}", body);
Schema schema = ModelUtils.getSchemaFromRequestBody(body);
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
Schema original = null;
// check if it's allOf (only 1 sub schema) with or without default/nullable/etc set in the top level
if (ModelUtils.isAllOf(schema) && schema.getAllOf().size() == 1 &&
schema.getType() == null && schema.getTypes() == null) {
if (schema.getAllOf().get(0) instanceof Schema) {
original = schema;
schema = (Schema) schema.getAllOf().get(0);
} else {
LOGGER.error("Unknown type in allOf schema. Please report the issue via openapi-generator's Github issue tracker.");
}
}
if (ModelUtils.isMapSchema(schema)) {
LOGGER.error("Form parameters with additionalProperties are not supported by OpenAPI Generator. Please report the issue to https://github.com/openapitools/openapi-generator if you need help.");
}
@ -7812,20 +7825,46 @@ public class DefaultCodegen implements CodegenConfig {
// restore original schema with description, extensions etc
if (original != null) {
schema = original;
// evaluate common attributes such as description if defined in the top level
if (schema.getDescription() != null) {
codegenParameter.description = escapeText(schema.getDescription());
codegenParameter.unescapedDescription = schema.getDescription();
if (original.getNullable() != null) {
codegenParameter.isNullable = original.getNullable();
} else if (original.getExtensions() != null && original.getExtensions().containsKey("x-nullable")) {
codegenParameter.isNullable = (Boolean) original.getExtensions().get("x-nullable");
}
if (original.getExtensions() != null) {
codegenParameter.vendorExtensions.putAll(original.getExtensions());
}
if (original.getDeprecated() != null) {
codegenParameter.isDeprecated = original.getDeprecated();
}
if (original.getDescription() != null) {
codegenParameter.description = escapeText(original.getDescription());
codegenParameter.unescapedDescription = original.getDescription();
}
if (original.getMaxLength() != null) {
codegenParameter.setMaxLength(original.getMaxLength());
}
if (original.getMinLength() != null) {
codegenParameter.setMinLength(original.getMinLength());
}
if (original.getMaxItems() != null) {
codegenParameter.setMaxItems(original.getMaxItems());
}
if (original.getMinItems() != null) {
codegenParameter.setMinItems(original.getMinItems());
}
if (original.getMaximum() != null) {
codegenParameter.setMaximum(String.valueOf(original.getMaximum().doubleValue()));
}
if (original.getMinimum() != null) {
codegenParameter.setMinimum(String.valueOf(original.getMinimum().doubleValue()));
}
/* comment out below as we don't store `title` in the codegen parametera the moment
if (original.getTitle() != null) {
codegenParameter.setTitle(original.getTitle());
}
*/
}
return codegenParameter;

View File

@ -176,7 +176,8 @@ paths:
$ref: '#/components/schemas/Pet'
application/json:
schema:
$ref: '#/components/schemas/Pet'
allOf:
- $ref: '#/components/schemas/Pet'
'400':
description: Invalid ID supplied
'404':
@ -1362,7 +1363,8 @@ components:
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
allOf:
- $ref: '#/components/schemas/Pet'
application/xml:
schema:
$ref: '#/components/schemas/Pet'

View File

@ -232,7 +232,8 @@ paths:
$ref: '#/components/schemas/Pet'
application/json:
schema:
$ref: '#/components/schemas/Pet'
allOf:
- $ref: '#/components/schemas/Pet'
description: successful operation
"400":
description: Invalid ID supplied
@ -1381,7 +1382,8 @@ components:
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
allOf:
- $ref: '#/components/schemas/Pet'
application/xml:
schema:
$ref: '#/components/schemas/Pet'