From 44777b896f9670548c904fce17283bd343fd412c Mon Sep 17 00:00:00 2001 From: Adrian Hjerstedt Date: Wed, 1 Feb 2023 16:23:42 +0000 Subject: [PATCH] fix: #1848 ensure correct separator in classpath location --- .../io/swagger/v3/parser/util/ClasspathHelper.java | 12 +++++++----- .../swagger/v3/parser/util/ClasspathHelperTest.java | 5 +++++ .../src/test/resources/issue-1878/test.txt | 1 + 3 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-1878/test.txt 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 c32c914fba..88ac9e49c1 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 @@ 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"); } } 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 5260ea0df6..77e4f271ab 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 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"); } } 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 0000000000..3f3f005b29 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-1878/test.txt @@ -0,0 +1 @@ +Test content \ No newline at end of file