Skip to content

Commit

Permalink
Fix Issue 2046
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jan 22, 2024
1 parent 00d2c5c commit c4e4e60
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 24 deletions.
6 changes: 6 additions & 0 deletions modules/swagger-parser-v3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<artifactId>swagger-parser-safe-url-resolver</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser-v2-converter</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
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
@@ -1,43 +1,77 @@
package io.swagger.v3.parser.test;


import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.hamcrest.CoreMatchers;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.reporters.Files;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.*;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.examples.Example;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.links.Link;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.parameters.*;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.ByteArraySchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.HeaderParameter;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.PathParameter;
import io.swagger.v3.oas.models.parameters.QueryParameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.parser.OpenAPIResolver;
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 io.swagger.v3.parser.util.SchemaTypeUtil;
import org.apache.commons.io.FileUtils;
import org.hamcrest.CoreMatchers;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.reporters.Files;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.util.*;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.testng.Assert.*;


public class OpenAPIV3ParserTest {
Expand Down Expand Up @@ -1084,6 +1118,43 @@ public void testIssueIntegerDefault() {
Assert.assertNull (openAPI.getPaths().get("/primitiveBody/binary").getPost().getRequestBody().getContent().get("application/octet-stream").getSchema().getFormat());
}

/**
* 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);
}

@Test
public void testIssue983() {
OpenAPIV3Parser parser = new OpenAPIV3Parser();
Expand Down

0 comments on commit c4e4e60

Please sign in to comment.