Skip to content

Commit

Permalink
Merge pull request #4637 from GooDer/openApi-3.1-ComposedSchema-fix
Browse files Browse the repository at this point in the history
Fix #4634: unite processing of allOf, anyOf, oneOf relation so it wil…
  • Loading branch information
micryc committed May 13, 2024
2 parents 616350b + 49d9684 commit 8660f35
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io.swagger.v3.oas.models.callbacks.Callback;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
Expand Down Expand Up @@ -310,20 +309,23 @@ private void addSchemaRef(Schema schema, Set<String> referencedDefinitions) {
addSchemaRef(((ArraySchema) schema).getItems(), referencedDefinitions);
} else if (schema.getTypes() != null && schema.getTypes().contains("array") && schema.getItems() != null) {
addSchemaRef(schema.getItems(), referencedDefinitions);
} else if (schema instanceof ComposedSchema) {
ComposedSchema composedSchema = (ComposedSchema) schema;
if (composedSchema.getAllOf() != null) {
for (Schema ref : composedSchema.getAllOf()) {
} else {
List<Schema> allOf = schema.getAllOf();
List<Schema> anyOf = schema.getAnyOf();
List<Schema> oneOf = schema.getOneOf();

if (allOf != null) {
for (Schema ref : allOf) {
addSchemaRef(ref, referencedDefinitions);
}
}
if (composedSchema.getAnyOf() != null) {
for (Schema ref : composedSchema.getAnyOf()) {
if (anyOf != null) {
for (Schema ref : anyOf) {
addSchemaRef(ref, referencedDefinitions);
}
}
if (composedSchema.getOneOf() != null) {
for (Schema ref : composedSchema.getOneOf()) {
if (oneOf != null) {
for (Schema ref : oneOf) {
addSchemaRef(ref, referencedDefinitions);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class SpecFilterTest {
private static final String RESOURCE_PATH = "specFiles/petstore-3.0-v2.json";
private static final String RESOURCE_PATH_3303 = "specFiles/petstore-3.0-v2-ticket-3303.json";
private static final String RESOURCE_PATH_LIST = "specFiles/3.1.0/list-3.1.json";
private static final String RESOURCE_PATH_COMPOSED_SCHEMA = "specFiles/3.1.0/composed-schema-3.1.json";
private static final String RESOURCE_REFERRED_SCHEMAS = "specFiles/petstore-3.0-referred-schemas.json";
private static final String RESOURCE_PATH_WITHOUT_MODELS = "specFiles/petstore-3.0-v2_withoutModels.json";
private static final String RESOURCE_DEPRECATED_OPERATIONS = "specFiles/deprecatedoperationmodel.json";
Expand Down Expand Up @@ -286,6 +287,18 @@ public void shouldRemoveBrokenNestedRefsKeepArray() throws IOException {
assertTrue(filtered.getComponents().getSchemas().containsKey("SomeChildObject"), "Schemas should contains child list");
}

@Test
public void shouldRemoveBrokenNestedRefsKeepComposedSchemas() throws IOException {
final OpenAPI openAPI = getOpenAPI31(RESOURCE_PATH_COMPOSED_SCHEMA);
final RemoveUnreferencedDefinitionsFilter remover = new RemoveUnreferencedDefinitionsFilter();
final OpenAPI filtered = new SpecFilter().filter(openAPI, remover, null, null, null);

assertEquals(filtered.getComponents().getSchemas().size(), 4, "Expected to have parent and abstract child with both implementations schemas");
assertTrue(filtered.getComponents().getSchemas().containsKey("SomeChild1ImplObject"), "Schemas should contains child 1 implementation");
assertTrue(filtered.getComponents().getSchemas().containsKey("SomeChild2ImplObject"), "Schemas should contains child 2 implementation");
assertTrue(filtered.getComponents().getSchemas().containsKey("SomeChildObject"), "Schemas should contains child abstract parent");
}

@Test
public void shouldNotRemoveGoodRefs() throws IOException {
final OpenAPI openAPI = getOpenAPI(RESOURCE_PATH);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"components": {
"schemas": {
"SomeChild1ImplObject": {
"allOf": [
{
"$ref": "#/components/schemas/SomeChildObject"
}
]
},
"SomeChild2ImplObject": {
"allOf": [
{
"$ref": "#/components/schemas/SomeChildObject"
}
]
},
"SomeChildObject": {
"description": "Some child object"
},
"SomeParentObject": {
"description": "Some parent object",
"properties": {
"id": {
"description": "id",
"format": "int64",
"type": "integer"
},
"other": {
"description": "other",
"oneOf": [
{
"$ref": "#/components/schemas/SomeChild1ImplObject"
},
{
"$ref": "#/components/schemas/SomeChild2ImplObject"
}
]
}
}
}
}
},
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"openapi": "3.1.0",
"paths": {
"/some/call": {
"get": {
"description": "Some operation description",
"operationId": "getSome",
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SomeParentObject"
}
}
},
"description": "OK"
}
},
"summary": "Some summary",
"tags": [
"Some"
]
}
}
},
"servers": [
{
"description": "Generated server url",
"url": "http://localhost:8080"
}
],
"tags": [
{
"description": "some actions",
"name": "Some"
}
]
}

0 comments on commit 8660f35

Please sign in to comment.