Repository: swagger-api/swagger-parser
Pull request #1075 (2019-04-14 01:46:36)

Fix NullPointerException when schema is null
fix and test issue #1072


Patch:
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java
index fd0c604b..e0181302 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java
@@ -2297,40 +2297,41 @@ public class OpenAPIDeserializer {
 
         //sets default value according to the schema type
         if(node.get("default")!= null) {
-            if(schema.getType().equals("array")) {
-                ArrayNode array = getArray("default", node, false, location, result);
-                if (array != null) {
-                    schema.setDefault(array);
-                }
-            }else if(schema.getType().equals("string")) {
-                value = getString("default", node, false, location, result);
-                if (value != null) {
-                    try {
-                        schema.setDefault( getDecodedObject( schema, value));
+            if(!StringUtils.isBlank(schema.getType())) {
+                if (schema.getType().equals("array")) {
+                    ArrayNode array = getArray("default", node, false, location, result);
+                    if (array != null) {
+                        schema.setDefault(array);
                     }
-                    catch( ParseException e) {
-                        result.invalidType( location, String.format( "default=`%s`", e.getMessage()), schema.getFormat(), node);
+                } else if (schema.getType().equals("string")) {
+                    value = getString("default", node, false, location, result);
+                    if (value != null) {
+                        try {
+                            schema.setDefault(getDecodedObject(schema, value));
+                        } catch (ParseException e) {
+                            result.invalidType(location, String.format("default=`%s`", e.getMessage()), schema.getFormat(), node);
+                        }
+                    }
+                } else if (schema.getType().equals("boolean")) {
+                    bool = getBoolean("default", node, false, location, result);
+                    if (bool != null) {
+                        schema.setDefault(bool);
+                    }
+                } else if (schema.getType().equals("object")) {
+                    Object object = getObject("default", node, false, location, result);
+                    if (object != null) {
+                        schema.setDefault(object);
+                    }
+                } else if (schema.getType().equals("integer")) {
+                    Integer number = getInteger("default", node, false, location, result);
+                    if (number != null) {
+                        schema.setDefault(number);
+                    }
+                } else if (schema.getType().equals("number")) {
+                    BigDecimal number = getBigDecimal("default", node, false, location, result);
+                    if (number != null) {
+                        schema.setDefault(number);
                     }
-                }
-            }else if(schema.getType().equals("boolean")) {
-                bool = getBoolean("default", node, false, location, result);
-                if (bool != null) {
-                    schema.setDefault(bool);
-                }
-            }else if(schema.getType().equals("object")) {
-                Object object = getObject("default", node, false, location, result);
-                if (object != null) {
-                    schema.setDefault(object);
-                }
-            } else if(schema.getType().equals("integer")) {
-                Integer number = getInteger("default", node, false, location, result);
-                if (number != null) {
-                    schema.setDefault(number);
-                }
-            } else if(schema.getType().equals("number")) {
-                BigDecimal number = getBigDecimal("default", node, false, location, result);
-                if (number != null) {
-                    schema.setDefault(number);
                 }
             }
         }


Test patch:
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java
index dca3900b..c2da4671 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
 import io.swagger.v3.core.util.Json;
+import io.swagger.v3.core.util.Yaml;
 import io.swagger.v3.oas.models.Components;
 import io.swagger.v3.oas.models.ExternalDocumentation;
 import io.swagger.v3.oas.models.OpenAPI;
@@ -64,6 +65,59 @@ import static org.testng.Assert.assertFalse;
 
 public class OpenAPIDeserializerTest {
 
+    @Test
+    public void testIssue1072() throws Exception {
+        String yaml = "openapi: 3.0.0\n" +
+                "info:\n" +
+                "  title: Test\n" +
+                "  version: 1.0.0\n" +
+                "\n" +
+                "paths:\n" +
+                "  /value:\n" +
+                "    get:\n" +
+                "      operationId: getValues\n" +
+                "      responses:\n" +
+                "        200:\n" +
+                "          description: Successful response\n" +
+                "          content:\n" +
+                "            application/json:\n" +
+                "              schema:\n" +
+                "                $ref: '#/components/schemas/ComponentA'\n" +
+                "components:\n" +
+                "  schemas:\n" +
+                "    ComponentA:\n" +
+                "      description: Component A\n" +
+                "      type: object\n" +
+                "      allOf:\n" +
+                "        - type: object\n" +
+                "          properties:\n" +
+                "            attributeWithoutType:\n" +
+                "              allOf:\n" +
+                "              - $ref: '#/components/schemas/ComponentB'\n" +
+                "              default: \"coucou\"\n" +
+                "            attributeWithWrongType:\n" +
+                "              type: object\n" +
+                "              allOf:\n" +
+                "                - $ref: '#/components/schemas/ComponentB'\n" +
+                "              default: \"coucou\"\n" +
+                "            correctAttribute:\n" +
+                "              type: string\n" +
+                "              allOf:\n" +
+                "                - $ref: '#/components/schemas/ComponentB'\n" +
+                "              default: \"coucou\"\n" +
+                "    ComponentB:\n" +
+                "      description: Component B\n" +
+                "      type: string";
+
+        OpenAPIV3Parser parser = new OpenAPIV3Parser();
+
+        SwaggerParseResult result = parser.readContents(yaml,null,null);
+        OpenAPI openAPI = result.getOpenAPI();
+        assertNotNull(openAPI);
+
+    }
+
+
     @Test
     public void testEmptyDefinitions() throws Exception {
         String yaml = "openapi: 3.0.0\n" +
@@ -100,8 +154,6 @@ public class OpenAPIDeserializerTest {
 
         assertNotNull(openAPI.getComponents().getSchemas().get("mydefinition"));
 
-
-
     }