Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 29a7ef6

Browse files
authored
Merge pull request swagger-api#1075 from swagger-api/issue-1072
Fix NullPointerException when schema is null
2 parents 79c2348 + 390b7d6 commit 29a7ef6

2 files changed

Lines changed: 87 additions & 34 deletions

File tree

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,40 +2297,41 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
22972297

22982298
//sets default value according to the schema type
22992299
if(node.get("default")!= null) {
2300-
if(schema.getType().equals("array")) {
2301-
ArrayNode array = getArray("default", node, false, location, result);
2302-
if (array != null) {
2303-
schema.setDefault(array);
2304-
}
2305-
}else if(schema.getType().equals("string")) {
2306-
value = getString("default", node, false, location, result);
2307-
if (value != null) {
2308-
try {
2309-
schema.setDefault( getDecodedObject( schema, value));
2300+
if(!StringUtils.isBlank(schema.getType())) {
2301+
if (schema.getType().equals("array")) {
2302+
ArrayNode array = getArray("default", node, false, location, result);
2303+
if (array != null) {
2304+
schema.setDefault(array);
23102305
}
2311-
catch( ParseException e) {
2312-
result.invalidType( location, String.format( "default=`%s`", e.getMessage()), schema.getFormat(), node);
2306+
} else if (schema.getType().equals("string")) {
2307+
value = getString("default", node, false, location, result);
2308+
if (value != null) {
2309+
try {
2310+
schema.setDefault(getDecodedObject(schema, value));
2311+
} catch (ParseException e) {
2312+
result.invalidType(location, String.format("default=`%s`", e.getMessage()), schema.getFormat(), node);
2313+
}
2314+
}
2315+
} else if (schema.getType().equals("boolean")) {
2316+
bool = getBoolean("default", node, false, location, result);
2317+
if (bool != null) {
2318+
schema.setDefault(bool);
2319+
}
2320+
} else if (schema.getType().equals("object")) {
2321+
Object object = getObject("default", node, false, location, result);
2322+
if (object != null) {
2323+
schema.setDefault(object);
2324+
}
2325+
} else if (schema.getType().equals("integer")) {
2326+
Integer number = getInteger("default", node, false, location, result);
2327+
if (number != null) {
2328+
schema.setDefault(number);
2329+
}
2330+
} else if (schema.getType().equals("number")) {
2331+
BigDecimal number = getBigDecimal("default", node, false, location, result);
2332+
if (number != null) {
2333+
schema.setDefault(number);
23132334
}
2314-
}
2315-
}else if(schema.getType().equals("boolean")) {
2316-
bool = getBoolean("default", node, false, location, result);
2317-
if (bool != null) {
2318-
schema.setDefault(bool);
2319-
}
2320-
}else if(schema.getType().equals("object")) {
2321-
Object object = getObject("default", node, false, location, result);
2322-
if (object != null) {
2323-
schema.setDefault(object);
2324-
}
2325-
} else if(schema.getType().equals("integer")) {
2326-
Integer number = getInteger("default", node, false, location, result);
2327-
if (number != null) {
2328-
schema.setDefault(number);
2329-
}
2330-
} else if(schema.getType().equals("number")) {
2331-
BigDecimal number = getBigDecimal("default", node, false, location, result);
2332-
if (number != null) {
2333-
schema.setDefault(number);
23342335
}
23352336
}
23362337
}

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
66
import io.swagger.v3.core.util.Json;
7+
import io.swagger.v3.core.util.Yaml;
78
import io.swagger.v3.oas.models.Components;
89
import io.swagger.v3.oas.models.ExternalDocumentation;
910
import io.swagger.v3.oas.models.OpenAPI;
@@ -64,6 +65,59 @@
6465

6566
public class OpenAPIDeserializerTest {
6667

68+
@Test
69+
public void testIssue1072() throws Exception {
70+
String yaml = "openapi: 3.0.0\n" +
71+
"info:\n" +
72+
" title: Test\n" +
73+
" version: 1.0.0\n" +
74+
"\n" +
75+
"paths:\n" +
76+
" /value:\n" +
77+
" get:\n" +
78+
" operationId: getValues\n" +
79+
" responses:\n" +
80+
" 200:\n" +
81+
" description: Successful response\n" +
82+
" content:\n" +
83+
" application/json:\n" +
84+
" schema:\n" +
85+
" $ref: '#/components/schemas/ComponentA'\n" +
86+
"components:\n" +
87+
" schemas:\n" +
88+
" ComponentA:\n" +
89+
" description: Component A\n" +
90+
" type: object\n" +
91+
" allOf:\n" +
92+
" - type: object\n" +
93+
" properties:\n" +
94+
" attributeWithoutType:\n" +
95+
" allOf:\n" +
96+
" - $ref: '#/components/schemas/ComponentB'\n" +
97+
" default: \"coucou\"\n" +
98+
" attributeWithWrongType:\n" +
99+
" type: object\n" +
100+
" allOf:\n" +
101+
" - $ref: '#/components/schemas/ComponentB'\n" +
102+
" default: \"coucou\"\n" +
103+
" correctAttribute:\n" +
104+
" type: string\n" +
105+
" allOf:\n" +
106+
" - $ref: '#/components/schemas/ComponentB'\n" +
107+
" default: \"coucou\"\n" +
108+
" ComponentB:\n" +
109+
" description: Component B\n" +
110+
" type: string";
111+
112+
OpenAPIV3Parser parser = new OpenAPIV3Parser();
113+
114+
SwaggerParseResult result = parser.readContents(yaml,null,null);
115+
OpenAPI openAPI = result.getOpenAPI();
116+
assertNotNull(openAPI);
117+
118+
}
119+
120+
67121
@Test
68122
public void testEmptyDefinitions() throws Exception {
69123
String yaml = "openapi: 3.0.0\n" +
@@ -100,8 +154,6 @@ public void testEmptyDefinitions() throws Exception {
100154

101155
assertNotNull(openAPI.getComponents().getSchemas().get("mydefinition"));
102156

103-
104-
105157
}
106158

107159

0 commit comments

Comments
 (0)