Skip to content

Commit 290223e

Browse files
committed
Test and fix #750 by reusing ExternalRefProcessor
1 parent be2301b commit 290223e

File tree

2 files changed

+14
-39
lines changed

2 files changed

+14
-39
lines changed

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

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.swagger.models.auth.AuthorizationValue;
44
import io.swagger.models.refs.RefFormat;
5+
import io.swagger.parser.processors.ExternalRefProcessor;
56
import org.apache.commons.io.FileUtils;
67
import org.apache.commons.io.IOUtils;
78
import org.apache.commons.lang3.StringUtils;
@@ -80,45 +81,7 @@ public static String readExternalUrlRef(String file, RefFormat refFormat, List<A
8081
}
8182

8283
public static String buildUrl(String rootPath, String relativePath) {
83-
String[] rootPathParts = rootPath.split("/");
84-
String [] relPathParts = relativePath.split("/");
85-
86-
if(rootPath == null || relativePath == null) {
87-
return null;
88-
}
89-
90-
int trimRoot = 0;
91-
int trimRel = 0;
92-
93-
if(!"".equals(rootPathParts[rootPathParts.length - 1])) {
94-
trimRoot = 1;
95-
}
96-
for(int i = 0; i < rootPathParts.length; i++) {
97-
if("".equals(rootPathParts[i])) {
98-
trimRel += 1;
99-
}
100-
else {
101-
break;
102-
}
103-
}
104-
for(int i = 0; i < relPathParts.length; i ++) {
105-
if(".".equals(relPathParts[i])) {
106-
trimRel += 1;
107-
}
108-
else if ("..".equals(relPathParts[i])) {
109-
trimRel += 1;
110-
}
111-
}
112-
113-
String [] outputParts = new String[rootPathParts.length + relPathParts.length - trimRoot - trimRel];
114-
System.arraycopy(rootPathParts, 0, outputParts, 0, rootPathParts.length - trimRoot);
115-
System.arraycopy(relPathParts,
116-
trimRel,
117-
outputParts,
118-
rootPathParts.length - trimRoot + trimRel - 1,
119-
relPathParts.length - trimRel);
120-
121-
return StringUtils.join(outputParts, "/");
84+
return ExternalRefProcessor.join(rootPath, relativePath);
12285
}
12386

12487

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,16 @@ public void testPathJoin1() {
297297
// relative locations
298298
assertEquals(ExternalRefProcessor.join("./foo#/definitions/Foo", "./bar#/definitions/Bar"), "./bar#/definitions/Bar");
299299
}
300+
301+
@Test
302+
public void testRelativeRef() {
303+
// Test for #750
304+
assertEquals(
305+
RefUtils.buildUrl(
306+
"https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/specification/storage/resource-manager/Microsoft.Storage/stable/2018-02-01/storage.json",
307+
"../../../../../common-types/resource-management/v1/types.json"
308+
),
309+
"https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/specification/common-types/resource-management/v1/types.json"
310+
);
311+
}
300312
}

0 commit comments

Comments
 (0)