Repository: swagger-api/swagger-parser
Pull request #1879 (2023-02-01 16:25:22)

fix: #1878 ensure correct separator in classpath location
This PR provides a possible solution to #1878.


Patch:
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ClasspathHelper.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ClasspathHelper.java
index c32c914f..88ac9e49 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ClasspathHelper.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ClasspathHelper.java
@@ -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;
@@ -9,25 +10,26 @@ import java.io.InputStream;
 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");
     }
 }


Test patch:
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/ClasspathHelperTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/ClasspathHelperTest.java
index 5260ea0d..77e4f271 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/ClasspathHelperTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/ClasspathHelperTest.java
@@ -15,6 +15,11 @@ public class ClasspathHelperTest {
     @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");
     }
 }
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1878/test.txt b/modules/swagger-parser-v3/src/test/resources/issue-1878/test.txt
new file mode 100644
index 00000000..3f3f005b
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/issue-1878/test.txt
@@ -0,0 +1,1 @@
+Test content
\ No newline at end of file