Repository: swagger-api/swagger-parser
Pull request #1336 (2020-03-10 22:28:53)

fix for issue #1335 - OAS3: Resolved YAML doesn't include Example components from domains
Fix for issue #1335 -  OAS3: Resolved YAML doesn't include Example components from domains


Patch:
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java
index fdbeb377..02d63f1e 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java
@@ -721,6 +721,42 @@ public final class ExternalRefProcessor {
             if(mediaType.getSchema() != null) {
                 processRefSchemaObject(mediaType.getSchema(), $ref);
             }
+            if(mediaType.getExamples() != null) {
+                processRefExamples(mediaType.getExamples(), $ref);
+            }
+        }
+    }
+
+    private void processRefExamples(Map<String, Example> examples, String $ref) {
+        String file = $ref.split("#/")[0];
+        for(Example example : examples.values()) {
+            if (example.get$ref() != null) {
+                RefFormat ref = computeRefFormat(example.get$ref());
+                if (isAnExternalRefFormat(ref)) {
+                    processRefExample(example, $ref);
+                } else {
+                    processRefToExternalExample(file + example.get$ref(), RefFormat.RELATIVE);
+                }
+            }
+        }
+    }
+
+    private void processRefExample(Example example, String externalFile) {
+        RefFormat format = computeRefFormat(example.get$ref());
+
+        if (!isAnExternalRefFormat(format)) {
+            example.set$ref(RefType.SCHEMAS.getInternalPrefix()+ processRefToExternalSchema(externalFile + example.get$ref(), RefFormat.RELATIVE));
+            return;
+        }
+        String $ref = example.get$ref();
+        String subRefExternalPath = getExternalPath(example.get$ref())
+                .orElse(null);
+
+        if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
+            $ref = join(externalFile, example.get$ref());
+            example.set$ref($ref);
+        }else {
+            processRefToExternalExample($ref, format);
         }
     }
 


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 48963289..8cae3650 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
@@ -2261,6 +2261,16 @@ public class OpenAPIV3ParserTest {
         assertEquals(6, openAPI.getComponents().getSchemas().size());
     }
 
+    @Test
+    public void testIssue1335() {
+        final ParseOptions options = new ParseOptions();
+        options.setResolve(true);
+
+        SwaggerParseResult result = new OpenAPIV3Parser()
+                .readLocation("src/test/resources/issue1335.yaml", null, options);
+        assertNotNull(result.getOpenAPI().getComponents().getExamples().get("ex1"));
+    }
+
     @Test
     public void testRegressionIssue1236() {
         final ParseOptions options = new ParseOptions();
diff --git a/modules/swagger-parser-v3/src/test/resources/domain-issue-1335.yaml b/modules/swagger-parser-v3/src/test/resources/domain-issue-1335.yaml
new file mode 100644
index 00000000..fcb128e5
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/domain-issue-1335.yaml
@@ -0,0 +1,15 @@
+components:
+  responses:
+    200ok:
+      description: ok
+      content:
+        application/json:
+          schema:
+            type: string
+          examples:
+            ex1:
+              $ref: '#/components/examples/ex1'
+
+  examples:
+    ex1:
+      value: hello
\ No newline at end of file
diff --git a/modules/swagger-parser-v3/src/test/resources/issue1335.yaml b/modules/swagger-parser-v3/src/test/resources/issue1335.yaml
new file mode 100644
index 00000000..ba80fbda
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/issue1335.yaml
@@ -0,0 +1,10 @@
+openapi: 3.0.2
+info:
+  title: Example references
+  version: 1.0.0
+paths:
+  /foo:
+    get:
+      responses:
+        '200':
+          $ref: './domain-issue-1335.yaml#/components/responses/200ok'
\ No newline at end of file