diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/filter/SpecFilter.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/filter/SpecFilter.java index c4f5b6d2af..f930ff9a64 100755 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/filter/SpecFilter.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/filter/SpecFilter.java @@ -308,6 +308,8 @@ private void addSchemaRef(Schema schema, Set referencedDefinitions) { if (schema instanceof ArraySchema && ((ArraySchema) schema).getItems() != null) { 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) { diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/filter/SpecFilterTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/filter/SpecFilterTest.java index 3065f48c45..9ffb7c687e 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/filter/SpecFilterTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/filter/SpecFilterTest.java @@ -15,6 +15,7 @@ import io.swagger.v3.core.filter.resources.ReplaceGetOperationsFilter; import io.swagger.v3.core.matchers.SerializationMatchers; import io.swagger.v3.core.util.Json; +import io.swagger.v3.core.util.Json31; import io.swagger.v3.core.util.ResourceUtils; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; @@ -37,6 +38,7 @@ import static org.testng.Assert.assertNotEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; public class SpecFilterTest { @@ -44,6 +46,7 @@ public class SpecFilterTest { private static final String RESOURCE_RECURSIVE_MODELS = "specFiles/recursivemodels.json"; 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_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"; @@ -273,6 +276,16 @@ public void shouldRemoveBrokenNestedRefs() throws IOException { assertNotNull(filtered.getComponents().getSchemas().get("discriminatorMatchedChildB")); } + @Test + public void shouldRemoveBrokenNestedRefsKeepArray() throws IOException { + final OpenAPI openAPI = getOpenAPI31(RESOURCE_PATH_LIST); + final RemoveUnreferencedDefinitionsFilter remover = new RemoveUnreferencedDefinitionsFilter(); + final OpenAPI filtered = new SpecFilter().filter(openAPI, remover, null, null, null); + + assertEquals(filtered.getComponents().getSchemas().size(), 2, "Expected to have parent and child list schemas"); + assertTrue(filtered.getComponents().getSchemas().containsKey("SomeChildObject"), "Schemas should contains child list"); + } + @Test public void shouldNotRemoveGoodRefs() throws IOException { final OpenAPI openAPI = getOpenAPI(RESOURCE_PATH); @@ -438,4 +451,9 @@ private OpenAPI getOpenAPI(String path) throws IOException { final String json = ResourceUtils.loadClassResource(getClass(), path); return Json.mapper().readValue(json, OpenAPI.class); } + + private OpenAPI getOpenAPI31(String path) throws IOException { + final String json = ResourceUtils.loadClassResource(getClass(), path); + return Json31.mapper().readValue(json, OpenAPI.class); + } } diff --git a/modules/swagger-core/src/test/resources/specFiles/3.1.0/list-3.1.json b/modules/swagger-core/src/test/resources/specFiles/3.1.0/list-3.1.json new file mode 100644 index 0000000000..0c7e95ad93 --- /dev/null +++ b/modules/swagger-core/src/test/resources/specFiles/3.1.0/list-3.1.json @@ -0,0 +1,78 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "OpenAPI definition", + "version": "v0" + }, + "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" + } + ], + "components": { + "schemas": { + "SomeChildObject": { + "description": "Some child object", + "properties": { + "id": { + "description": "id", + "format": "int64", + "type": "integer" + }, + "name": { + "description": "name", + "type": "string" + } + } + }, + "SomeParentObject": { + "description": "Some parent object", + "properties": { + "id": { + "description": "id", + "format": "int64", + "type": "integer" + }, + "someList": { + "description": "list", + "items": { + "$ref": "#/components/schemas/SomeChildObject" + }, + "type": "array" + } + } + } + } + } +} \ No newline at end of file