Skip to content

Commit 8660f35

Browse files
authored
Merge pull request #4637 from GooDer/openApi-3.1-ComposedSchema-fix
Fix #4634: unite processing of allOf, anyOf, oneOf relation so it wil…
2 parents 616350b + 49d9684 commit 8660f35

File tree

3 files changed

+109
-9
lines changed

3 files changed

+109
-9
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/filter/SpecFilter.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import io.swagger.v3.oas.models.callbacks.Callback;
1313
import io.swagger.v3.oas.models.headers.Header;
1414
import io.swagger.v3.oas.models.media.ArraySchema;
15-
import io.swagger.v3.oas.models.media.ComposedSchema;
1615
import io.swagger.v3.oas.models.media.Content;
1716
import io.swagger.v3.oas.models.media.MediaType;
1817
import io.swagger.v3.oas.models.media.Schema;
@@ -310,20 +309,23 @@ private void addSchemaRef(Schema schema, Set<String> referencedDefinitions) {
310309
addSchemaRef(((ArraySchema) schema).getItems(), referencedDefinitions);
311310
} else if (schema.getTypes() != null && schema.getTypes().contains("array") && schema.getItems() != null) {
312311
addSchemaRef(schema.getItems(), referencedDefinitions);
313-
} else if (schema instanceof ComposedSchema) {
314-
ComposedSchema composedSchema = (ComposedSchema) schema;
315-
if (composedSchema.getAllOf() != null) {
316-
for (Schema ref : composedSchema.getAllOf()) {
312+
} else {
313+
List<Schema> allOf = schema.getAllOf();
314+
List<Schema> anyOf = schema.getAnyOf();
315+
List<Schema> oneOf = schema.getOneOf();
316+
317+
if (allOf != null) {
318+
for (Schema ref : allOf) {
317319
addSchemaRef(ref, referencedDefinitions);
318320
}
319321
}
320-
if (composedSchema.getAnyOf() != null) {
321-
for (Schema ref : composedSchema.getAnyOf()) {
322+
if (anyOf != null) {
323+
for (Schema ref : anyOf) {
322324
addSchemaRef(ref, referencedDefinitions);
323325
}
324326
}
325-
if (composedSchema.getOneOf() != null) {
326-
for (Schema ref : composedSchema.getOneOf()) {
327+
if (oneOf != null) {
328+
for (Schema ref : oneOf) {
327329
addSchemaRef(ref, referencedDefinitions);
328330
}
329331
}

modules/swagger-core/src/test/java/io/swagger/v3/core/filter/SpecFilterTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class SpecFilterTest {
4747
private static final String RESOURCE_PATH = "specFiles/petstore-3.0-v2.json";
4848
private static final String RESOURCE_PATH_3303 = "specFiles/petstore-3.0-v2-ticket-3303.json";
4949
private static final String RESOURCE_PATH_LIST = "specFiles/3.1.0/list-3.1.json";
50+
private static final String RESOURCE_PATH_COMPOSED_SCHEMA = "specFiles/3.1.0/composed-schema-3.1.json";
5051
private static final String RESOURCE_REFERRED_SCHEMAS = "specFiles/petstore-3.0-referred-schemas.json";
5152
private static final String RESOURCE_PATH_WITHOUT_MODELS = "specFiles/petstore-3.0-v2_withoutModels.json";
5253
private static final String RESOURCE_DEPRECATED_OPERATIONS = "specFiles/deprecatedoperationmodel.json";
@@ -286,6 +287,18 @@ public void shouldRemoveBrokenNestedRefsKeepArray() throws IOException {
286287
assertTrue(filtered.getComponents().getSchemas().containsKey("SomeChildObject"), "Schemas should contains child list");
287288
}
288289

290+
@Test
291+
public void shouldRemoveBrokenNestedRefsKeepComposedSchemas() throws IOException {
292+
final OpenAPI openAPI = getOpenAPI31(RESOURCE_PATH_COMPOSED_SCHEMA);
293+
final RemoveUnreferencedDefinitionsFilter remover = new RemoveUnreferencedDefinitionsFilter();
294+
final OpenAPI filtered = new SpecFilter().filter(openAPI, remover, null, null, null);
295+
296+
assertEquals(filtered.getComponents().getSchemas().size(), 4, "Expected to have parent and abstract child with both implementations schemas");
297+
assertTrue(filtered.getComponents().getSchemas().containsKey("SomeChild1ImplObject"), "Schemas should contains child 1 implementation");
298+
assertTrue(filtered.getComponents().getSchemas().containsKey("SomeChild2ImplObject"), "Schemas should contains child 2 implementation");
299+
assertTrue(filtered.getComponents().getSchemas().containsKey("SomeChildObject"), "Schemas should contains child abstract parent");
300+
}
301+
289302
@Test
290303
public void shouldNotRemoveGoodRefs() throws IOException {
291304
final OpenAPI openAPI = getOpenAPI(RESOURCE_PATH);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"components": {
3+
"schemas": {
4+
"SomeChild1ImplObject": {
5+
"allOf": [
6+
{
7+
"$ref": "#/components/schemas/SomeChildObject"
8+
}
9+
]
10+
},
11+
"SomeChild2ImplObject": {
12+
"allOf": [
13+
{
14+
"$ref": "#/components/schemas/SomeChildObject"
15+
}
16+
]
17+
},
18+
"SomeChildObject": {
19+
"description": "Some child object"
20+
},
21+
"SomeParentObject": {
22+
"description": "Some parent object",
23+
"properties": {
24+
"id": {
25+
"description": "id",
26+
"format": "int64",
27+
"type": "integer"
28+
},
29+
"other": {
30+
"description": "other",
31+
"oneOf": [
32+
{
33+
"$ref": "#/components/schemas/SomeChild1ImplObject"
34+
},
35+
{
36+
"$ref": "#/components/schemas/SomeChild2ImplObject"
37+
}
38+
]
39+
}
40+
}
41+
}
42+
}
43+
},
44+
"info": {
45+
"title": "OpenAPI definition",
46+
"version": "v0"
47+
},
48+
"openapi": "3.1.0",
49+
"paths": {
50+
"/some/call": {
51+
"get": {
52+
"description": "Some operation description",
53+
"operationId": "getSome",
54+
"responses": {
55+
"200": {
56+
"content": {
57+
"application/json": {
58+
"schema": {
59+
"$ref": "#/components/schemas/SomeParentObject"
60+
}
61+
}
62+
},
63+
"description": "OK"
64+
}
65+
},
66+
"summary": "Some summary",
67+
"tags": [
68+
"Some"
69+
]
70+
}
71+
}
72+
},
73+
"servers": [
74+
{
75+
"description": "Generated server url",
76+
"url": "http://localhost:8080"
77+
}
78+
],
79+
"tags": [
80+
{
81+
"description": "some actions",
82+
"name": "Some"
83+
}
84+
]
85+
}

0 commit comments

Comments
 (0)