Skip to content

Commit 3fcc473

Browse files
Frantisek Simonfrantuma
authored andcommitted
Fix #4618: process array schema in OpenAPI 3.1 same as in OpenAPI 3.0
1 parent 2344ce4 commit 3fcc473

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ private void addSchemaRef(Schema schema, Set<String> referencedDefinitions) {
308308
if (schema instanceof ArraySchema &&
309309
((ArraySchema) schema).getItems() != null) {
310310
addSchemaRef(((ArraySchema) schema).getItems(), referencedDefinitions);
311+
} else if (schema.getTypes() != null && schema.getTypes().contains("array") && schema.getItems() != null) {
312+
addSchemaRef(schema.getItems(), referencedDefinitions);
311313
} else if (schema instanceof ComposedSchema) {
312314
ComposedSchema composedSchema = (ComposedSchema) schema;
313315
if (composedSchema.getAllOf() != null) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.swagger.v3.core.filter.resources.ReplaceGetOperationsFilter;
1616
import io.swagger.v3.core.matchers.SerializationMatchers;
1717
import io.swagger.v3.core.util.Json;
18+
import io.swagger.v3.core.util.Json31;
1819
import io.swagger.v3.core.util.ResourceUtils;
1920
import io.swagger.v3.oas.models.Components;
2021
import io.swagger.v3.oas.models.OpenAPI;
@@ -37,13 +38,15 @@
3738
import static org.testng.Assert.assertNotEquals;
3839
import static org.testng.Assert.assertNotNull;
3940
import static org.testng.Assert.assertNull;
41+
import static org.testng.Assert.assertTrue;
4042
import static org.testng.Assert.fail;
4143

4244
public class SpecFilterTest {
4345

4446
private static final String RESOURCE_RECURSIVE_MODELS = "specFiles/recursivemodels.json";
4547
private static final String RESOURCE_PATH = "specFiles/petstore-3.0-v2.json";
4648
private static final String RESOURCE_PATH_3303 = "specFiles/petstore-3.0-v2-ticket-3303.json";
49+
private static final String RESOURCE_PATH_LIST = "specFiles/3.1.0/list-3.1.json";
4750
private static final String RESOURCE_REFERRED_SCHEMAS = "specFiles/petstore-3.0-referred-schemas.json";
4851
private static final String RESOURCE_PATH_WITHOUT_MODELS = "specFiles/petstore-3.0-v2_withoutModels.json";
4952
private static final String RESOURCE_DEPRECATED_OPERATIONS = "specFiles/deprecatedoperationmodel.json";
@@ -273,6 +276,16 @@ public void shouldRemoveBrokenNestedRefs() throws IOException {
273276
assertNotNull(filtered.getComponents().getSchemas().get("discriminatorMatchedChildB"));
274277
}
275278

279+
@Test
280+
public void shouldRemoveBrokenNestedRefsKeepArray() throws IOException {
281+
final OpenAPI openAPI = getOpenAPI31(RESOURCE_PATH_LIST);
282+
final RemoveUnreferencedDefinitionsFilter remover = new RemoveUnreferencedDefinitionsFilter();
283+
final OpenAPI filtered = new SpecFilter().filter(openAPI, remover, null, null, null);
284+
285+
assertEquals(filtered.getComponents().getSchemas().size(), 2, "Expected to have parent and child list schemas");
286+
assertTrue(filtered.getComponents().getSchemas().containsKey("SomeChildObject"), "Schemas should contains child list");
287+
}
288+
276289
@Test
277290
public void shouldNotRemoveGoodRefs() throws IOException {
278291
final OpenAPI openAPI = getOpenAPI(RESOURCE_PATH);
@@ -438,4 +451,9 @@ private OpenAPI getOpenAPI(String path) throws IOException {
438451
final String json = ResourceUtils.loadClassResource(getClass(), path);
439452
return Json.mapper().readValue(json, OpenAPI.class);
440453
}
454+
455+
private OpenAPI getOpenAPI31(String path) throws IOException {
456+
final String json = ResourceUtils.loadClassResource(getClass(), path);
457+
return Json31.mapper().readValue(json, OpenAPI.class);
458+
}
441459
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"paths": {
8+
"/some/call": {
9+
"get": {
10+
"description": "Some operation description",
11+
"operationId": "getSome",
12+
"responses": {
13+
"200": {
14+
"content": {
15+
"application/json": {
16+
"schema": {
17+
"$ref": "#/components/schemas/SomeParentObject"
18+
}
19+
}
20+
},
21+
"description": "OK"
22+
}
23+
},
24+
"summary": "Some summary",
25+
"tags": [
26+
"Some"
27+
]
28+
}
29+
}
30+
},
31+
"servers": [
32+
{
33+
"description": "Generated server url",
34+
"url": "http://localhost:8080"
35+
}
36+
],
37+
"tags": [
38+
{
39+
"description": "some actions",
40+
"name": "Some"
41+
}
42+
],
43+
"components": {
44+
"schemas": {
45+
"SomeChildObject": {
46+
"description": "Some child object",
47+
"properties": {
48+
"id": {
49+
"description": "id",
50+
"format": "int64",
51+
"type": "integer"
52+
},
53+
"name": {
54+
"description": "name",
55+
"type": "string"
56+
}
57+
}
58+
},
59+
"SomeParentObject": {
60+
"description": "Some parent object",
61+
"properties": {
62+
"id": {
63+
"description": "id",
64+
"format": "int64",
65+
"type": "integer"
66+
},
67+
"someList": {
68+
"description": "list",
69+
"items": {
70+
"$ref": "#/components/schemas/SomeChildObject"
71+
},
72+
"type": "array"
73+
}
74+
}
75+
}
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)