Repository: swagger-api/swagger-parser (java)

Issue:
Examples with Ref not parsed when in request bodies
The same as #837, but for request bodies.

```yaml
requestBody:
  content:
    application/json:
      examples:
         key:
           $ref: 'file.yaml#components/examples/key'
```

Pull request:
Resolve request body examples
This reproduces and resolves #1131, including cases for #853 and #837.

Patch:
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java
index 2e3c4d61..8db11597 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java
@@ -225,6 +225,12 @@ public class PathsProcessor {
                 if (mediaType.getSchema() != null) {
                     updateLocalRefs(mediaType.getSchema(), pathRef);
                 }
+                Map<String, Example> examples = content.get(key).getExamples();
+                if (examples != null) {
+                    for (Example example : examples.values()) {
+                        updateLocalRefs(example, pathRef);
+                    }
+                }
             }
         }else if(body.get$ref() != null){
 
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/RequestBodyProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/RequestBodyProcessor.java
index 34f95477..ce1caa02 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/RequestBodyProcessor.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/RequestBodyProcessor.java
@@ -1,6 +1,7 @@
 package io.swagger.v3.parser.processors;
 
 import io.swagger.v3.oas.models.OpenAPI;
+import io.swagger.v3.oas.models.examples.Example;
 import io.swagger.v3.oas.models.media.MediaType;
 import io.swagger.v3.oas.models.media.Schema;
 import io.swagger.v3.oas.models.parameters.RequestBody;
@@ -18,11 +19,13 @@ import static io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat;
 public class RequestBodyProcessor {
     private final SchemaProcessor schemaProcessor;
     private final ExternalRefProcessor externalRefProcessor;
+    private final ExampleProcessor exampleProcessor;
     private final ResolverCache cache;
     private final OpenAPI openAPI;
 
     public RequestBodyProcessor(ResolverCache cache, OpenAPI openAPI) {
         schemaProcessor = new SchemaProcessor(cache,openAPI);
+        exampleProcessor = new ExampleProcessor(cache,openAPI);
         this.externalRefProcessor = new ExternalRefProcessor(cache, openAPI);
         this.cache = cache;
         this.openAPI = openAPI;
@@ -44,6 +47,11 @@ public class RequestBodyProcessor {
                         schemaProcessor.processSchema(schema);
                     }
                 }
+                if(mediaType.getExamples() != null) {
+                    for(Example ex: mediaType.getExamples().values()){
+                        exampleProcessor.processExample(ex);
+                    }
+                }
             }
         }
     }


