Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #1592 Add support for JAR: URLs to OpenAPIV3Parser #1593

Merged
merged 12 commits into from
Feb 10, 2022
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.swagger.v3.parser.util.OpenAPIDeserializer;
import io.swagger.v3.parser.util.RemoteUrl;
import io.swagger.v3.parser.util.ResolverFully;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand All @@ -28,6 +29,7 @@
import java.util.ServiceLoader;
import javax.net.ssl.SSLHandshakeException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -227,6 +229,9 @@ private String readContentFromLocation(String location, List<AuthorizationValue>
try {
if (adjustedLocation.toLowerCase().startsWith("http")) {
return RemoteUrl.urlToString(adjustedLocation, auth);
} else if (adjustedLocation.toLowerCase().startsWith("jar:")) {
final InputStream in = new URI(adjustedLocation).toURL().openStream();
return IOUtils.toString(in, encoding);
} else {
final String fileScheme = "file:";
final Path path = adjustedLocation.toLowerCase().startsWith(fileScheme) ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public ResolverCache(OpenAPI openApi, List<AuthorizationValue> auths, String par
this.rootPath = parentFileLocation;

if(parentFileLocation != null) {
if(parentFileLocation.startsWith("http")) {
if(parentFileLocation.startsWith("http") || parentFileLocation.startsWith("jar")) {
parentDirectory = null;
} else {
parentDirectory = PathUtils.getParentDirectoryOfFile(parentFileLocation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -2967,6 +2968,17 @@ public void testIssue1540() throws Exception{
Assert.assertEquals((String)testPutExtensions.get("x-order"),"2147483647");
}

@Test
public void testIssue1592() throws Exception {
File file = new File("src/test/resources/issue-1592.jar");
URL url = new URL("jar:" + file.toURI() + "!/test.yaml");
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = new OpenAPIV3Parser().readLocation(url.toString(), null, options);
OpenAPI openAPI = result.getOpenAPI();
assertNotNull(openAPI);
}

@Test
public void testNullExample() throws Exception{
String yamlString = FileUtils.readFileToString(new File("src/test/resources/null-full-example.yaml"), "UTF-8");
Expand Down
Binary file not shown.