Fix ClassCastException in OpenAPINormalizer on composed schemas in 3.1 (#17912)

This commit is contained in:
Bastien Jansen
2024-02-21 08:22:46 +01:00
committed by GitHub
parent 6c86c2d3cf
commit 3af7363e76
3 changed files with 57 additions and 15 deletions

View File

@@ -458,33 +458,31 @@ public class OpenAPINormalizer {
} 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) {

View File

@@ -467,4 +467,12 @@ public class OpenAPINormalizerTest {
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();
}
}

View File

@@ -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