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

Commit 9a040f6

Browse files
authored
Merge pull request swagger-api#1134 from scottr-ad/master
issue-1133 - Parser does not resolve $ref pointing to relative locations on classpath
2 parents 895c99f + a00786a commit 9a040f6

4 files changed

Lines changed: 63 additions & 7 deletions

File tree

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ResolverCache.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,13 @@ public <T> T loadRef(String ref, RefFormat refFormat, Class<T> expectedType) {
118118
if(parentDirectory != null) {
119119
contents = RefUtils.readExternalRef(file, refFormat, auths, parentDirectory);
120120
}
121-
else if(rootPath != null) {
121+
else if(rootPath != null && rootPath.startsWith("http")) {
122122
contents = RefUtils.readExternalUrlRef(file, refFormat, auths, rootPath);
123123
}
124+
else if (rootPath != null) {
125+
contents = RefUtils.readExternalClasspathRef(file, refFormat, auths, rootPath);
126+
127+
}
124128
externalFileCache.put(file, contents);
125129
}
126130

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,32 @@ public static Path getParentDirectoryOfFile(String location) {
2020
file = Paths.get(location).toAbsolutePath();
2121
}
2222
if (!Files.exists(file)) {
23-
URL url = PathUtils.class.getClassLoader().getResource(location);
24-
file = Paths.get((URI.create(url.toExternalForm())));
25-
return file.getParent();
23+
return getParentDirectoryFromUrl(location);
2624
}
2725

28-
29-
3026
} catch (Exception e) {
3127
e.getMessage();
3228
}
3329

3430
return file.toAbsolutePath().getParent();
3531
}
32+
33+
private static Path getParentDirectoryFromUrl(String location){
34+
try {
35+
URL url = PathUtils.class.getResource(location);
36+
if (url == null){
37+
url = PathUtils.class.getClassLoader().getResource(location);
38+
}
39+
if(url == null) {
40+
url = ClassLoader.getSystemResource(location);
41+
}
42+
43+
Path file = Paths.get((URI.create(url.toExternalForm())));
44+
return file.getParent();
45+
46+
} catch (Exception e) {
47+
e.getMessage();
48+
return null;
49+
}
50+
}
3651
}

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

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

33
import io.swagger.v3.parser.core.models.AuthorizationValue;
44
import io.swagger.v3.parser.models.RefFormat;
5+
import io.swagger.v3.parser.processors.ExternalRefProcessor;
56
import org.apache.commons.io.IOUtils;
67
import org.apache.commons.lang3.StringUtils;
78

@@ -118,6 +119,32 @@ public static String readExternalUrlRef(String file, RefFormat refFormat, List<A
118119

119120
}
120121

122+
public static String readExternalClasspathRef(String file, RefFormat refFormat, List<AuthorizationValue> auths,
123+
String rootPath) {
124+
125+
if (!RefUtils.isAnExternalRefFormat(refFormat)) {
126+
throw new RuntimeException("Ref is not external");
127+
}
128+
129+
String result;
130+
131+
try {
132+
if (refFormat == RefFormat.URL) {
133+
result = RemoteUrl.urlToString(file, auths);
134+
} else {
135+
//its assumed to be a relative ref
136+
String pathRef = ExternalRefProcessor.join(rootPath, file);
137+
138+
result = ClasspathHelper.loadFileFromClasspath(pathRef);
139+
}
140+
} catch (Exception e) {
141+
throw new RuntimeException("Unable to load " + refFormat + " ref: " + file, e);
142+
}
143+
144+
return result;
145+
146+
}
147+
121148
public static String buildUrl(String rootPath, String relativePath) {
122149
String[] rootPathParts = rootPath.split("/");
123150
String [] relPathParts = relativePath.split("/");

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@
22

33
import org.testng.annotations.Test;
44

5+
import java.nio.file.Path;
56
import java.nio.file.Paths;
67

78
import static org.testng.Assert.assertEquals;
9+
import static org.testng.Assert.assertNull;
810

911
public class PathUtilTest {
1012

1113
@Test
1214
public void testGetParentDirectoryOfFile() throws Exception {
1315

14-
final String actualResult = PathUtils.getParentDirectoryOfFile("src/test/resources/parent.json").toString();
16+
final String actualResult = PathUtils.getParentDirectoryOfFile("src/test/resources/test.yaml").toString();
1517

1618
final String expectedResult = Paths.get("src/test/resources").toAbsolutePath().toString();
1719

1820
assertEquals(actualResult, expectedResult);
1921
}
22+
23+
@Test
24+
public void testGetParentDirectoryOfNonExistentFile() throws Exception {
25+
26+
final Path result = PathUtils.getParentDirectoryOfFile("src/test/resources/parent.json");
27+
28+
assertNull(result);
29+
}
2030
}

0 commit comments

Comments
 (0)