Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit a3d3a42

Browse files
authored
Merge pull request swagger-api#996 from douglasbgray/issue-975
Fix for issue 975
2 parents a4a0667 + f80b2ba commit a3d3a42

5 files changed

Lines changed: 246 additions & 30 deletions

File tree

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

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.slf4j.LoggerFactory;
2525

2626
import java.net.URI;
27+
import java.util.Collection;
2728
import java.util.LinkedHashMap;
2829
import java.util.Map;
2930

@@ -177,37 +178,40 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
177178
return newRef;
178179
}
179180

180-
private void processProperties(Map<String,Schema> subProps, String file) {
181-
if (subProps != null) {
182-
for (Map.Entry<String, Schema> prop : subProps.entrySet()) {
183-
if (prop.getValue().get$ref() != null) {
184-
processRefSchema(prop.getValue(), file);
185-
} else if (prop.getValue() instanceof ArraySchema) {
186-
ArraySchema arrayProp = (ArraySchema) prop.getValue();
187-
if (arrayProp.getItems() != null && arrayProp.getItems().get$ref() != null &&
188-
StringUtils.isNotBlank(arrayProp.getItems().get$ref())) {
189-
processRefSchema(arrayProp.getItems(), file);
190-
}
191-
if (arrayProp.getItems() != null && arrayProp.getItems().getProperties() != null ) {
192-
processProperties(arrayProp.getItems().getProperties(), file);
193-
}
194-
} else if (prop.getValue().getAdditionalProperties() != null && prop.getValue().getAdditionalProperties() instanceof Schema) {
195-
Schema mapProp = (Schema) prop.getValue().getAdditionalProperties();
196-
if (mapProp.get$ref() != null) {
197-
processRefSchema(mapProp, file);
198-
} else if (mapProp.getAdditionalProperties() instanceof ArraySchema &&
199-
((ArraySchema) mapProp.getAdditionalProperties()).getItems()!= null &&
200-
((ArraySchema) mapProp.getAdditionalProperties()).getItems().get$ref() != null
201-
&& StringUtils.isNotBlank(((ArraySchema) mapProp.getAdditionalProperties()).getItems().get$ref())) {
202-
processRefSchema(((ArraySchema) mapProp.getAdditionalProperties()).getItems(), file);
203-
}
204-
}else if (prop.getValue() instanceof ObjectSchema){
205-
ObjectSchema objProp = (ObjectSchema) prop.getValue();
206-
if(objProp.getProperties() != null ){
207-
processProperties(objProp.getProperties(),file);
208-
}
209-
}
181+
private void processProperty(Schema property, String file) {
182+
if (property != null) {
183+
if (StringUtils.isNotBlank(property.get$ref())) {
184+
processRefSchema(property, file);
185+
}
186+
if (property.getProperties() != null) {
187+
processProperties(property.getProperties(), file);
210188
}
189+
if (property instanceof ArraySchema) {
190+
processProperty(((ArraySchema) property).getItems(), file);
191+
}
192+
if (property.getAdditionalProperties() instanceof Schema) {
193+
processProperty(((Schema) property.getAdditionalProperties()), file);
194+
}
195+
if (property instanceof ComposedSchema) {
196+
ComposedSchema composed = (ComposedSchema) property;
197+
processProperties(composed.getAllOf(), file);
198+
processProperties(composed.getAnyOf(), file);
199+
processProperties(composed.getOneOf(), file);
200+
}
201+
}
202+
}
203+
204+
private void processProperties(Collection<Schema> properties, String file) {
205+
if (properties != null) {
206+
for (Schema property : properties) {
207+
processProperty(property, file);
208+
}
209+
}
210+
}
211+
212+
private void processProperties(Map<String, Schema> properties, String file) {
213+
if (properties != null) {
214+
processProperties(properties.values(), file);
211215
}
212216
}
213217

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,87 @@ public void testLinkIssue() {
16631663
assertEquals(requestBody, "$response.body#/slug");
16641664
}
16651665

1666+
@SuppressWarnings("unchecked")
1667+
private Map<String, Schema> issue975ExtractPropertiesFromTestResource() {
1668+
ParseOptions options = new ParseOptions();
1669+
options.setResolve(true);
1670+
OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-975/contract/openapi.yaml", null, options).getOpenAPI();
1671+
Schema myResponseSchema = openAPI.getComponents().getSchemas().get("MyResponse");
1672+
return myResponseSchema.getProperties();
1673+
}
1674+
1675+
@Test(description = "Test that relative references are resolvable when property is a reference to a relative file.")
1676+
public void testIssue975_property() {
1677+
Map<String, Schema> properties = issue975ExtractPropertiesFromTestResource();
1678+
assertEquals(properties.get("images").get$ref(), "#/components/schemas/Image");
1679+
}
1680+
1681+
@Test(description = "Test that relative references are resolvable when property is an array with a reference to a relative file.")
1682+
public void testIssue975_array() {
1683+
Map<String, Schema> properties = issue975ExtractPropertiesFromTestResource();
1684+
ArraySchema imagesArray = (ArraySchema) properties.get("imagesArray");
1685+
assertEquals(imagesArray.getItems().get$ref(), "#/components/schemas/Image");
1686+
}
1687+
1688+
@Test(description = "Test that relative references are resolvable when property is a map with a reference to a relative file.")
1689+
public void testIssue975_map() {
1690+
Map<String, Schema> properties = issue975ExtractPropertiesFromTestResource();
1691+
Schema imagesMap = (Schema) properties.get("imagesMap").getAdditionalProperties();
1692+
assertEquals(imagesMap.get$ref(), "#/components/schemas/Image");
1693+
}
1694+
1695+
@Test(description = "Test that relative references are resolvable when property is an array with a map with a reference to a relative file.")
1696+
public void testIssue975_array_map() {
1697+
Map<String, Schema> properties = issue975ExtractPropertiesFromTestResource();
1698+
ArraySchema imagesArray = (ArraySchema) properties.get("imagesArrayMap");
1699+
Schema imagesdMap = (Schema) imagesArray.getItems().getAdditionalProperties();
1700+
assertEquals(imagesdMap.get$ref(), "#/components/schemas/Image");
1701+
}
1702+
1703+
@Test(description = "Test that relative references are resolvable when property is a map with an array with a reference to a relative file.")
1704+
public void testIssue975_map_array() {
1705+
Map<String, Schema> properties = issue975ExtractPropertiesFromTestResource();
1706+
ArraySchema imagesArray = (ArraySchema) properties.get("imagesMapArray").getAdditionalProperties();
1707+
assertEquals(imagesArray.getItems().get$ref(), "#/components/schemas/Image");
1708+
}
1709+
1710+
@Test(description = "Test that relative references are resolvable when property is an array with an array with a reference to a relative file.")
1711+
public void testIssue975_array_array() {
1712+
Map<String, Schema> properties = issue975ExtractPropertiesFromTestResource();
1713+
ArraySchema imagesArray = (ArraySchema) properties.get("imagesArrayArray");
1714+
imagesArray = (ArraySchema) imagesArray.getItems();
1715+
assertEquals(imagesArray.getItems().get$ref(), "#/components/schemas/Image");
1716+
}
1717+
1718+
@Test(description = "Test that relative references are resolvable when property is a map with a map with a reference to a relative file.")
1719+
public void testIssue975_map_map() {
1720+
Map<String, Schema> properties = issue975ExtractPropertiesFromTestResource();
1721+
Schema imagesMap = (Schema) properties.get("imagesMapMap").getAdditionalProperties();
1722+
imagesMap = (Schema) imagesMap.getAdditionalProperties();
1723+
assertEquals(imagesMap.get$ref(), "#/components/schemas/Image");
1724+
}
1725+
1726+
@Test(description = "Test that relative references are resolvable when property is an oneOf a reference to a relative file.")
1727+
public void testIssue975_oneOf() {
1728+
Map<String, Schema> properties = issue975ExtractPropertiesFromTestResource();
1729+
ComposedSchema composed = (ComposedSchema) properties.get("oneOfExample");
1730+
assertEquals(composed.getOneOf().get(0).get$ref(), "#/components/schemas/Image");
1731+
}
1732+
1733+
@Test(description = "Test that relative references are resolvable when property is an anyOf a reference to a relative file.")
1734+
public void testIssue975_anyOf() {
1735+
Map<String, Schema> properties = issue975ExtractPropertiesFromTestResource();
1736+
ComposedSchema composed = (ComposedSchema) properties.get("anyOfExample");
1737+
assertEquals(composed.getAnyOf().get(0).get$ref(), "#/components/schemas/Image");
1738+
}
1739+
1740+
@Test(description = "Test that relative references are resolvable when property is an allOf a reference to a relative file.")
1741+
public void testIssue975_allOf() {
1742+
Map<String, Schema> properties = issue975ExtractPropertiesFromTestResource();
1743+
ComposedSchema composed = (ComposedSchema) properties.get("allOfExample");
1744+
assertEquals(composed.getAllOf().get(0).get$ref(), "#/components/schemas/Image");
1745+
}
1746+
16661747
private static int getDynamicPort() {
16671748
return new Random().ints(10000, 20000).findFirst().getAsInt();
16681749
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Common Image Type
4+
version: 5.0.0
5+
paths:
6+
/foo:
7+
get:
8+
summary: Common JSON objects
9+
responses:
10+
'200':
11+
description: Success
12+
content:
13+
application/json:
14+
schema:
15+
$ref: '../types/responses.yaml#/components/schemas/MyResponse'
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Common Image Type
4+
version: 5.0.0
5+
paths:
6+
/foo:
7+
get:
8+
summary: Common JSON objects
9+
responses:
10+
'204':
11+
description: Success
12+
13+
components:
14+
schemas:
15+
MyResponse:
16+
type: object
17+
properties:
18+
19+
# Expected property type = Image
20+
images:
21+
$ref: './types.yaml#/components/schemas/Image'
22+
23+
# Expected property type = List<Image>
24+
imagesArray:
25+
type: array
26+
items:
27+
$ref: './types.yaml#/components/schemas/Image'
28+
29+
# Expected property type = Map<String, Image>
30+
imagesMap:
31+
type: object
32+
additionalProperties:
33+
$ref: './types.yaml#/components/schemas/Image'
34+
35+
# Expected property type = List<Map<String, Image>>
36+
imagesArrayMap:
37+
type: array
38+
items:
39+
type: object
40+
additionalProperties:
41+
$ref: './types.yaml#/components/schemas/Image'
42+
43+
# Expected property type = Map<String, List<Image>>
44+
imagesMapArray:
45+
type: object
46+
additionalProperties:
47+
type: array
48+
items:
49+
$ref: './types.yaml#/components/schemas/Image'
50+
51+
# Expected property type = List<List<Image>>
52+
imagesArrayArray:
53+
type: array
54+
items:
55+
type: array
56+
items:
57+
$ref: './types.yaml#/components/schemas/Image'
58+
59+
# Expected property type = Map<String, Map<String, Image>>
60+
imagesMapMap:
61+
type: object
62+
additionalProperties:
63+
type: object
64+
additionalProperties:
65+
$ref: './types.yaml#/components/schemas/Image'
66+
67+
# Expected property type = Object
68+
oneOfExample:
69+
type: object
70+
oneOf:
71+
- $ref: './types.yaml#/components/schemas/Image'
72+
- type: object
73+
properties:
74+
localField:
75+
type: string
76+
77+
# Expected property type = Object
78+
anyOfExample:
79+
type: object
80+
anyOf:
81+
- $ref: './types.yaml#/components/schemas/Image'
82+
- type: object
83+
properties:
84+
localField:
85+
type: string
86+
87+
# Expected property type = Object
88+
allOfExample:
89+
type: object
90+
allOf:
91+
- $ref: './types.yaml#/components/schemas/Image'
92+
- type: object
93+
properties:
94+
localField:
95+
type: string
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Common Image Type
4+
version: 5.0.0
5+
paths:
6+
/foo:
7+
get:
8+
summary: Common JSON objects
9+
responses:
10+
'204':
11+
description: Success
12+
13+
components:
14+
schemas:
15+
Image:
16+
type: object
17+
properties:
18+
title:
19+
type: string
20+
URL:
21+
type: string

0 commit comments

Comments
 (0)