Skip to content

Commit 466f159

Browse files
committedDec 9, 2024
fix(swagger-api#2104): properly process inline refs in composed schemas
1 parent 153070c commit 466f159

File tree

5 files changed

+116
-10
lines changed

5 files changed

+116
-10
lines changed
 

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

+6-10
Original file line numberDiff line numberDiff line change
@@ -215,22 +215,18 @@ private void processComposedSchema(ComposedSchema composedSchema, String file) {
215215
if (composedSchema.getOneOf() != null) {
216216
for (Schema item : composedSchema.getOneOf()) {
217217
if (item.get$ref() != null) {
218-
if (item.get$ref() != null) {
219-
processRefSchema(item, file);
220-
} else {
221-
processSchema(item, file);
222-
}
218+
processRefSchema(item, file);
219+
} else {
220+
processSchema(item, file);
223221
}
224222
}
225223
}
226224
if (composedSchema.getAnyOf() != null) {
227225
for (Schema item : composedSchema.getAnyOf()) {
228226
if (item.get$ref() != null) {
229-
if (item.get$ref() != null) {
230-
processRefSchema(item, file);
231-
} else {
232-
processSchema(item, file);
233-
}
227+
processRefSchema(item, file);
228+
} else {
229+
processSchema(item, file);
234230
}
235231
}
236232
}

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

+25
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,29 @@ public void testHandleComposedSchemasInArrayItems() {
229229
assertTrue(components.containsKey("OrderRowType"));
230230
}
231231

232+
@Test
233+
public void testHandleInlineRefsInComposedSchemas() {
234+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
235+
ParseOptions options = new ParseOptions();
236+
options.setResolve(true);
237+
SwaggerParseResult parseResult = openApiParser.readLocation("issue-2104/openapi.yaml", null, options);
238+
OpenAPI openAPI = parseResult.getOpenAPI();
239+
240+
Map<String, Schema> components = openAPI.getComponents().getSchemas();
241+
assertEquals(components.size(), 4);
242+
assertTrue(components.containsKey("ResponseAllOf"));
243+
assertTrue(components.containsKey("ResponseOneOf"));
244+
assertTrue(components.containsKey("ResponseAnyOf"));
245+
assertTrue(components.containsKey("Product"));
246+
247+
Schema allOfInlineProduct = (Schema)((Schema)components.get("ResponseAllOf").getAllOf().get(1)).getProperties().get("product");
248+
assertThat(allOfInlineProduct.get$ref(), is("#/components/schemas/Product"));
249+
250+
Schema oneOfInlineProduct = (Schema)((Schema)components.get("ResponseOneOf").getOneOf().get(1)).getProperties().get("product");
251+
assertThat(oneOfInlineProduct.get$ref(), is("#/components/schemas/Product"));
252+
253+
Schema anyOfInlineProduct = (Schema)((Schema)components.get("ResponseAnyOf").getAnyOf().get(1)).getProperties().get("product");
254+
assertThat(anyOfInlineProduct.get$ref(), is("#/components/schemas/Product"));
255+
}
256+
232257
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
components:
2+
schemas:
3+
ResponseAllOf:
4+
allOf:
5+
- $ref: '../depth2/product/definitions.yaml#/components/schemas/Product'
6+
- type: object
7+
properties:
8+
product:
9+
$ref: '../depth2/product/definitions.yaml#/components/schemas/Product'
10+
ResponseOneOf:
11+
oneOf:
12+
- $ref: '../depth2/product/definitions.yaml#/components/schemas/Product'
13+
- type: object
14+
properties:
15+
product:
16+
$ref: '../depth2/product/definitions.yaml#/components/schemas/Product'
17+
ResponseAnyOf:
18+
anyOf:
19+
- $ref: '../depth2/product/definitions.yaml#/components/schemas/Product'
20+
- type: object
21+
properties:
22+
product:
23+
$ref: '../depth2/product/definitions.yaml#/components/schemas/Product'
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
components:
2+
schemas:
3+
Product:
4+
type: object
5+
additionalProperties: false
6+
required:
7+
- type
8+
properties:
9+
id:
10+
type: string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
openapi: 3.0.1
2+
info:
3+
title: API
4+
description: API
5+
version: LATEST
6+
paths:
7+
/test-all-of:
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: './depth1/depth2/definitions.yaml#/components/schemas/ResponseAllOf'
22+
/test-one-of:
23+
post:
24+
requestBody:
25+
required: true
26+
content:
27+
text/plain:
28+
schema:
29+
type: string
30+
responses:
31+
200:
32+
description: OK
33+
content:
34+
application/json:
35+
schema:
36+
$ref: './depth1/depth2/definitions.yaml#/components/schemas/ResponseOneOf'
37+
/test-any-of:
38+
post:
39+
requestBody:
40+
required: true
41+
content:
42+
text/plain:
43+
schema:
44+
type: string
45+
responses:
46+
200:
47+
description: OK
48+
content:
49+
application/json:
50+
schema:
51+
$ref: './depth1/depth2/definitions.yaml#/components/schemas/ResponseAnyOf'

0 commit comments

Comments
 (0)
Please sign in to comment.