Skip to content

Commit

Permalink
Revert "#1889 - Add reference to parent element"
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed May 15, 2023
1 parent 0805312 commit ece9cd5
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 321 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 @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand All @@ -139,25 +130,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 @@ -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());
}
}
}
}
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,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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<String, Schema> 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");
Expand Down
Loading

0 comments on commit ece9cd5

Please sign in to comment.