Skip to content

Commit 8fa7861

Browse files
author
Scott Russell
committed
Issue #1170 - Modified ExternalRefProcessor and ResolverFully to load and resolve $ref within array items inline object.
1 parent 6c4f094 commit 8fa7861

File tree

5 files changed

+135
-9
lines changed

5 files changed

+135
-9
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,13 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
173173
}
174174

175175
}
176-
if (schema instanceof ArraySchema && ((ArraySchema) schema).getItems() != null && ((ArraySchema) schema).getItems().get$ref() != null
177-
&& StringUtils.isNotBlank(((ArraySchema) schema).getItems().get$ref())) {
178-
processRefSchema(((ArraySchema) schema).getItems(), file);
176+
if (schema instanceof ArraySchema && ((ArraySchema) schema).getItems() != null) {
177+
ArraySchema arraySchema = (ArraySchema) schema;
178+
if (StringUtils.isNotBlank(arraySchema.getItems().get$ref())) {
179+
processRefSchema(((ArraySchema) schema).getItems(), file);
180+
} else {
181+
processProperties(arraySchema.getItems().getProperties() ,file);
182+
}
179183
}
180184
}
181185

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,7 @@ public Schema resolveSchema(Schema schema) {
302302

303303
if(schema instanceof ArraySchema) {
304304
ArraySchema arrayModel = (ArraySchema) schema;
305-
if(arrayModel.getItems().get$ref() != null) {
306-
arrayModel.setItems(resolveSchema(arrayModel.getItems()));
307-
} else {
308-
arrayModel.setItems(arrayModel.getItems());
309-
}
310-
305+
arrayModel.setItems(resolveSchema(arrayModel.getItems()));
311306
return arrayModel;
312307
}
313308

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,51 @@ public void testIssue1161(@Injectable final List<AuthorizationValue> auths) {
539539
assertTrue(petsColouringProperty == colouringsSchema);
540540
}
541541

542+
@Test
543+
public void testIssue1170(@Injectable final List<AuthorizationValue> auths) {
544+
String path = "/issue-1170/swagger.yaml";
545+
546+
ParseOptions options = new ParseOptions();
547+
options.setResolve(true);
548+
options.setResolveFully(true);
549+
550+
OpenAPI openAPI = new OpenAPIV3Parser().readLocation(path, auths, options).getOpenAPI();
551+
552+
// Array schema with items $ref
553+
Schema breedsListSchema = openAPI.getComponents().getSchemas().get("BreedsList");
554+
Schema breedSchema = openAPI.getComponents().getSchemas().get("Breed");
555+
556+
assertNotNull(breedsListSchema);
557+
assertNotNull(breedSchema);
558+
559+
assertTrue(breedsListSchema instanceof ArraySchema);
560+
Schema breedPropertySchema = ((ArraySchema) breedsListSchema).getItems().getProperties().get("breed");
561+
assertNotNull(breedPropertySchema);
562+
563+
// Verify items resolved fully
564+
assertTrue(breedPropertySchema.get$ref() == null);
565+
assertTrue(breedPropertySchema == breedSchema);
566+
567+
568+
// Array schema with inline items object with $ref properties
569+
Schema petsListSchema = openAPI.getComponents().getSchemas().get("PetsList");
570+
Schema colouringsSchema = openAPI.getComponents().getSchemas().get("Colouring");
571+
Schema colourSchema = openAPI.getComponents().getSchemas().get("Colour");
572+
573+
assertNotNull(petsListSchema);
574+
assertNotNull(colouringsSchema);
575+
assertNotNull(colourSchema);
576+
577+
assertTrue(petsListSchema instanceof ArraySchema);
578+
Schema colouringPropertySchema = ((ArraySchema) petsListSchema).getItems().getProperties().get("colouring");
579+
assertNotNull(colouringPropertySchema);
580+
581+
// Verify inline items resolved fully
582+
assertTrue(colouringPropertySchema.get$ref() == null);
583+
assertTrue(colouringPropertySchema == colouringsSchema);
584+
585+
}
586+
542587
@Test
543588
public void selfReferenceTest(@Injectable final List<AuthorizationValue> auths) {
544589
String yaml = "" +
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
openapi: "3.0.2"
2+
info:
3+
version: 15.3.0
4+
title: "Common Data Types"
5+
paths: {}
6+
components:
7+
schemas:
8+
BreedsList:
9+
type: array
10+
items:
11+
$ref: '#/components/schemas/BreedsListItems'
12+
13+
BreedsListItems:
14+
type: object
15+
properties:
16+
breed:
17+
$ref: '#/components/schemas/Breed'
18+
19+
Breed:
20+
type: object
21+
properties:
22+
name:
23+
type: string
24+
family:
25+
type: string
26+
27+
PetsList:
28+
type: array
29+
items:
30+
type: object
31+
properties:
32+
petType:
33+
type: string
34+
colouring:
35+
$ref: '#/components/schemas/Colouring'
36+
37+
Colouring:
38+
type: object
39+
properties:
40+
primary:
41+
$ref: '#/components/schemas/Colour'
42+
secondary:
43+
$ref: '#/components/schemas/Colour'
44+
45+
Colour:
46+
type: string
47+
enum: ['black', 'white', 'tan', 'red', 'blue']
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
openapi: "3.0.2"
2+
info:
3+
version: 15.3.0
4+
title: test
5+
paths:
6+
/add-breeds:
7+
put:
8+
requestBody:
9+
content:
10+
application/json:
11+
schema:
12+
$ref: 'common.yaml#/components/schemas/BreedsList'
13+
responses:
14+
'200':
15+
description: Expected response to a valid request
16+
content:
17+
text/plain:
18+
schema:
19+
type: string
20+
21+
/add-pets:
22+
put:
23+
requestBody:
24+
content:
25+
application/json:
26+
schema:
27+
$ref: 'common.yaml#/components/schemas/PetsList'
28+
responses:
29+
'200':
30+
description: Expected response to a valid request
31+
content:
32+
text/plain:
33+
schema:
34+
type: string
35+

0 commit comments

Comments
 (0)