Skip to content

Commit

Permalink
Merge pull request #1879 from adrianhj/fix/ensure-valid-classpath-paths
Browse files Browse the repository at this point in the history
fix: #1878 ensure correct separator in classpath location
  • Loading branch information
gracekarina authored Feb 28, 2023
2 parents 092438d + 90a54c5 commit 7e7123d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.swagger.v3.parser.util;


import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
Expand All @@ -9,25 +10,26 @@
public class ClasspathHelper {

public static String loadFileFromClasspath(String location) {
String file = FilenameUtils.separatorsToUnix(location);

InputStream inputStream = ClasspathHelper.class.getResourceAsStream(location);
InputStream inputStream = ClasspathHelper.class.getResourceAsStream(file);

if(inputStream == null) {
inputStream = ClasspathHelper.class.getClassLoader().getResourceAsStream(location);
inputStream = ClasspathHelper.class.getClassLoader().getResourceAsStream(file);
}

if(inputStream == null) {
inputStream = ClassLoader.getSystemResourceAsStream(location);
inputStream = ClassLoader.getSystemResourceAsStream(file);
}

if(inputStream != null) {
try {
return IOUtils.toString(inputStream);
} catch (IOException e) {
throw new RuntimeException("Could not read " + location + " from the classpath", e);
throw new RuntimeException("Could not read " + file + " from the classpath", e);
}
}

throw new RuntimeException("Could not find " + location + " on the classpath");
throw new RuntimeException("Could not find " + file + " on the classpath");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public void testLoadFileFromClasspath() throws Exception {
@Test(expectedExceptions = {RuntimeException.class})
public void testLoadFileFromClasspath_DoesntExist() throws Exception {
ClasspathHelper.loadFileFromClasspath("nothing.txt");
}

@Test
public void testLoadFileFromClasspath_HandlesWindowsPathsOk() {
final String contents = ClasspathHelper.loadFileFromClasspath("\\issue-1878\\test.txt");
assertEquals(contents, "Test content");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test content

0 comments on commit 7e7123d

Please sign in to comment.