Skip to content

Commit

Permalink
Fix #4618: process array schema in OpenAPI 3.1 same as in OpenAPI 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Frantisek Simon authored and frantuma committed Feb 13, 2024
1 parent 2344ce4 commit 3fcc473
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ private void addSchemaRef(Schema schema, Set<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,13 +38,15 @@
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 {

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";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}

0 comments on commit 3fcc473

Please sign in to comment.