From a4051c9cc802750a8952c265d51ff418c9ef716d Mon Sep 17 00:00:00 2001 From: Robert Elliot Date: Wed, 26 Apr 2023 16:57:45 +0100 Subject: [PATCH] Revert #1889 - Add reference to parent element This reverts commit 7e3dd076d576f214fb810d283b5ee1723dc4309d. This reverts commit 1d9af1427301888e2f8de229ec87e4b4cd429dfb. This reverts commit 815de7b0d51fc444c4c3430773f52781a4256a82. This reverts commit f4fe2dc0beefdd50a0dccfaacf0faaee68ba297d. This reverts commit bd2570f9e912a387a08f8d6dd2d762c77dade981. This reverts commit 2f3865a44b2df62077314e37bfb6774d114a68cc. This reverts commit 7e6af139609b9fd31535538c778138e0888cf385. --- .../processors/ExternalRefProcessor.java | 2 +- .../v3/parser/processors/SchemaProcessor.java | 57 ++++--------------- .../v3/parser/util/OpenAPIDeserializer.java | 4 +- .../swagger/v3/parser/util/ResolverFully.java | 4 +- .../v3/parser/util/SchemaTypeUtil.java | 4 -- .../v3/parser/test/OpenAPIResolverTest.java | 9 +++ .../v3/parser/test/OpenAPIV3ParserTest.java | 25 -------- 7 files changed, 25 insertions(+), 80 deletions(-) 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..db63d6cdb4 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 @@ -86,17 +86,6 @@ public void processSchemaType(Schema schema){ if (schema.getDiscriminator() != null) { 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 +104,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 +128,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) { @@ -256,18 +237,4 @@ private void processReferenceSchema(Schema schema) { } } } - - 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()); - } - } } 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..9bb7915a5b 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,7 +312,7 @@ 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; + 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..34021128ad 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 @@ -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..5c8b4b2743 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 @@ -77,32 +77,7 @@ 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();