Skip to content

Commit e9312d7

Browse files
authored
Merge pull request #1686 from swagger-api/issue1685
NPE when swagger field is invalid in 2.0 to 3.0 converter
2 parents 1670890 + 8516f27 commit e9312d7

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public SwaggerParseResult readContents(String swaggerAsString, List<Authorizatio
109109

110110
private SwaggerParseResult readResult(SwaggerDeserializationResult result, List<AuthorizationValue> auth, ParseOptions options) {
111111
SwaggerParseResult out = convert(result);
112-
if (out != null && options != null) {
112+
if (out != null && out.getOpenAPI() != null && options != null) {
113113
if (options.isResolveFully()) {
114114
new ResolverFully(options.isResolveCombinators()).resolveFully(out.getOpenAPI());
115115
}

modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.swagger.parser;
22

3+
import io.swagger.v3.core.util.Yaml;
34
import io.swagger.v3.oas.models.Components;
45
import io.swagger.v3.oas.models.OpenAPI;
56
import io.swagger.v3.oas.models.media.ArraySchema;
@@ -27,9 +28,19 @@
2728
import static org.testng.Assert.assertEquals;
2829
import static org.testng.Assert.assertNotNull;
2930
import static org.testng.Assert.assertTrue;
31+
import static org.testng.AssertJUnit.assertNull;
3032

3133
public class OpenAPIParserTest {
3234

35+
@Test
36+
public void testNPE_1685() {
37+
OpenAPIParser openAPIParser = new OpenAPIParser();
38+
ParseOptions options = new ParseOptions();
39+
options.setResolveFully(true);
40+
SwaggerParseResult swaggerParseResult = openAPIParser.readLocation("issue1685.json", null, options);
41+
assertNull(swaggerParseResult.getOpenAPI());
42+
}
43+
3344
@Test
3445
public void testIssue1608(){
3546
ParseOptions options = new ParseOptions();
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"notswagger": "x.0",
3+
"info": {
4+
"description": "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.",
5+
"version": "1.0.5",
6+
"title": "Swagger Petstore",
7+
"termsOfService": "http://swagger.io/terms/",
8+
"contact": {
9+
"email": "[email protected]"
10+
},
11+
"license": {
12+
"name": "Apache 2.0",
13+
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
14+
}
15+
},
16+
"host": "petstore.swagger.io",
17+
"basePath": "/v2",
18+
"schemes": [
19+
"https",
20+
"http"
21+
],
22+
"paths": {
23+
"/pet": {
24+
"put": {
25+
"summary": "Update an existing pet",
26+
"description": "",
27+
"operationId": "updatePet",
28+
"consumes": [
29+
"application/json",
30+
"application/xml"
31+
],
32+
"produces": [
33+
"application/json",
34+
"application/xml"
35+
],
36+
"parameters": [
37+
{
38+
"in": "body",
39+
"name": "body",
40+
"description": "Pet object that needs to be added to the store",
41+
"required": true,
42+
"schema": {
43+
"$ref": "#/definitions/Pet"
44+
}
45+
}
46+
],
47+
"responses": {
48+
"400": {
49+
"description": "Invalid ID supplied"
50+
}
51+
}
52+
}
53+
}
54+
},
55+
"definitions": {
56+
"Pet": {
57+
"type": "object",
58+
"required": [
59+
"name"
60+
],
61+
"properties": {
62+
"name": {
63+
"type": "string",
64+
"example": "doggie"
65+
}
66+
},
67+
"xml": {
68+
"name": "Pet"
69+
}
70+
}
71+
},
72+
"externalDocs": {
73+
"description": "Find out more about Swagger",
74+
"url": "http://swagger.io"
75+
}
76+
}

0 commit comments

Comments
 (0)