Skip to content

Commit 0b02734

Browse files
authored
better handling of allOf in request/response (#17964)
1 parent a8efb8e commit 0b02734

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7107,6 +7107,19 @@ public List<CodegenParameter> fromRequestBodyToFormParameters(RequestBody body,
71077107
LOGGER.debug("debugging fromRequestBodyToFormParameters= {}", body);
71087108
Schema schema = ModelUtils.getSchemaFromRequestBody(body);
71097109
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
7110+
7111+
Schema original = null;
7112+
// check if it's allOf (only 1 sub schema) with or without default/nullable/etc set in the top level
7113+
if (ModelUtils.isAllOf(schema) && schema.getAllOf().size() == 1 &&
7114+
schema.getType() == null && schema.getTypes() == null) {
7115+
if (schema.getAllOf().get(0) instanceof Schema) {
7116+
original = schema;
7117+
schema = (Schema) schema.getAllOf().get(0);
7118+
} else {
7119+
LOGGER.error("Unknown type in allOf schema. Please report the issue via openapi-generator's Github issue tracker.");
7120+
}
7121+
}
7122+
71107123
if (ModelUtils.isMapSchema(schema)) {
71117124
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.");
71127125
}
@@ -7812,20 +7825,46 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
78127825

78137826
// restore original schema with description, extensions etc
78147827
if (original != null) {
7815-
schema = original;
78167828
// evaluate common attributes such as description if defined in the top level
7817-
if (schema.getDescription() != null) {
7818-
codegenParameter.description = escapeText(schema.getDescription());
7819-
codegenParameter.unescapedDescription = schema.getDescription();
7829+
if (original.getNullable() != null) {
7830+
codegenParameter.isNullable = original.getNullable();
7831+
} else if (original.getExtensions() != null && original.getExtensions().containsKey("x-nullable")) {
7832+
codegenParameter.isNullable = (Boolean) original.getExtensions().get("x-nullable");
78207833
}
78217834

78227835
if (original.getExtensions() != null) {
78237836
codegenParameter.vendorExtensions.putAll(original.getExtensions());
78247837
}
7825-
78267838
if (original.getDeprecated() != null) {
78277839
codegenParameter.isDeprecated = original.getDeprecated();
78287840
}
7841+
if (original.getDescription() != null) {
7842+
codegenParameter.description = escapeText(original.getDescription());
7843+
codegenParameter.unescapedDescription = original.getDescription();
7844+
}
7845+
if (original.getMaxLength() != null) {
7846+
codegenParameter.setMaxLength(original.getMaxLength());
7847+
}
7848+
if (original.getMinLength() != null) {
7849+
codegenParameter.setMinLength(original.getMinLength());
7850+
}
7851+
if (original.getMaxItems() != null) {
7852+
codegenParameter.setMaxItems(original.getMaxItems());
7853+
}
7854+
if (original.getMinItems() != null) {
7855+
codegenParameter.setMinItems(original.getMinItems());
7856+
}
7857+
if (original.getMaximum() != null) {
7858+
codegenParameter.setMaximum(String.valueOf(original.getMaximum().doubleValue()));
7859+
}
7860+
if (original.getMinimum() != null) {
7861+
codegenParameter.setMinimum(String.valueOf(original.getMinimum().doubleValue()));
7862+
}
7863+
/* comment out below as we don't store `title` in the codegen parametera the moment
7864+
if (original.getTitle() != null) {
7865+
codegenParameter.setTitle(original.getTitle());
7866+
}
7867+
*/
78297868
}
78307869

78317870
return codegenParameter;

modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ paths:
176176
$ref: '#/components/schemas/Pet'
177177
application/json:
178178
schema:
179-
$ref: '#/components/schemas/Pet'
179+
allOf:
180+
- $ref: '#/components/schemas/Pet'
180181
'400':
181182
description: Invalid ID supplied
182183
'404':
@@ -1362,7 +1363,8 @@ components:
13621363
content:
13631364
application/json:
13641365
schema:
1365-
$ref: '#/components/schemas/Pet'
1366+
allOf:
1367+
- $ref: '#/components/schemas/Pet'
13661368
application/xml:
13671369
schema:
13681370
$ref: '#/components/schemas/Pet'

samples/client/petstore/java/okhttp-gson/api/openapi.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ paths:
232232
$ref: '#/components/schemas/Pet'
233233
application/json:
234234
schema:
235-
$ref: '#/components/schemas/Pet'
235+
allOf:
236+
- $ref: '#/components/schemas/Pet'
236237
description: successful operation
237238
"400":
238239
description: Invalid ID supplied
@@ -1381,7 +1382,8 @@ components:
13811382
content:
13821383
application/json:
13831384
schema:
1384-
$ref: '#/components/schemas/Pet'
1385+
allOf:
1386+
- $ref: '#/components/schemas/Pet'
13851387
application/xml:
13861388
schema:
13871389
$ref: '#/components/schemas/Pet'

0 commit comments

Comments
 (0)