Skip to content

Commit 974cd15

Browse files
authored
Merge pull request #1132 from fractaloop/issue-1131
Resolve request body examples
2 parents 1f56ef3 + 2f92445 commit 974cd15

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ protected void updateLocalRefs(RequestBody body, String pathRef) {
225225
if (mediaType.getSchema() != null) {
226226
updateLocalRefs(mediaType.getSchema(), pathRef);
227227
}
228+
Map<String, Example> examples = content.get(key).getExamples();
229+
if (examples != null) {
230+
for (Example example : examples.values()) {
231+
updateLocalRefs(example, pathRef);
232+
}
233+
}
228234
}
229235
}else if(body.get$ref() != null){
230236

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.swagger.v3.parser.processors;
22

33
import io.swagger.v3.oas.models.OpenAPI;
4+
import io.swagger.v3.oas.models.examples.Example;
45
import io.swagger.v3.oas.models.media.MediaType;
56
import io.swagger.v3.oas.models.media.Schema;
67
import io.swagger.v3.oas.models.parameters.RequestBody;
@@ -18,11 +19,13 @@
1819
public class RequestBodyProcessor {
1920
private final SchemaProcessor schemaProcessor;
2021
private final ExternalRefProcessor externalRefProcessor;
22+
private final ExampleProcessor exampleProcessor;
2123
private final ResolverCache cache;
2224
private final OpenAPI openAPI;
2325

2426
public RequestBodyProcessor(ResolverCache cache, OpenAPI openAPI) {
2527
schemaProcessor = new SchemaProcessor(cache,openAPI);
28+
exampleProcessor = new ExampleProcessor(cache,openAPI);
2629
this.externalRefProcessor = new ExternalRefProcessor(cache, openAPI);
2730
this.cache = cache;
2831
this.openAPI = openAPI;
@@ -44,6 +47,11 @@ public void processRequestBody(RequestBody requestBody) {
4447
schemaProcessor.processSchema(schema);
4548
}
4649
}
50+
if(mediaType.getExamples() != null) {
51+
for(Example ex: mediaType.getExamples().values()){
52+
exampleProcessor.processExample(ex);
53+
}
54+
}
4755
}
4856
}
4957
}

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public void testIssue853() {
226226
ParseOptions options = new ParseOptions();
227227
options.setResolve(true);
228228
options.setFlatten(true);
229-
final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-837-853/main.yaml", null, options).getOpenAPI();
229+
final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-837-853-1131/main.yaml", null, options).getOpenAPI();
230230

231231
Assert.assertNotNull(openAPI);
232232

@@ -249,7 +249,7 @@ public void testIssue853() {
249249
public void testIssue837() {
250250
ParseOptions options = new ParseOptions();
251251
options.setResolve(true);
252-
final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-837-853/main.yaml", null, options).getOpenAPI();
252+
final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-837-853-1131/main.yaml", null, options).getOpenAPI();
253253

254254
Assert.assertNotNull(openAPI);
255255

@@ -263,6 +263,38 @@ public void testIssue837() {
263263
Assert.assertEquals(examples.get("external").get$ref(), "#/components/examples/ExternalRef");
264264
}
265265

266+
@Test
267+
public void testIssue1131() {
268+
ParseOptions options = new ParseOptions();
269+
options.setResolve(true);
270+
final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-837-853-1131/main.yaml", null, options).getOpenAPI();
271+
272+
Assert.assertNotNull(openAPI);
273+
274+
Content content = openAPI.getPaths().get("/events").getGet().getRequestBody().getContent();
275+
Assert.assertNotNull(content);
276+
277+
Map<String, Example> examples = content.get("application/json").getExamples();
278+
Assert.assertEquals(examples.size(), 3);
279+
Assert.assertEquals(((ObjectNode) examples.get("plain").getValue()).get("test").asText(), "plain");
280+
Assert.assertEquals(examples.get("local").get$ref(), "#/components/examples/LocalRef");
281+
Assert.assertEquals(examples.get("external").get$ref(), "#/components/examples/ExternalRef");
282+
283+
// Also cover the case from Issue 853
284+
Operation post = openAPI.getPaths().get("/guests").getPost();
285+
Assert.assertNotNull(post);
286+
287+
content = post.getRequestBody().getContent();
288+
Assert.assertNotNull(content);
289+
290+
examples = content.get("application/json").getExamples();
291+
Assert.assertEquals(examples.size(), 1);
292+
assertNotNull(openAPI.getComponents());
293+
assertNotNull(openAPI.getComponents().getExamples());
294+
assertNotNull(openAPI.getComponents().getExamples().get("testExample"));
295+
assertEquals(((LinkedHashMap<String, Object>)openAPI.getComponents().getExamples().get("testExample").getValue()).get("test"),"value");
296+
}
297+
266298
@Test
267299
public void testIssue834() {
268300
ParseOptions options = new ParseOptions();

modules/swagger-parser-v3/src/test/resources/issue-837-853/external-operation.yaml renamed to modules/swagger-parser-v3/src/test/resources/issue-837-853-1131/external-operation.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
post:
22
description: add a guest
33
operationId: addGuest
4+
requestBody:
5+
content:
6+
application/json:
7+
schema:
8+
properties:
9+
test:
10+
type: string
11+
examples:
12+
testExample:
13+
$ref: '#/components/examples/testExample'
414
responses:
515
'201':
616
description: New guest added to event list

modules/swagger-parser-v3/src/test/resources/issue-837-853/main.yaml renamed to modules/swagger-parser-v3/src/test/resources/issue-837-853-1131/main.yaml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ paths:
99
get:
1010
description: A list of events
1111
operationId: getEvents
12+
requestBody:
13+
content:
14+
application/json:
15+
schema:
16+
required:
17+
- test
18+
type: object
19+
properties:
20+
test:
21+
type: string
22+
examples:
23+
plain:
24+
summary: plain example
25+
value:
26+
test: plain
27+
local:
28+
$ref: '#/components/examples/LocalRef'
29+
external:
30+
$ref: './issue-837-853-1131/components.yaml#/components/examples/ExternalRef'
1231
responses:
1332
'200':
1433
description: OK
@@ -29,9 +48,9 @@ paths:
2948
local:
3049
$ref: '#/components/examples/LocalRef'
3150
external:
32-
$ref: './issue-837-853/components.yaml#/components/examples/ExternalRef'
51+
$ref: './issue-837-853-1131/components.yaml#/components/examples/ExternalRef'
3352
/guests:
34-
$ref: './issue-837-853/external-operation.yaml'
53+
$ref: './issue-837-853-1131/external-operation.yaml'
3554

3655
components:
3756
examples:

0 commit comments

Comments
 (0)