Skip to content

Commit cbaff9a

Browse files
committed
Revert swagger-api#1889 - Add reference to parent element
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.
1 parent 26605a1 commit cbaff9a

File tree

7 files changed

+29
-84
lines changed

7 files changed

+29
-84
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: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,6 @@ public void processSchemaType(Schema schema){
8686
if (schema.getDiscriminator() != null) {
8787
processDiscriminatorSchema(schema);
8888
}
89-
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("..");
10089
}
10190

10291
private void processDiscriminatorSchema(Schema schema) {
@@ -115,7 +104,7 @@ private void processAdditionalProperties(Object additionalProperties) {
115104
if (schema.getAdditionalProperties() != null && schema.getAdditionalProperties() instanceof Schema) {
116105
Schema additionalPropertiesSchema = (Schema) schema.getAdditionalProperties();
117106
if (additionalPropertiesSchema.get$ref() != null) {
118-
processReferenceSchemaForProperty(additionalPropertiesSchema);
107+
processReferenceSchema(additionalPropertiesSchema);
119108
} else {
120109
processSchemaType(additionalPropertiesSchema);
121110
}
@@ -139,25 +128,17 @@ public void processPropertySchema(Schema schema) {
139128
processReferenceSchema(schema);
140129
}
141130

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);
131+
Map<String, Schema> properties = schema.getProperties();
132+
if (properties != null) {
133+
for (Map.Entry<String, Schema> propertyEntry : properties.entrySet()) {
134+
Schema property = propertyEntry.getValue();
135+
if(property.get$ref() != null) {
136+
processReferenceSchema(property);
137+
}else {
138+
processSchemaType(property);
139+
}
140+
}
141+
}
161142
}
162143

163144
public void processComposedSchema(ComposedSchema composedSchema) {
@@ -256,18 +237,4 @@ private void processReferenceSchema(Schema schema) {
256237
}
257238
}
258239
}
259-
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-
}
273240
}

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: 2 additions & 2 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,7 +312,7 @@ 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;
315+
resolved = (Schema) parentSchema.getProperties().get(ref);
316316
} else {
317317
ref = ref.substring(ref.lastIndexOf("/") + 1);
318318
resolved = schemas != null ? schemas.get(ref) : null;

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,7 @@
7777
import io.swagger.v3.parser.core.models.AuthorizationValue;
7878
import io.swagger.v3.parser.core.models.ParseOptions;
7979
import io.swagger.v3.parser.core.models.SwaggerParseResult;
80-
import io.swagger.v3.parser.util.SchemaTypeUtil;
8180
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.*;
98-
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.*;
10681

10782
public class OpenAPIV3ParserTest {
10883
protected int serverPort = getDynamicPort();
@@ -476,7 +451,7 @@ public void testIssue1889_ArrayReferenceNull() {
476451
Assert.assertNotNull(result.getOpenAPI());
477452
OpenAPI openAPI = result.getOpenAPI();
478453
String expectedReference = openAPI.getPaths().get("/pets").getGet().getResponses().get("200").getContent()
479-
.get("application/json").getSchema().get$ref();
454+
.get("application/json").getSchema().getItems().get$ref();
480455
assertEquals(expectedReference, "#/components/schemas/Pet");
481456
}
482457

@@ -2332,9 +2307,9 @@ private OpenAPI doRelativeFileTest(String location) {
23322307
assertEquals(refInDefinitions.getDescription(), "The example model");
23332308
expectedPropertiesInModel(refInDefinitions, "foo", "bar");
23342309

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

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

0 commit comments

Comments
 (0)