Skip to content

Commit

Permalink
Revert swagger-api#1889 - Add reference to parent element
Browse files Browse the repository at this point in the history
This reverts commit 7e3dd07.
This reverts commit 1d9af14.
This reverts commit 815de7b.
This reverts commit f4fe2dc.
This reverts commit bd2570f.
This reverts commit 2f3865a.
This reverts commit 7e6af13.
  • Loading branch information
Mahoney committed Apr 26, 2023
1 parent 26605a1 commit a4051c9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand All @@ -139,25 +128,17 @@ public void processPropertySchema(Schema schema) {
processReferenceSchema(schema);
}

Map<String, Schema> properties = schema.getProperties();
if (properties != null) {
for (Map.Entry<String, Schema> 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<String, Schema> properties = schema.getProperties();
if (properties != null) {
for (Map.Entry<String, Schema> propertyEntry : properties.entrySet()) {
Schema property = propertyEntry.getValue();
if(property.get$ref() != null) {
processReferenceSchema(property);
}else {
processSchemaType(property);
}
}
}
}

public void processComposedSchema(ComposedSchema composedSchema) {
Expand Down Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,15 @@ public void testIssue1170(@Injectable final List<AuthorizationValue> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a4051c9

Please sign in to comment.