Repository: swagger-api/swagger-parser
Pull request #824 (2018-09-01 14:35:30)

refs #822 - decode properly refs


Patch:
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ResolverCache.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ResolverCache.java
index 62d1c584..a78538d3 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ResolverCache.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ResolverCache.java
@@ -15,6 +15,8 @@ import io.swagger.v3.parser.util.OpenAPIDeserializer;
 import org.apache.commons.lang3.StringUtils;
 
 import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
 import java.nio.file.Path;
 import java.util.Collections;
 import java.util.HashMap;
@@ -249,6 +251,12 @@ public class ResolverCache {
     }
 
     private String unescapePointer(String jsonPathElement) {
+        // URL decode the fragment
+        try {
+            jsonPathElement = URLDecoder.decode(jsonPathElement, "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            //
+        }
         // Unescape the JSON Pointer segment using the algorithm described in RFC 6901, section 4:
         // https://tools.ietf.org/html/rfc6901#section-4
         // First transform any occurrence of the sequence '~1' to '/'


Test patch:
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/FileReferenceTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/FileReferenceTest.java
index cb6d3af9..38a6540b 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/FileReferenceTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/FileReferenceTest.java
@@ -152,6 +152,22 @@ public class FileReferenceTest {
         assertNotNull(swagger.getPaths().get("/foo").getGet());
     }
 
+    @Test
+    public void testIssue822() {
+
+        ParseOptions options = new ParseOptions();
+        options.setResolve(true);
+        SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./src/test/resources/issue-822.yaml", null, options);
+
+        OpenAPI swagger = result.getOpenAPI();
+        assertNotNull(swagger.getPaths().get("/foo").getGet());
+        assertNotNull(swagger.getPaths().get("/bar/wtf").getGet());
+        assertNull(swagger.getPaths().get("/bar/haha").getGet());
+        assertNotNull(swagger.getPaths().get("/wtf/{bar}").getGet());
+        assertNotNull(swagger.getPaths().get("/haha/{bar}").getGet());
+        assertNull(swagger.getPaths().get("/haha2/{bar}").getGet());
+    }
+
     @Test
     public void testIssue336() {
         ParseOptions options = new ParseOptions();
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-822-b.yaml b/modules/swagger-parser-v3/src/test/resources/issue-822-b.yaml
new file mode 100644
index 00000000..10f14e3d
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/issue-822-b.yaml
@@ -0,0 +1,37 @@
+openapi: "3.0.0"
+
+info:
+  version: 1.0.0
+  title: Path include test case child
+
+paths:
+  /foo:
+    get:
+      responses:
+        200:
+          description: "Request successful"
+  /bar/wtf:
+    get:
+      responses:
+        200:
+          description: "Request successful"
+  /bar/haha:
+    get:
+      responses:
+        200:
+          description: "Request successful"
+  /wtf/{bar}:
+    get:
+      responses:
+        200:
+          description: "Request successful"
+  /haha/{bar}:
+    get:
+      responses:
+        200:
+          description: "Request successful"
+  /haha2/{bar}:
+    get:
+      responses:
+        200:
+          description: "Request successful"
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-822.yaml b/modules/swagger-parser-v3/src/test/resources/issue-822.yaml
new file mode 100644
index 00000000..b04455e2
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/issue-822.yaml
@@ -0,0 +1,19 @@
+openapi: "3.0.0"
+
+info:
+  version: 1.0.0
+  title: Path include test case
+
+paths:
+  /foo:
+    $ref: './issue-822-b.yaml#/paths/~1foo'
+  /wtf/{bar}:
+    $ref: './issue-822-b.yaml#/paths/~1wtf~1%7Bbar%7D'
+  /bar/wtf:
+    $ref: './issue-822-b.yaml#/paths/~1bar~1wtf'
+  /haha/{bar}:
+    $ref: './issue-822-b.yaml#/paths/~1haha~1{bar}'
+  /haha2/{bar}:
+    $ref: './issue-822-b.yaml#/paths/~1haha/{bar}'
+  /bar/haha:
+    $ref: './issue-822-b.yaml#/paths/bar/haha'