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 2046] NPE in OpenAPIV3Parser.read(String, List<AuthorizationValue>, ParseOptions) #2047

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}
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
Loading