Skip to content

Commit

Permalink
fix: #1848 ensure correct separator in classpath location
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhj committed Feb 1, 2023
1 parent b63dd8f commit 44777b8
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 44777b8

Please sign in to comment.