Skip to content

Commit 35e6027

Browse files
bjansenkota65535
authored andcommitted
Fix ClassCastException in OpenAPINormalizer on composed schemas in 3.1 (OpenAPITools#17912)
1 parent 09409d8 commit 35e6027

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

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

+12-14
Original file line numberDiff line numberDiff line change
@@ -458,33 +458,31 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
458458
} else if (ModelUtils.isAllOf(schema)) { // allOf
459459
return normalizeAllOf(schema, visitedSchemas);
460460
} else if (ModelUtils.isComposedSchema(schema)) { // composed schema
461-
ComposedSchema cs = (ComposedSchema) schema;
462-
463-
if (ModelUtils.isComplexComposedSchema(cs)) {
464-
cs = (ComposedSchema) normalizeComplexComposedSchema(cs, visitedSchemas);
461+
if (ModelUtils.isComplexComposedSchema(schema)) {
462+
schema = normalizeComplexComposedSchema(schema, visitedSchemas);
465463
}
466464

467-
if (cs.getAllOf() != null && !cs.getAllOf().isEmpty()) {
468-
return normalizeAllOf(cs, visitedSchemas);
465+
if (schema.getAllOf() != null && !schema.getAllOf().isEmpty()) {
466+
return normalizeAllOf(schema, visitedSchemas);
469467
}
470468

471-
if (cs.getOneOf() != null && !cs.getOneOf().isEmpty()) {
472-
return normalizeOneOf(cs, visitedSchemas);
469+
if (schema.getOneOf() != null && !schema.getOneOf().isEmpty()) {
470+
return normalizeOneOf(schema, visitedSchemas);
473471
}
474472

475-
if (cs.getAnyOf() != null && !cs.getAnyOf().isEmpty()) {
476-
return normalizeAnyOf(cs, visitedSchemas);
473+
if (schema.getAnyOf() != null && !schema.getAnyOf().isEmpty()) {
474+
return normalizeAnyOf(schema, visitedSchemas);
477475
}
478476

479-
if (cs.getProperties() != null && !cs.getProperties().isEmpty()) {
480-
normalizeProperties(cs.getProperties(), visitedSchemas);
477+
if (schema.getProperties() != null && !schema.getProperties().isEmpty()) {
478+
normalizeProperties(schema.getProperties(), visitedSchemas);
481479
}
482480

483-
if (cs.getAdditionalProperties() != null) {
481+
if (schema.getAdditionalProperties() != null) {
484482
// normalizeAdditionalProperties(m);
485483
}
486484

487-
return cs;
485+
return schema;
488486
} else if (schema.getProperties() != null && !schema.getProperties().isEmpty()) {
489487
normalizeProperties(schema.getProperties(), visitedSchemas);
490488
} else if (schema instanceof BooleanSchema) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,12 @@ public void testFilter() {
467467
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getExtensions().get("x-internal"), false);
468468
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getPut().getExtensions().get("x-internal"), true);
469469
}
470-
}
470+
471+
@Test
472+
public void testComposedSchemaDoesNotThrow() {
473+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/composed-schema.yaml");
474+
475+
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, Collections.emptyMap());
476+
openAPINormalizer.normalize();
477+
}
478+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
openapi: 3.1.0
2+
info:
3+
version: 1.0.0
4+
title: Example
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://api.example.xyz/v1
9+
paths:
10+
/payment:
11+
post:
12+
operationId: payment
13+
requestBody:
14+
content:
15+
application/json:
16+
schema:
17+
$ref: "#/components/schemas/Payment"
18+
19+
components:
20+
schemas:
21+
Payment:
22+
type: object
23+
properties:
24+
label:
25+
type: string
26+
otherLabel:
27+
type: string
28+
amount:
29+
type: number
30+
oneOf:
31+
- required:
32+
- label
33+
- amount
34+
- required:
35+
- otherLabel
36+
- amount

0 commit comments

Comments
 (0)