Test patch:
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
index ce0835c6..9220c01d 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
@@ -226,7 +226,7 @@ public class OpenAPIV3ParserTest {
         ParseOptions options = new ParseOptions();
         options.setResolve(true);
         options.setFlatten(true);
-        final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-837-853/main.yaml", null, options).getOpenAPI();
+        final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-837-853-1131/main.yaml", null, options).getOpenAPI();
 
         Assert.assertNotNull(openAPI);
 
@@ -249,7 +249,7 @@ public class OpenAPIV3ParserTest {
     public void testIssue837() {
         ParseOptions options = new ParseOptions();
         options.setResolve(true);
-        final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-837-853/main.yaml", null, options).getOpenAPI();
+        final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-837-853-1131/main.yaml", null, options).getOpenAPI();
 
         Assert.assertNotNull(openAPI);
 
@@ -263,6 +263,38 @@ public class OpenAPIV3ParserTest {
         Assert.assertEquals(examples.get("external").get$ref(), "#/components/examples/ExternalRef");
     }
 
+    @Test
+    public void testIssue1131() {
+        ParseOptions options = new ParseOptions();
+        options.setResolve(true);
+        final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-837-853-1131/main.yaml", null, options).getOpenAPI();
+
+        Assert.assertNotNull(openAPI);
+
+        Content content = openAPI.getPaths().get("/events").getGet().getRequestBody().getContent();
+        Assert.assertNotNull(content);
+
+        Map<String, Example> examples = content.get("application/json").getExamples();
+        Assert.assertEquals(examples.size(), 3);
+        Assert.assertEquals(((ObjectNode) examples.get("plain").getValue()).get("test").asText(), "plain");
+        Assert.assertEquals(examples.get("local").get$ref(), "#/components/examples/LocalRef");
+        Assert.assertEquals(examples.get("external").get$ref(), "#/components/examples/ExternalRef");
+
+        // Also cover the case from Issue 853
+        Operation post = openAPI.getPaths().get("/guests").getPost();
+        Assert.assertNotNull(post);
+
+        content = post.getRequestBody().getContent();
+        Assert.assertNotNull(content);
+
+        examples = content.get("application/json").getExamples();
+        Assert.assertEquals(examples.size(), 1);
+        assertNotNull(openAPI.getComponents());
+        assertNotNull(openAPI.getComponents().getExamples());
+        assertNotNull(openAPI.getComponents().getExamples().get("testExample"));
+        assertEquals(((LinkedHashMap<String, Object>)openAPI.getComponents().getExamples().get("testExample").getValue()).get("test"),"value");
+    }
+
     @Test
     public void testIssue834() {
         ParseOptions options = new ParseOptions();
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-837-853/components.yaml b/modules/swagger-parser-v3/src/test/resources/issue-837-853-1131/components.yaml
similarity index 100%
rename from modules/swagger-parser-v3/src/test/resources/issue-837-853/components.yaml
rename to modules/swagger-parser-v3/src/test/resources/issue-837-853-1131/components.yaml
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-837-853/external-operation.yaml b/modules/swagger-parser-v3/src/test/resources/issue-837-853-1131/external-operation.yaml
similarity index 65%
rename from modules/swagger-parser-v3/src/test/resources/issue-837-853/external-operation.yaml
rename to modules/swagger-parser-v3/src/test/resources/issue-837-853-1131/external-operation.yaml
index 38ce0c2d..30fcdb82 100644
--- a/modules/swagger-parser-v3/src/test/resources/issue-837-853/external-operation.yaml
+++ b/modules/swagger-parser-v3/src/test/resources/issue-837-853-1131/external-operation.yaml
@@ -1,6 +1,16 @@
 post:
   description: add a guest
   operationId: addGuest
+  requestBody:
+    content:
+      application/json:
+        schema:
+          properties:
+            test:
+              type: string
+        examples:
+          testExample:
+            $ref: '#/components/examples/testExample'
   responses:
     '201':
       description: New guest added to event list
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-837-853/main.yaml b/modules/swagger-parser-v3/src/test/resources/issue-837-853-1131/main.yaml
similarity index 54%
rename from modules/swagger-parser-v3/src/test/resources/issue-837-853/main.yaml
rename to modules/swagger-parser-v3/src/test/resources/issue-837-853-1131/main.yaml
index 68b6ca06..78521076 100644
--- a/modules/swagger-parser-v3/src/test/resources/issue-837-853/main.yaml
+++ b/modules/swagger-parser-v3/src/test/resources/issue-837-853-1131/main.yaml
@@ -9,6 +9,25 @@ paths:
     get:
       description: A list of events
       operationId: getEvents
+      requestBody:
+        content:
+          application/json:
+            schema:
+              required:
+                - test
+              type: object
+              properties:
+                test:
+                  type: string
+            examples:
+              plain:
+                summary: plain example
+                value:
+                  test: plain
+              local:
+                $ref: '#/components/examples/LocalRef'
+              external:
+                $ref: './issue-837-853-1131/components.yaml#/components/examples/ExternalRef'
       responses:
         '200':
           description: OK
@@ -29,9 +48,9 @@ paths:
                 local:
                   $ref: '#/components/examples/LocalRef'
                 external:
-                  $ref: './issue-837-853/components.yaml#/components/examples/ExternalRef'
+                  $ref: './issue-837-853-1131/components.yaml#/components/examples/ExternalRef'
   /guests:
-    $ref: './issue-837-853/external-operation.yaml'
+    $ref: './issue-837-853-1131/external-operation.yaml'
 
 components:
   examples: