Skip to content

Commit 73d4dfd

Browse files
committed
Support file URL scheme produced by java.io.File et al.
The URLs produced by java.io.File.toURI() (and deprecated .toURL()) and java.lang.Class.getResource(String) contain no additional slashes between "file:" and the path. This makes using Swagger20Parser with files from these methods a bit more painful than it needs to be. This commit addresses this by recognizing strings starting with "file:" as file URLs. Signed-off-by: Kevin Locke <[email protected]>
1 parent 7c801e3 commit 73d4dfd

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

modules/swagger-parser/src/main/java/io/swagger/parser/Swagger20Parser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public SwaggerDeserializationResult readWithInfo(String location, List<Authoriza
4242
if (location.toLowerCase().startsWith("http")) {
4343
data = RemoteUrl.urlToString(location, auths);
4444
} else {
45-
final String fileScheme = "file://";
45+
final String fileScheme = "file:";
4646
Path path;
4747
if (location.toLowerCase().startsWith(fileScheme)) {
4848
path = Paths.get(URI.create(location));
@@ -80,7 +80,7 @@ public Swagger read(String location, List<AuthorizationValue> auths) throws IOEx
8080
if (location.toLowerCase().startsWith("http")) {
8181
data = RemoteUrl.urlToString(location, auths);
8282
} else {
83-
final String fileScheme = "file://";
83+
final String fileScheme = "file:";
8484
Path path;
8585
if (location.toLowerCase().startsWith(fileScheme)) {
8686
path = Paths.get(URI.create(location));

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class PathUtils {
88

99

1010
public static Path getParentDirectoryOfFile(String fileStr) {
11-
final String fileScheme = "file://";
11+
final String fileScheme = "file:";
1212
Path file;
1313
fileStr = fileStr.replaceAll("\\\\","/");
1414
if (fileStr.toLowerCase().startsWith(fileScheme)) {

modules/swagger-parser/src/test/java/io/swagger/parser/SwaggerReaderTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ public void readUberApiFromFileWithSpaces() {
3838
assertNotNull(swagger);
3939
}
4040

41+
@Test(description = "it should read the uber api from Path URI")
42+
public void readUberApiFromPathUri() {
43+
final SwaggerParser parser = new SwaggerParser();
44+
java.nio.file.Path uberPath = Paths.get("src/test/resources/uber.json");
45+
final Swagger swagger = parser.read(uberPath.toUri().toString());
46+
assertNotNull(swagger);
47+
}
48+
49+
@Test(description = "it should read the uber api from File URI")
50+
public void readUberApiFromFileUri() {
51+
final SwaggerParser parser = new SwaggerParser();
52+
java.io.File uberFile = new java.io.File("src/test/resources/uber.json");
53+
final Swagger swagger = parser.read(uberFile.toURI().toString());
54+
assertNotNull(swagger);
55+
}
56+
4157
@Test(description = "it should read the uber api with url string without file scheme")
4258
public void readUberApiFromFileNoScheme() {
4359
final SwaggerParser parser = new SwaggerParser();

0 commit comments

Comments
 (0)