From e6977d7c2018902262b2e5f09daece495f72c9ee Mon Sep 17 00:00:00 2001 From: Martin Goldhahn Date: Sat, 6 Jan 2024 23:01:02 +0100 Subject: [PATCH] fixed issue 2034, added test case for issue --- .../swagger/v3/parser/reference/Visitor.java | 12 ++--- .../parser/test/OpenAPIV31ParserUriTest.java | 52 +++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV31ParserUriTest.java diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/Visitor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/Visitor.java index d41b21cc22..7e1f26375d 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/Visitor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/Visitor.java @@ -62,14 +62,14 @@ public interface Visitor { Example visitExample(Example example); - default String readFile(String uri) throws Exception { - try (InputStream inputStream = new FileInputStream(uri)) { + default String readFile(String path) throws Exception { + try (InputStream inputStream = new FileInputStream(path)) { return IOUtils.toString(inputStream, UTF_8); } } - default String readClasspath(String uri) throws Exception { - return ClasspathHelper.loadFileFromClasspath(uri); + default String readClasspath(String classPath) throws Exception { + return ClasspathHelper.loadFileFromClasspath(classPath); } default String readHttp(String uri, List auths) throws Exception { return RemoteUrl.urlToString(uri, auths); @@ -81,9 +81,9 @@ default String readURI(String absoluteUri, List auths) throw if (resolved.getScheme().startsWith("http")) { return readHttp(absoluteUri, auths); } else if (resolved.getScheme().startsWith("file")) { - return readFile(absoluteUri); + return readFile(resolved.getPath()); } else if (resolved.getScheme().startsWith("classpath")) { - return readClasspath(absoluteUri); + return readClasspath(resolved.getPath()); } } // If no matches exists, try file diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV31ParserUriTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV31ParserUriTest.java new file mode 100644 index 0000000000..5452bb2bf8 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV31ParserUriTest.java @@ -0,0 +1,52 @@ +package io.swagger.v3.parser.test; + + +import io.swagger.v3.parser.OpenAPIV3Parser; +import io.swagger.v3.parser.core.models.ParseOptions; +import io.swagger.v3.parser.core.models.SwaggerParseResult; +import org.apache.commons.io.IOUtils; +import org.testng.annotations.Test; + +import java.net.URI; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; + +/** + * Test parsing classpath and file URIs. + * Before the fix, an exception is logged as an error and a message containing "(No such file or directory)" + * is added to the parse result. + * This test checks for the absence of the message. + */ +public class OpenAPIV31ParserUriTest { + @Test + public void resolveFileInput() throws Exception { + URI uri = getClass().getResource("/3.1.0/basic.yaml").toURI(); + assertEquals(uri.getScheme(), "file"); + String uriString = uri.toString(); + ParseOptions options = new ParseOptions(); + options.setResolve(true); + SwaggerParseResult result = new OpenAPIV3Parser().readLocation(uriString, null, options); + validateParseResult(result, "(No such file or directory)"); + } + + @Test + public void resolveClasspathInput() throws Exception { + URL url = getClass().getResource("/3.1.0/basic.yaml"); + String content = IOUtils.toString(url, StandardCharsets.UTF_8); + ParseOptions options = new ParseOptions(); + options.setResolve(true); + SwaggerParseResult result = new OpenAPIV3Parser().readContents(content, null, options, "classpath:/3.1.0/basic.yaml"); + validateParseResult(result, "Could not find classpath:/3.1.0/basic.yaml"); + } + + private static void validateParseResult(SwaggerParseResult result, String checkForMessage) { + String noSuchFileMessage = result.getMessages().stream() + .filter(message -> message.contains(checkForMessage)) + .findFirst() + .orElse(null); + assertNull(noSuchFileMessage); + } +}