Skip to content

Commit 96241cb

Browse files
chimmifrantuma
authored andcommitted
Issue 2071. Correctly handle external refs in array items composed schemas
1 parent 6c22e31 commit 96241cb

File tree

4 files changed

+106
-31
lines changed

4 files changed

+106
-31
lines changed

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

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -147,36 +147,7 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
147147

148148
if(schema instanceof ComposedSchema){
149149
ComposedSchema composedSchema = (ComposedSchema) schema;
150-
if (composedSchema.getAllOf() != null){
151-
for(Schema item : composedSchema.getAllOf()){
152-
if (item.get$ref() != null){
153-
processRefSchema(item,file);
154-
} else{
155-
processSchema(item, file);
156-
}
157-
}
158-
159-
}if (composedSchema.getOneOf() != null){
160-
for(Schema item : composedSchema.getOneOf()){
161-
if (item.get$ref() != null){
162-
if (item.get$ref() != null){
163-
processRefSchema(item,file);
164-
}else{
165-
processSchema(item, file);
166-
}
167-
}
168-
}
169-
}if (composedSchema.getAnyOf() != null){
170-
for(Schema item : composedSchema.getAnyOf()){
171-
if (item.get$ref() != null){
172-
if (item.get$ref() != null){
173-
processRefSchema(item,file);
174-
}else{
175-
processSchema(item, file);
176-
}
177-
}
178-
}
179-
}
150+
processComposedSchema(composedSchema, file);
180151
}
181152
//Loop the properties and recursively call this method;
182153
Map<String, Schema> subProps = schema.getProperties();
@@ -212,14 +183,52 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
212183
ArraySchema arraySchema = (ArraySchema) schema;
213184
if (StringUtils.isNotBlank(arraySchema.getItems().get$ref())) {
214185
processRefSchema(((ArraySchema) schema).getItems(), file);
186+
} else if (arraySchema.getItems() instanceof ComposedSchema) {
187+
ComposedSchema composedSchema = (ComposedSchema) arraySchema.getItems();
188+
processComposedSchema(composedSchema, file);
215189
} else {
216-
processProperties(arraySchema.getItems().getProperties() ,file);
190+
processProperties(arraySchema.getItems().getProperties(), file);
217191
}
218192
}
219193
}
220194
return newRef;
221195
}
222196

197+
private void processComposedSchema(ComposedSchema composedSchema, String file) {
198+
if (composedSchema.getAllOf() != null) {
199+
for (Schema item : composedSchema.getAllOf()) {
200+
if (item.get$ref() != null) {
201+
processRefSchema(item, file);
202+
} else {
203+
processSchema(item, file);
204+
}
205+
}
206+
207+
}
208+
if (composedSchema.getOneOf() != null) {
209+
for (Schema item : composedSchema.getOneOf()) {
210+
if (item.get$ref() != null) {
211+
if (item.get$ref() != null) {
212+
processRefSchema(item, file);
213+
} else {
214+
processSchema(item, file);
215+
}
216+
}
217+
}
218+
}
219+
if (composedSchema.getAnyOf() != null) {
220+
for (Schema item : composedSchema.getAnyOf()) {
221+
if (item.get$ref() != null) {
222+
if (item.get$ref() != null) {
223+
processRefSchema(item, file);
224+
} else {
225+
processSchema(item, file);
226+
}
227+
}
228+
}
229+
}
230+
}
231+
223232
private void processSchema(Schema property, String file) {
224233
if (property != null) {
225234
if (StringUtils.isNotBlank(property.get$ref())) {

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/processors/ExternalRefProcessorTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
import io.swagger.v3.oas.models.OpenAPI;
66
import io.swagger.v3.oas.models.media.Schema;
77
import io.swagger.v3.oas.models.media.StringSchema;
8+
import io.swagger.v3.parser.OpenAPIV3Parser;
89
import io.swagger.v3.parser.ResolverCache;
910
import io.swagger.v3.parser.core.models.AuthorizationValue;
11+
import io.swagger.v3.parser.core.models.ParseOptions;
12+
import io.swagger.v3.parser.core.models.SwaggerParseResult;
1013
import io.swagger.v3.parser.models.RefFormat;
1114
import io.swagger.v3.parser.util.RemoteUrl;
1215
import mockit.Expectations;
1316
import mockit.Injectable;
1417
import mockit.Mocked;
1518
import mockit.Expectations;
19+
import org.testng.Assert;
1620
import org.testng.annotations.Test;
1721

1822
import java.util.ArrayList;
@@ -23,6 +27,7 @@
2327
import static org.hamcrest.CoreMatchers.is;
2428
import static org.hamcrest.CoreMatchers.notNullValue;
2529
import static org.junit.Assert.assertThat;
30+
import static org.junit.Assert.assertTrue;
2631
import static org.testng.Assert.assertEquals;
2732

2833

@@ -206,4 +211,19 @@ public void testRelativeRefIncludingUrlRef()
206211
is("./relative-with-url/relative-with-local.yaml#/relative-same-file")
207212
);
208213
}
214+
215+
@Test
216+
public void testHandleComposedSchemasInArrayItems() {
217+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
218+
ParseOptions options = new ParseOptions();
219+
options.setResolve(true);
220+
SwaggerParseResult parseResult = openApiParser.readLocation("issue-2071/openapi.yaml", null, options);
221+
OpenAPI openAPI = parseResult.getOpenAPI();
222+
223+
assertEquals(openAPI.getComponents().getSchemas().size(), 3);
224+
assertTrue(openAPI.getComponents().getSchemas().containsKey("Response"));
225+
assertTrue(openAPI.getComponents().getSchemas().containsKey("ProductRow"));
226+
assertTrue(openAPI.getComponents().getSchemas().containsKey("ProductsRowType"));
227+
}
228+
209229
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
components:
2+
schemas:
3+
Response:
4+
type: array
5+
items:
6+
oneOf:
7+
- $ref: '#/components/schemas/ProductRow'
8+
discriminator:
9+
propertyName: type
10+
mapping:
11+
'product': '#/components/schemas/ProductRow'
12+
ProductRow:
13+
type: object
14+
additionalProperties: false
15+
required:
16+
- type
17+
properties:
18+
type:
19+
$ref: '#/components/schemas/ProductsRowType'
20+
payload:
21+
type: string
22+
ProductsRowType:
23+
type: string
24+
enum:
25+
- product
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
openapi: 3.0.1
2+
info:
3+
title: API
4+
description: API
5+
version: LATEST
6+
paths:
7+
/test-path:
8+
post:
9+
requestBody:
10+
required: true
11+
content:
12+
text/plain:
13+
schema:
14+
type: string
15+
responses:
16+
200:
17+
description: OK
18+
content:
19+
application/json:
20+
schema:
21+
$ref: 'definitions.yaml#/components/schemas/Response'

0 commit comments

Comments
 (0)