Skip to content

Commit

Permalink
Merge pull request #2035 from maddingo/bugfix/issue-2034
Browse files Browse the repository at this point in the history
fixed issue 2034, added test case for issue
  • Loading branch information
gracekarina authored Jan 15, 2024
2 parents 2295b8a + 429c24c commit 3837814
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthorizationValue> auths) throws Exception {
return RemoteUrl.urlToString(uri, auths);
Expand All @@ -81,9 +81,9 @@ default String readURI(String absoluteUri, List<AuthorizationValue> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 3837814

Please sign in to comment.