From 0b02734b933eee1d107e70aa8ede2bd00855aa3f Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 28 Feb 2024 11:19:11 +0800 Subject: [PATCH] better handling of allOf in request/response (#17964) --- .../openapitools/codegen/DefaultCodegen.java | 49 +++++++++++++++++-- ...points-models-for-testing-okhttp-gson.yaml | 6 ++- .../java/okhttp-gson/api/openapi.yaml | 6 ++- 3 files changed, 52 insertions(+), 9 deletions(-) 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 875ddfc85be..e63277d05bc 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 @@ -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; diff --git a/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml b/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml index fbec8830165..7778d762ffd 100644 --- a/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml @@ -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' diff --git a/samples/client/petstore/java/okhttp-gson/api/openapi.yaml b/samples/client/petstore/java/okhttp-gson/api/openapi.yaml index f94b20ca861..1d81c5e407d 100644 --- a/samples/client/petstore/java/okhttp-gson/api/openapi.yaml +++ b/samples/client/petstore/java/okhttp-gson/api/openapi.yaml @@ -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'