Skip to content

Commit

Permalink
Fix ClassCastException in OpenAPINormalizer on composed schemas i…
Browse files Browse the repository at this point in the history
  • Loading branch information
bjansen authored and oscarr-reyes committed Feb 25, 2024
1 parent 9423530 commit d13b823
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,33 +458,31 @@ public Schema normalizeSchema(Schema schema, Set<Schema> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

@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();
}
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d13b823

Please sign in to comment.