Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 45245ac

Browse files
committed
buildUrl: 1. Trim ".." from root as well 2. If rel starts with "/", trim root to domain
1 parent 5c480c5 commit 45245ac

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/RefUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ public static String buildUrl(String rootPath, String relativePath) {
114114
if(!"".equals(rootPathParts[rootPathParts.length - 1])) {
115115
trimRoot = 1;
116116
}
117+
if("".equals(relPathParts[0])) {
118+
trimRel = 1; trimRoot = rootPathParts.length-3;
119+
}
117120
for(int i = 0; i < rootPathParts.length; i++) {
118121
if("".equals(rootPathParts[i])) {
119122
trimRel += 1;
@@ -127,7 +130,7 @@ public static String buildUrl(String rootPath, String relativePath) {
127130
trimRel += 1;
128131
}
129132
else if ("..".equals(relPathParts[i])) {
130-
trimRel += 1;
133+
trimRel += 1; trimRoot += 1;
131134
}
132135
}
133136

@@ -136,7 +139,7 @@ else if ("..".equals(relPathParts[i])) {
136139
System.arraycopy(relPathParts,
137140
trimRel,
138141
outputParts,
139-
rootPathParts.length - trimRoot + trimRel - 1,
142+
rootPathParts.length - trimRoot,
140143
relPathParts.length - trimRel);
141144

142145
return StringUtils.join(outputParts, "/");

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/RefUtilsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,12 @@ public void testPathJoin1() {
293293
// relative locations
294294
assertEquals(ExternalRefProcessor.join("./foo#/definitions/Foo", "./bar#/definitions/Bar"), "./bar#/definitions/Bar");
295295
}
296+
297+
@Test
298+
public void testPathJoin2() {
299+
assertEquals(RefUtils.buildUrl("http://foo.bar.com/my/dir/file.yaml", "../newFile.yaml"), "http://foo.bar.com/my/newFile.yaml");
300+
assertEquals(RefUtils.buildUrl("http://foo.bar.com/my/dir/file.yaml", "../../newFile.yaml"), "http://foo.bar.com/newFile.yaml");
301+
assertEquals(RefUtils.buildUrl("http://foo.bar.com/my/dir/file.yaml", "/newFile.yaml"), "http://foo.bar.com/newFile.yaml");
302+
assertEquals(RefUtils.buildUrl("http://foo.bar.com/my/dir/file.yaml", "/my/newFile.yaml"), "http://foo.bar.com/my/newFile.yaml");
303+
}
296304
}

0 commit comments

Comments
 (0)