From d13b8231c52f8015ee040254779331953bf0c391 Mon Sep 17 00:00:00 2001 From: Bastien Jansen Date: Wed, 21 Feb 2024 08:22:46 +0100 Subject: [PATCH] Fix `ClassCastException` in `OpenAPINormalizer` on composed schemas in 3.1 (#17912) --- .../codegen/OpenAPINormalizer.java | 26 +++++++------- .../codegen/OpenAPINormalizerTest.java | 10 +++++- .../test/resources/3_1/composed-schema.yaml | 36 +++++++++++++++++++ 3 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/3_1/composed-schema.yaml diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java index ffc5235ea102..2eed307c7beb 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java @@ -458,33 +458,31 @@ public Schema normalizeSchema(Schema schema, Set visitedSchemas) { } else if (ModelUtils.isAllOf(schema)) { // allOf return normalizeAllOf(schema, visitedSchemas); } else if (ModelUtils.isComposedSchema(schema)) { // composed schema - ComposedSchema cs = (ComposedSchema) schema; - - if (ModelUtils.isComplexComposedSchema(cs)) { - cs = (ComposedSchema) normalizeComplexComposedSchema(cs, visitedSchemas); + if (ModelUtils.isComplexComposedSchema(schema)) { + schema = normalizeComplexComposedSchema(schema, visitedSchemas); } - if (cs.getAllOf() != null && !cs.getAllOf().isEmpty()) { - return normalizeAllOf(cs, visitedSchemas); + if (schema.getAllOf() != null && !schema.getAllOf().isEmpty()) { + return normalizeAllOf(schema, visitedSchemas); } - if (cs.getOneOf() != null && !cs.getOneOf().isEmpty()) { - return normalizeOneOf(cs, visitedSchemas); + if (schema.getOneOf() != null && !schema.getOneOf().isEmpty()) { + return normalizeOneOf(schema, visitedSchemas); } - if (cs.getAnyOf() != null && !cs.getAnyOf().isEmpty()) { - return normalizeAnyOf(cs, visitedSchemas); + if (schema.getAnyOf() != null && !schema.getAnyOf().isEmpty()) { + return normalizeAnyOf(schema, visitedSchemas); } - if (cs.getProperties() != null && !cs.getProperties().isEmpty()) { - normalizeProperties(cs.getProperties(), visitedSchemas); + if (schema.getProperties() != null && !schema.getProperties().isEmpty()) { + normalizeProperties(schema.getProperties(), visitedSchemas); } - if (cs.getAdditionalProperties() != null) { + if (schema.getAdditionalProperties() != null) { // normalizeAdditionalProperties(m); } - return cs; + return schema; } else if (schema.getProperties() != null && !schema.getProperties().isEmpty()) { normalizeProperties(schema.getProperties(), visitedSchemas); } else if (schema instanceof BooleanSchema) { diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java index 2694910817a8..05253ca2f194 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java @@ -467,4 +467,12 @@ public void testFilter() { assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getExtensions().get("x-internal"), false); assertEquals(openAPI.getPaths().get("/person/display/{personId}").getPut().getExtensions().get("x-internal"), true); } -} \ No newline at end of file + + @Test + public void testComposedSchemaDoesNotThrow() { + OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/composed-schema.yaml"); + + OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, Collections.emptyMap()); + openAPINormalizer.normalize(); + } +} diff --git a/modules/openapi-generator/src/test/resources/3_1/composed-schema.yaml b/modules/openapi-generator/src/test/resources/3_1/composed-schema.yaml new file mode 100644 index 000000000000..5b89638e1e37 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_1/composed-schema.yaml @@ -0,0 +1,36 @@ +openapi: 3.1.0 +info: + version: 1.0.0 + title: Example + license: + name: MIT +servers: + - url: http://api.example.xyz/v1 +paths: + /payment: + post: + operationId: payment + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/Payment" + +components: + schemas: + Payment: + type: object + properties: + label: + type: string + otherLabel: + type: string + amount: + type: number + oneOf: + - required: + - label + - amount + - required: + - otherLabel + - amount