Skip to content

Commit ece9cd5

Browse files
committed
Revert "#1889 - Add reference to parent element"
1 parent 0805312 commit ece9cd5

File tree

8 files changed

+36
-321
lines changed

8 files changed

+36
-321
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
119119
cache.addReferencedKey(newRef);
120120

121121
String file = $ref.split("#/")[0];
122-
if (schema.get$ref() != null && !Objects.equals(schema.getType(), "array")) {
122+
if (schema.get$ref() != null) {
123123
RefFormat ref = computeRefFormat(schema.get$ref());
124124
if (isAnExternalRefFormat(ref)) {
125125
if (!ref.equals(RefFormat.URL)) {

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/SchemaProcessor.java

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import io.swagger.v3.oas.models.OpenAPI;
5+
56
import io.swagger.v3.oas.models.media.ArraySchema;
67
import io.swagger.v3.oas.models.media.ComposedSchema;
78
import io.swagger.v3.oas.models.media.Discriminator;
@@ -87,16 +88,6 @@ public void processSchemaType(Schema schema){
8788
processDiscriminatorSchema(schema);
8889
}
8990

90-
if (doesInternalSchemaContains(schema)) {
91-
processInternalPropertyReferences(schema.getItems().getItems());
92-
}
93-
}
94-
95-
private boolean doesInternalSchemaContains(Schema schema) {
96-
return schema.getItems() != null
97-
&& schema.getItems().getItems() != null
98-
&& schema.getItems().getItems().get$ref() != null
99-
&& schema.getItems().getItems().get$ref().startsWith("..");
10091
}
10192

10293
private void processDiscriminatorSchema(Schema schema) {
@@ -115,7 +106,7 @@ private void processAdditionalProperties(Object additionalProperties) {
115106
if (schema.getAdditionalProperties() != null && schema.getAdditionalProperties() instanceof Schema) {
116107
Schema additionalPropertiesSchema = (Schema) schema.getAdditionalProperties();
117108
if (additionalPropertiesSchema.get$ref() != null) {
118-
processReferenceSchemaForProperty(additionalPropertiesSchema);
109+
processReferenceSchema(additionalPropertiesSchema);
119110
} else {
120111
processSchemaType(additionalPropertiesSchema);
121112
}
@@ -139,25 +130,17 @@ public void processPropertySchema(Schema schema) {
139130
processReferenceSchema(schema);
140131
}
141132

142-
Map<String, Schema> properties = schema.getProperties();
143-
if (properties != null) {
144-
for (Map.Entry<String, Schema> propertyEntry : properties.entrySet()) {
145-
Schema property = propertyEntry.getValue();
146-
if(property.get$ref() != null) {
147-
if (property instanceof ArraySchema) {
148-
processReferenceSchemaForProperty(property);
149-
} else {
150-
processReferenceSchema(property);
151-
}
152-
}else {
153-
processSchemaType(property);
154-
}
155-
}
156-
}
157-
}
158-
159-
private void processInternalPropertyReferences(Schema schema) {
160-
processReferenceSchemaForProperty(schema);
133+
Map<String, Schema> properties = schema.getProperties();
134+
if (properties != null) {
135+
for (Map.Entry<String, Schema> propertyEntry : properties.entrySet()) {
136+
Schema property = propertyEntry.getValue();
137+
if(property.get$ref() != null) {
138+
processReferenceSchema(property);
139+
}else {
140+
processSchemaType(property);
141+
}
142+
}
143+
}
161144
}
162145

163146
public void processComposedSchema(ComposedSchema composedSchema) {
@@ -251,23 +234,11 @@ private void processReferenceSchema(Schema schema) {
251234

252235
if (isAnExternalRefFormat(refFormat)){
253236
final String newRef = externalRefProcessor.processRefToExternalSchema($ref, refFormat);
237+
254238
if (newRef != null) {
255239
schema.set$ref(RefType.SCHEMAS.getInternalPrefix() + newRef);
256240
}
257241
}
258242
}
259243

260-
private void processReferenceSchemaForProperty(Schema schema) {
261-
RefFormat refFormat = computeRefFormat(schema.get$ref());
262-
String $ref = schema.get$ref();
263-
264-
final String newRef = externalRefProcessor.processRefToExternalSchema($ref, refFormat);
265-
if (newRef != null && !newRef.startsWith("#/components")) {
266-
schema.set$ref(RefType.SCHEMAS.getInternalPrefix() + newRef);
267-
}
268-
Schema internalSchema = schema.getItems();
269-
if (internalSchema != null && !internalSchema.get$ref().startsWith("#/components")) {
270-
processReferenceSchemaForProperty(schema.getItems());
271-
}
272-
}
273-
}
244+
}

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2754,9 +2754,7 @@ at the moment path passed as string (basePath) from upper components can be both
27542754

27552755
if (schema == null) {
27562756
schema = SchemaTypeUtil.createSchemaByType(node);
2757-
} else if (itemsNode != null && itemsNode.has("$ref" )) {
2758-
SchemaTypeUtil.updateReferenceForParentNode(schema, itemsNode.get("$ref").textValue());
2759-
}
2757+
}
27602758

27612759
JsonNode ref = node.get("$ref");
27622760
if (ref != null) {

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public Schema resolveSchema(Schema schema) {
303303
return null;
304304
}
305305

306-
if(schema.get$ref() != null && schema.getItems() == null) {
306+
if(schema.get$ref() != null) {
307307
String ref= schema.get$ref();
308308
Schema resolved;
309309
//This validation is done to solve deep properties eg. '#/components/schemas/TypeProject/properties/id'
@@ -312,8 +312,8 @@ public Schema resolveSchema(Schema schema) {
312312
String refSchema = split[3];
313313
Schema parentSchema = schemas.get(refSchema);
314314
ref = ref.substring(ref.lastIndexOf("/") + 1);
315-
resolved = parentSchema != null ? (Schema) parentSchema.getProperties().get(ref) : null;
316-
} else {
315+
resolved = (Schema)parentSchema.getProperties().get(ref);
316+
}else {
317317
ref = ref.substring(ref.lastIndexOf("/") + 1);
318318
resolved = schemas != null ? schemas.get(ref) : null;
319319
}

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/SchemaTypeUtil.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class SchemaTypeUtil {
4343
public static final String BINARY_AS_STRING = "swaggerParserBinaryAsString";
4444

4545
public static Schema createSchemaByType(ObjectNode node){
46-
if (node == null) {
46+
if(node == null) {
4747
return new Schema();
4848
}
4949
final String type = getNodeValue(node, TYPE);
@@ -55,10 +55,6 @@ public static Schema createSchemaByType(ObjectNode node){
5555
return createSchema(type, format);
5656
}
5757

58-
public static void updateReferenceForParentNode(Schema schema, String ref) {
59-
schema.set$ref(ref);
60-
}
61-
6258
public static Schema createSchema(String type, String format) {
6359

6460
if(INTEGER_TYPE.equals(type)) {

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,15 @@ public void testIssue1170(@Injectable final List<AuthorizationValue> auths) {
737737
assertNotNull(breedsListSchema);
738738
assertNotNull(breedSchema);
739739

740+
assertTrue(breedsListSchema instanceof ArraySchema);
741+
Schema breedPropertySchema = ((ArraySchema) breedsListSchema).getItems().getProperties().get("breed");
742+
assertNotNull(breedPropertySchema);
743+
744+
// Verify items resolved fully
745+
assertTrue(breedPropertySchema.get$ref() == null);
746+
assertTrue(breedPropertySchema == breedSchema);
747+
748+
740749
// Array schema with inline items object with $ref properties
741750
Schema petsListSchema = openAPI.getComponents().getSchemas().get("PetsList");
742751
Schema colouringsSchema = openAPI.getComponents().getSchemas().get("Colouring");

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.swagger.v3.parser.test;
22

3+
34
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
45
import static com.github.tomakehurst.wiremock.client.WireMock.get;
56
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
@@ -45,6 +46,7 @@
4546
import com.github.tomakehurst.wiremock.WireMockServer;
4647
import com.github.tomakehurst.wiremock.client.WireMock;
4748
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
49+
4850
import io.swagger.v3.core.util.Json;
4951
import io.swagger.v3.core.util.Yaml;
5052
import io.swagger.v3.oas.models.Components;
@@ -77,32 +79,8 @@
7779
import io.swagger.v3.parser.core.models.AuthorizationValue;
7880
import io.swagger.v3.parser.core.models.ParseOptions;
7981
import io.swagger.v3.parser.core.models.SwaggerParseResult;
80-
import io.swagger.v3.parser.util.SchemaTypeUtil;
8182
import mockit.Injectable;
82-
import org.apache.commons.io.FileUtils;
83-
import org.hamcrest.CoreMatchers;
84-
import org.junit.Ignore;
85-
import org.testng.Assert;
86-
import org.testng.annotations.AfterClass;
87-
import org.testng.annotations.BeforeClass;
88-
import org.testng.annotations.Test;
89-
import org.testng.reporters.Files;
90-
91-
import java.io.File;
92-
import java.io.IOException;
93-
import java.math.BigDecimal;
94-
import java.net.HttpURLConnection;
95-
import java.net.URL;
96-
import java.nio.charset.StandardCharsets;
97-
import java.util.*;
9883

99-
import static com.github.tomakehurst.wiremock.client.WireMock.*;
100-
import static java.util.Arrays.asList;
101-
import static java.util.Collections.emptyList;
102-
import static org.hamcrest.CoreMatchers.equalTo;
103-
import static org.hamcrest.CoreMatchers.*;
104-
import static org.junit.Assert.assertThat;
105-
import static org.testng.Assert.*;
10684

10785
public class OpenAPIV3ParserTest {
10886
protected int serverPort = getDynamicPort();
@@ -450,7 +428,7 @@ public void testExampleFormatByte() throws Exception{
450428
}
451429

452430
@Test
453-
public void testIssue1658() throws Exception {
431+
public void testIssue1658() throws Exception{
454432
ParseOptions options = new ParseOptions();
455433
options.setResolve(true);
456434
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("src/test/resources/issue-1658/issue1658.yaml", null, options);
@@ -466,20 +444,6 @@ public void testIssue1658() throws Exception {
466444

467445
}
468446

469-
@Test
470-
public void testIssue1889_ArrayReferenceNull() {
471-
ParseOptions options = new ParseOptions();
472-
options.setResolve(true);
473-
SwaggerParseResult result = new OpenAPIV3Parser()
474-
.readLocation("src/test/resources/issue-1889/issue1889.yaml", null, options);
475-
Assert.assertNotNull(result);
476-
Assert.assertNotNull(result.getOpenAPI());
477-
OpenAPI openAPI = result.getOpenAPI();
478-
String expectedReference = openAPI.getPaths().get("/pets").getGet().getResponses().get("200").getContent()
479-
.get("application/json").getSchema().get$ref();
480-
assertEquals(expectedReference, "#/components/schemas/Pet");
481-
}
482-
483447
@Test
484448
public void testIssue1644_NullValue() throws Exception{
485449
ParseOptions options = new ParseOptions();
@@ -2332,9 +2296,9 @@ private OpenAPI doRelativeFileTest(String location) {
23322296
assertEquals(refInDefinitions.getDescription(), "The example model");
23332297
expectedPropertiesInModel(refInDefinitions, "foo", "bar");
23342298

2335-
final ObjectSchema referencedObjectModel = (ObjectSchema) definitions.get("arrayModel");
2336-
final Map<String, Schema> referencedObjectProperties = referencedObjectModel.getProperties();
2337-
assertTrue(referencedObjectProperties.containsKey("hello"));
2299+
final ArraySchema arrayModel = (ArraySchema) definitions.get("arrayModel");
2300+
final Schema arrayModelItems = arrayModel.getItems();
2301+
assertEquals(arrayModelItems.get$ref(), "#/components/schemas/foo");
23382302

23392303
final Schema fooModel = definitions.get("foo");
23402304
assertEquals(fooModel.getDescription(), "Just another model");

0 commit comments

Comments
 (0)