diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java index bdba5d20d6..67ad3ffda6 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java @@ -119,7 +119,7 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) { cache.addReferencedKey(newRef); String file = $ref.split("#/")[0]; - if (schema.get$ref() != null && !Objects.equals(schema.getType(), "array")) { + if (schema.get$ref() != null) { RefFormat ref = computeRefFormat(schema.get$ref()); if (isAnExternalRefFormat(ref)) { if (!ref.equals(RefFormat.URL)) { diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/SchemaProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/SchemaProcessor.java index a253aa3cda..6a3f480ab9 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/SchemaProcessor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/SchemaProcessor.java @@ -2,6 +2,7 @@ import io.swagger.v3.oas.models.OpenAPI; + import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.ComposedSchema; import io.swagger.v3.oas.models.media.Discriminator; @@ -87,16 +88,6 @@ public void processSchemaType(Schema schema){ processDiscriminatorSchema(schema); } - if (doesInternalSchemaContains(schema)) { - processInternalPropertyReferences(schema.getItems().getItems()); - } - } - - private boolean doesInternalSchemaContains(Schema schema) { - return schema.getItems() != null - && schema.getItems().getItems() != null - && schema.getItems().getItems().get$ref() != null - && schema.getItems().getItems().get$ref().startsWith(".."); } private void processDiscriminatorSchema(Schema schema) { @@ -115,7 +106,7 @@ private void processAdditionalProperties(Object additionalProperties) { if (schema.getAdditionalProperties() != null && schema.getAdditionalProperties() instanceof Schema) { Schema additionalPropertiesSchema = (Schema) schema.getAdditionalProperties(); if (additionalPropertiesSchema.get$ref() != null) { - processReferenceSchemaForProperty(additionalPropertiesSchema); + processReferenceSchema(additionalPropertiesSchema); } else { processSchemaType(additionalPropertiesSchema); } @@ -139,25 +130,17 @@ public void processPropertySchema(Schema schema) { processReferenceSchema(schema); } - Map properties = schema.getProperties(); - if (properties != null) { - for (Map.Entry propertyEntry : properties.entrySet()) { - Schema property = propertyEntry.getValue(); - if(property.get$ref() != null) { - if (property instanceof ArraySchema) { - processReferenceSchemaForProperty(property); - } else { - processReferenceSchema(property); - } - }else { - processSchemaType(property); - } - } - } - } - - private void processInternalPropertyReferences(Schema schema) { - processReferenceSchemaForProperty(schema); + Map properties = schema.getProperties(); + if (properties != null) { + for (Map.Entry propertyEntry : properties.entrySet()) { + Schema property = propertyEntry.getValue(); + if(property.get$ref() != null) { + processReferenceSchema(property); + }else { + processSchemaType(property); + } + } + } } public void processComposedSchema(ComposedSchema composedSchema) { @@ -251,23 +234,11 @@ private void processReferenceSchema(Schema schema) { if (isAnExternalRefFormat(refFormat)){ final String newRef = externalRefProcessor.processRefToExternalSchema($ref, refFormat); + if (newRef != null) { schema.set$ref(RefType.SCHEMAS.getInternalPrefix() + newRef); } } } - private void processReferenceSchemaForProperty(Schema schema) { - RefFormat refFormat = computeRefFormat(schema.get$ref()); - String $ref = schema.get$ref(); - - final String newRef = externalRefProcessor.processRefToExternalSchema($ref, refFormat); - if (newRef != null && !newRef.startsWith("#/components")) { - schema.set$ref(RefType.SCHEMAS.getInternalPrefix() + newRef); - } - Schema internalSchema = schema.getItems(); - if (internalSchema != null && !internalSchema.get$ref().startsWith("#/components")) { - processReferenceSchemaForProperty(schema.getItems()); - } - } -} +} \ No newline at end of file diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java index 9b59c4af05..a289916bf1 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java @@ -2754,9 +2754,7 @@ at the moment path passed as string (basePath) from upper components can be both if (schema == null) { schema = SchemaTypeUtil.createSchemaByType(node); - } else if (itemsNode != null && itemsNode.has("$ref" )) { - SchemaTypeUtil.updateReferenceForParentNode(schema, itemsNode.get("$ref").textValue()); - } + } JsonNode ref = node.get("$ref"); if (ref != null) { diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java index bace11b688..d2aec84e16 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java @@ -303,7 +303,7 @@ public Schema resolveSchema(Schema schema) { return null; } - if(schema.get$ref() != null && schema.getItems() == null) { + if(schema.get$ref() != null) { String ref= schema.get$ref(); Schema resolved; //This validation is done to solve deep properties eg. '#/components/schemas/TypeProject/properties/id' @@ -312,8 +312,8 @@ public Schema resolveSchema(Schema schema) { String refSchema = split[3]; Schema parentSchema = schemas.get(refSchema); ref = ref.substring(ref.lastIndexOf("/") + 1); - resolved = parentSchema != null ? (Schema) parentSchema.getProperties().get(ref) : null; - } else { + resolved = (Schema)parentSchema.getProperties().get(ref); + }else { ref = ref.substring(ref.lastIndexOf("/") + 1); resolved = schemas != null ? schemas.get(ref) : null; } diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/SchemaTypeUtil.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/SchemaTypeUtil.java index 76bdadafe1..87cc7b118a 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/SchemaTypeUtil.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/SchemaTypeUtil.java @@ -43,7 +43,7 @@ public class SchemaTypeUtil { public static final String BINARY_AS_STRING = "swaggerParserBinaryAsString"; public static Schema createSchemaByType(ObjectNode node){ - if (node == null) { + if(node == null) { return new Schema(); } final String type = getNodeValue(node, TYPE); @@ -55,10 +55,6 @@ public static Schema createSchemaByType(ObjectNode node){ return createSchema(type, format); } - public static void updateReferenceForParentNode(Schema schema, String ref) { - schema.set$ref(ref); - } - public static Schema createSchema(String type, String format) { if(INTEGER_TYPE.equals(type)) { diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java index 91eb5d5a9a..a02fc929bb 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java @@ -737,6 +737,15 @@ public void testIssue1170(@Injectable final List auths) { assertNotNull(breedsListSchema); assertNotNull(breedSchema); + assertTrue(breedsListSchema instanceof ArraySchema); + Schema breedPropertySchema = ((ArraySchema) breedsListSchema).getItems().getProperties().get("breed"); + assertNotNull(breedPropertySchema); + + // Verify items resolved fully + assertTrue(breedPropertySchema.get$ref() == null); + assertTrue(breedPropertySchema == breedSchema); + + // Array schema with inline items object with $ref properties Schema petsListSchema = openAPI.getComponents().getSchemas().get("PetsList"); Schema colouringsSchema = openAPI.getComponents().getSchemas().get("Colouring"); diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java index 285f97e5c1..0f08bea307 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java @@ -1,5 +1,6 @@ package io.swagger.v3.parser.test; + import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; @@ -45,6 +46,7 @@ import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; import com.github.tomakehurst.wiremock.core.WireMockConfiguration; + import io.swagger.v3.core.util.Json; import io.swagger.v3.core.util.Yaml; import io.swagger.v3.oas.models.Components; @@ -77,32 +79,8 @@ import io.swagger.v3.parser.core.models.AuthorizationValue; import io.swagger.v3.parser.core.models.ParseOptions; import io.swagger.v3.parser.core.models.SwaggerParseResult; -import io.swagger.v3.parser.util.SchemaTypeUtil; import mockit.Injectable; -import org.apache.commons.io.FileUtils; -import org.hamcrest.CoreMatchers; -import org.junit.Ignore; -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; -import org.testng.reporters.Files; - -import java.io.File; -import java.io.IOException; -import java.math.BigDecimal; -import java.net.HttpURLConnection; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.*; -import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static java.util.Arrays.asList; -import static java.util.Collections.emptyList; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.assertThat; -import static org.testng.Assert.*; public class OpenAPIV3ParserTest { protected int serverPort = getDynamicPort(); @@ -450,7 +428,7 @@ public void testExampleFormatByte() throws Exception{ } @Test - public void testIssue1658() throws Exception { + public void testIssue1658() throws Exception{ ParseOptions options = new ParseOptions(); options.setResolve(true); SwaggerParseResult result = new OpenAPIV3Parser().readLocation("src/test/resources/issue-1658/issue1658.yaml", null, options); @@ -466,20 +444,6 @@ public void testIssue1658() throws Exception { } - @Test - public void testIssue1889_ArrayReferenceNull() { - ParseOptions options = new ParseOptions(); - options.setResolve(true); - SwaggerParseResult result = new OpenAPIV3Parser() - .readLocation("src/test/resources/issue-1889/issue1889.yaml", null, options); - Assert.assertNotNull(result); - Assert.assertNotNull(result.getOpenAPI()); - OpenAPI openAPI = result.getOpenAPI(); - String expectedReference = openAPI.getPaths().get("/pets").getGet().getResponses().get("200").getContent() - .get("application/json").getSchema().get$ref(); - assertEquals(expectedReference, "#/components/schemas/Pet"); - } - @Test public void testIssue1644_NullValue() throws Exception{ ParseOptions options = new ParseOptions(); @@ -2332,9 +2296,9 @@ private OpenAPI doRelativeFileTest(String location) { assertEquals(refInDefinitions.getDescription(), "The example model"); expectedPropertiesInModel(refInDefinitions, "foo", "bar"); - final ObjectSchema referencedObjectModel = (ObjectSchema) definitions.get("arrayModel"); - final Map referencedObjectProperties = referencedObjectModel.getProperties(); - assertTrue(referencedObjectProperties.containsKey("hello")); + final ArraySchema arrayModel = (ArraySchema) definitions.get("arrayModel"); + final Schema arrayModelItems = arrayModel.getItems(); + assertEquals(arrayModelItems.get$ref(), "#/components/schemas/foo"); final Schema fooModel = definitions.get("foo"); assertEquals(fooModel.getDescription(), "Just another model"); diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1889/issue1889.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1889/issue1889.yaml deleted file mode 100644 index bbfcd52c48..0000000000 --- a/modules/swagger-parser-v3/src/test/resources/issue-1889/issue1889.yaml +++ /dev/null @@ -1,223 +0,0 @@ -{ - "openapi" : "3.0.0", - "info" : { - "version" : "1.0.0", - "title" : "Swagger Petstore", - "description" : "A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification", - "termsOfService" : "http://swagger.io/terms/", - "contact" : { - "name" : "Swagger API Team", - "email" : "apiteam@swagger.io", - "url" : "http://swagger.io" - }, - "license" : { - "name" : "Apache 2.0", - "url" : "https://www.apache.org/licenses/LICENSE-2.0.html" - } - }, - "servers" : [ { - "description" : "SwaggerHub API Auto Mocking", - "url" : "https://virtserver.swaggerhub.com/mocklab/uspto-data_set_api/1.0.0" - }, { - "url" : "http://petstore.swagger.io/api" - } ], - "paths" : { - "/pets" : { - "get" : { - "description" : "Returns all pets from the system that the user has access to\nNam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia.\n\nSed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien.\n", - "operationId" : "findPets", - "parameters" : [ { - "name" : "tags", - "in" : "query", - "description" : "tags to filter by", - "required" : false, - "style" : "form", - "schema" : { - "type" : "array", - "items" : { - "type" : "string" - } - } - }, { - "name" : "limit", - "in" : "query", - "description" : "maximum number of results to return", - "required" : false, - "schema" : { - "type" : "integer", - "format" : "int32" - } - } ], - "responses" : { - "200" : { - "description" : "pet response", - "content" : { - "application/json" : { - "schema" : { - "type" : "array", - "items" : { - "$ref" : "#/components/schemas/Pet" - } - } - } - } - }, - "default" : { - "description" : "unexpected error", - "content" : { - "application/json" : { - "schema" : { - "$ref" : "#/components/schemas/Error" - } - } - } - } - } - }, - "post" : { - "description" : "Creates a new pet in the store. Duplicates are allowed", - "operationId" : "addPet", - "requestBody" : { - "description" : "Pet to add to the store", - "required" : true, - "content" : { - "application/json" : { - "schema" : { - "$ref" : "#/components/schemas/NewPet" - } - } - } - }, - "responses" : { - "200" : { - "description" : "pet response", - "content" : { - "application/json" : { - "schema" : { - "$ref" : "#/components/schemas/Pet" - } - } - } - }, - "default" : { - "description" : "unexpected error", - "content" : { - "application/json" : { - "schema" : { - "$ref" : "#/components/schemas/Error" - } - } - } - } - } - } - }, - "/pets/{id}" : { - "get" : { - "description" : "Returns a user based on a single ID, if the user does not have access to the pet", - "operationId" : "find pet by id", - "parameters" : [ { - "name" : "id", - "in" : "path", - "description" : "ID of pet to fetch", - "required" : true, - "schema" : { - "type" : "integer", - "format" : "int64" - } - } ], - "responses" : { - "200" : { - "description" : "pet response", - "content" : { - "application/json" : { - "schema" : { - "$ref" : "#/components/schemas/Pet" - } - } - } - }, - "default" : { - "description" : "unexpected error", - "content" : { - "application/json" : { - "schema" : { - "$ref" : "#/components/schemas/Error" - } - } - } - } - } - }, - "delete" : { - "description" : "deletes a single pet based on the ID supplied", - "operationId" : "deletePet", - "parameters" : [ { - "name" : "id", - "in" : "path", - "description" : "ID of pet to delete", - "required" : true, - "schema" : { - "type" : "integer", - "format" : "int64" - } - } ], - "responses" : { - "204" : { - "description" : "pet deleted" - }, - "default" : { - "description" : "unexpected error", - "content" : { - "application/json" : { - "schema" : { - "$ref" : "#/components/schemas/Error" - } - } - } - } - } - } - } - }, - "components" : { - "schemas" : { - "Pet" : { - "allOf" : [ { - "$ref" : "#/components/schemas/NewPet" - }, { - "required" : [ "id" ], - "properties" : { - "id" : { - "type" : "integer", - "format" : "int64" - } - } - } ] - }, - "NewPet" : { - "required" : [ "name" ], - "properties" : { - "name" : { - "type" : "string" - }, - "tag" : { - "type" : "string" - } - } - }, - "Error" : { - "required" : [ "code", "message" ], - "properties" : { - "code" : { - "type" : "integer", - "format" : "int32" - }, - "message" : { - "type" : "string" - } - } - } - } - } -} \ No newline at end of file