Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/issue-1961
Browse files Browse the repository at this point in the history
  • Loading branch information
gracekarina authored Feb 2, 2024
2 parents bd30cb8 + 7d6bad8 commit 3e7e97f
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ You can include this library from Sonatype OSS for SNAPSHOTS, or Maven central f
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser</artifactId>
<version>2.1.19</version>
<version>2.1.20</version>
</dependency>
```

Expand Down
4 changes: 2 additions & 2 deletions modules/swagger-parser-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>swagger-parser-project</artifactId>
<groupId>io.swagger.parser.v3</groupId>
<version>2.1.20-SNAPSHOT</version>
<version>2.1.21-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down Expand Up @@ -91,7 +91,7 @@
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser-v3</artifactId>
<version>2.1.20-SNAPSHOT</version>
<version>2.1.21-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion modules/swagger-parser-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser-project</artifactId>
<version>2.1.20-SNAPSHOT</version>
<version>2.1.21-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion modules/swagger-parser-safe-url-resolver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser-project</artifactId>
<version>2.1.20-SNAPSHOT</version>
<version>2.1.21-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>

</parent>
Expand Down
2 changes: 1 addition & 1 deletion modules/swagger-parser-v2-converter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser-project</artifactId>
<version>2.1.20-SNAPSHOT</version>
<version>2.1.21-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.tags.Tag;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.converter.SwaggerConverter;
import io.swagger.v3.parser.core.extensions.SwaggerParserExtension;
import io.swagger.v3.parser.core.models.AuthorizationValue;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;

import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
Expand Down Expand Up @@ -923,4 +927,42 @@ public void testConvertFormDataAsObjectSchema() throws Exception {
assertEquals(companiesSchema.getClass(), ObjectSchema.class);

}

/**
* A clone (almost) of {@link OpenAPIV3Parser#readContents(String, List, ParseOptions)}.
*/
private OpenAPI read(String location, List<AuthorizationValue> auths, ParseOptions resolve, final List<SwaggerParserExtension> parserExtensions) {
if (location == null) {
return null;
}
SwaggerParseResult parsed;
for (SwaggerParserExtension extension : parserExtensions) {
parsed = extension.readLocation(location, auths, resolve);
if (parsed.getMessages() != null) {
for (String message : parsed.getMessages()) {
// LOGGER.info("{}: {}", extension, message);
}
}
final OpenAPI result = parsed.getOpenAPI();
if (result != null) {
return result;
}
}
return null;
}

/**
* Tests an NPE when bother the old and new parser are on the classpath. The only way to test this is cloning the
* {@link OpenAPIV3Parser#readContents(String, List, ParseOptions)} to see the NPE when we pass in the extentions. Instead of, for example, mocking the call
* to {@link io.swagger.v3.parser.OpenAPIV3Parser.getExtensions()}.
*/
@Test
public void testIssue2046() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
// This should not throw an NPE:
final OpenAPI openAPI = read("I/do/not/exist/on/the/file/system/I/really/do/not.yaml", null, options, Arrays.asList(new SwaggerConverter()));
Assert.assertNull(openAPI);
}

}
2 changes: 1 addition & 1 deletion modules/swagger-parser-v3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser-project</artifactId>
<version>2.1.20-SNAPSHOT</version>
<version>2.1.21-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ public OpenAPI read(String location, List<AuthorizationValue> auths, ParseOption
SwaggerParseResult parsed;
for (SwaggerParserExtension extension : parserExtensions) {
parsed = extension.readLocation(location, auths, resolve);
for (String message : parsed.getMessages()) {
LOGGER.info("{}: {}", extension, message);
if (parsed.getMessages() != null) {
for (String message : parsed.getMessages()) {
LOGGER.info("{}: {}", extension, message);
}
}
final OpenAPI result = parsed.getOpenAPI();
if (result != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public void processSchema(Schema schema) {

public void processSchemaType(Schema schema){

if (schema == null) {
return;
}
if (schema instanceof ArraySchema) {
processArraySchema((ArraySchema) schema);
}
Expand Down Expand Up @@ -215,7 +218,7 @@ private void changeDiscriminatorMapping(ComposedSchema composedSchema, String ol
public void processArraySchema(ArraySchema arraySchema) {

final Schema items = arraySchema.getItems();
if (items.get$ref() != null) {
if (items != null && items.get$ref() != null) {
processReferenceSchema(items);
}else{
processSchemaType(items);
Expand Down
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
Expand Up @@ -735,6 +735,9 @@ public void copyVendorExtensions(Schema source, Schema target) {
}

private boolean isObjectSchema(Schema schema) {
if (schema == null) {
return false;
}
return schema instanceof ObjectSchema
|| "object".equalsIgnoreCase(schema.getType())
|| (schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1464,4 +1464,13 @@ private static int getDynamicPort() {
return new Random().ints(50000, 60000).findFirst().getAsInt();
}

@Test
public void testResolveArraySchemaItemsNullPointerException() {
final ParseOptions options = new ParseOptions();
options.setResolve(true);
final String actualLocation = "C:/Users/ggregory/git/r/api-gateway/ais-swagger-test-fixtures/src/test/resources/APIs-guru/openapi-directory-master/APIs/clearblade.com/3.0/swagger.yaml";
final OpenAPI output = new OpenAPIV3Parser().read(actualLocation, null, options);
new OpenAPIResolver(output, null, actualLocation).resolve();
}

}
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);
}
}
2 changes: 1 addition & 1 deletion modules/swagger-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser-project</artifactId>
<version>2.1.20-SNAPSHOT</version>
<version>2.1.21-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser-project</artifactId>
<version>2.1.20-SNAPSHOT</version>
<version>2.1.21-SNAPSHOT</version>
<packaging>pom</packaging>
<name>swagger-parser-project</name>
<developers>
Expand Down Expand Up @@ -413,19 +413,19 @@
<properties>
<maven.compiler.release>8</maven.compiler.release>
<snakeyaml-version>2.2</snakeyaml-version>
<swagger-parser-v2-version>1.0.68</swagger-parser-v2-version>
<commons-io-version>2.15.0</commons-io-version>
<swagger-parser-v2-version>1.0.69</swagger-parser-v2-version>
<commons-io-version>2.15.1</commons-io-version>
<slf4j-version>2.0.9</slf4j-version>
<swagger-core-version>2.2.19</swagger-core-version>
<swagger-core-version>2.2.20</swagger-core-version>
<swagger-core-v2-version>1.6.12</swagger-core-v2-version>
<junit-version>4.13.2</junit-version>
<testng-version>7.8.0</testng-version>
<testng-version>7.9.0</testng-version>
<jmockit-version>1.49</jmockit-version>
<wiremock-version>2.35.1</wiremock-version>
<surefire-version>3.2.2</surefire-version>
<commons-lang-version>3.13.0</commons-lang-version>
<jackson-version>2.15.3</jackson-version>
<jackson-databind-version>2.15.3</jackson-databind-version>
<commons-lang-version>3.14.0</commons-lang-version>
<jackson-version>2.16.1</jackson-version>
<jackson-databind-version>2.16.1</jackson-databind-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sonatypeOssDistMgmtSnapshotsUrl>https://oss.sonatype.org/content/repositories/snapshots/</sonatypeOssDistMgmtSnapshotsUrl>
</properties>
Expand Down

0 comments on commit 3e7e97f

Please sign in to comment.