Skip to content

Commit ce79aed

Browse files
authored
Merge pull request #1038 from jweisman/issue-1035
PR for Issue #1035
2 parents c121b44 + 1f9c715 commit ce79aed

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

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
@@ -132,6 +132,9 @@ public static String buildUrl(String rootPath, String relativePath) {
132132
if(!"".equals(rootPathParts[rootPathParts.length - 1])) {
133133
trimRoot = 1;
134134
}
135+
if("".equals(relPathParts[0])) {
136+
trimRel = 1; trimRoot = rootPathParts.length-3;
137+
}
135138
for(int i = 0; i < rootPathParts.length; i++) {
136139
if("".equals(rootPathParts[i])) {
137140
trimRel += 1;
@@ -145,7 +148,7 @@ public static String buildUrl(String rootPath, String relativePath) {
145148
trimRel += 1;
146149
}
147150
else if ("..".equals(relPathParts[i])) {
148-
trimRel += 1;
151+
trimRel += 1; trimRoot += 1;
149152
}
150153
}
151154

@@ -154,7 +157,7 @@ else if ("..".equals(relPathParts[i])) {
154157
System.arraycopy(relPathParts,
155158
trimRel,
156159
outputParts,
157-
rootPathParts.length - trimRoot + trimRel - 1,
160+
rootPathParts.length - trimRoot,
158161
relPathParts.length - trimRel);
159162

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

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,20 @@ public void testPathJoin1() {
299299
assertEquals(ExternalRefProcessor.join("./foo#/definitions/Foo", "./bar#/definitions/Bar"), "./bar#/definitions/Bar");
300300
}
301301

302+
303+
@Test
304+
public void testPathJoin2() {
305+
assertEquals(RefUtils.buildUrl("http://foo.bar.com/my/dir/file.yaml", "../newFile.yaml"), "http://foo.bar.com/my/newFile.yaml");
306+
assertEquals(RefUtils.buildUrl("http://foo.bar.com/my/dir/file.yaml", "../../newFile.yaml"), "http://foo.bar.com/newFile.yaml");
307+
assertEquals(RefUtils.buildUrl("http://foo.bar.com/my/dir/file.yaml", "./newFile.yaml"), "http://foo.bar.com/my/dir/newFile.yaml");
308+
assertEquals(RefUtils.buildUrl("http://foo.bar.com/my/dir/file.yaml", "../second/newFile.yaml"), "http://foo.bar.com/my/second/newFile.yaml");
309+
assertEquals(RefUtils.buildUrl("http://foo.bar.com/my/dir/file.yaml", "../../otherDir/newFile.yaml"), "http://foo.bar.com/otherDir/newFile.yaml");
310+
assertEquals(RefUtils.buildUrl("http://foo.bar.com/file.yaml", "./newFile.yaml"), "http://foo.bar.com/newFile.yaml");
311+
assertEquals(RefUtils.buildUrl("http://foo.bar.com/my/dir/file.yaml", "/newFile.yaml"), "http://foo.bar.com/newFile.yaml");
312+
assertEquals(RefUtils.buildUrl("http://foo.bar.com/my/dir/file.yaml", "/my/newFile.yaml"), "http://foo.bar.com/my/newFile.yaml");
313+
}
314+
315+
302316
@Test
303317
public void shouldReturnEmptyExternalPathForInternalReference() {
304318
// given
@@ -371,5 +385,6 @@ public void shouldReturnExternalPathForHttpReference() {
371385
// then
372386
assertThat(externalPath.isPresent(), is(true));
373387
assertThat(externalPath.get(), equalTo("http://localhost/schema.json"));
388+
374389
}
375390
}

0 commit comments

Comments
 (0)