Repository: swagger-api/swagger-parser
Pull request #763 (2018-07-07 15:55:55)

Fix for issue 762
Hi team,

This PR is to fix the issue 'OpenAPI v2 Converter: NPE when type is array and 'items' field is missing in array property' [762](https://github.com/swagger-api/swagger-parser/issues/762).

Please review the PR and merge the same.

Thanks,
Mohammed


Patch:
diff --git a/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java b/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java
index 49008a07..e94b9895 100644
--- a/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java
+++ b/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java
@@ -815,6 +815,9 @@ public class SwaggerConverter implements SwaggerParserExtension {
     }
 
     private Schema convert(Property schema) {
+        if (schema == null) {
+            return null;
+        }
         Schema result;
 
         if (schema instanceof RefProperty) {


Test patch:
diff --git a/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java b/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java
index 070de49a..c2458b1a 100644
--- a/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java
+++ b/modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java
@@ -79,6 +79,7 @@ public class V2ConverterTest {
     private static final String ISSUE_708_YAML = "issue-708.yaml";
     private static final String ISSUE_756_JSON = "issue-756.json";
     private static final String ISSUE_758_JSON = "issue-758.json";
+    private static final String ISSUE_762_JSON = "issue-762.json";
 
     private static final String API_BATCH_PATH = "/api/batch/";
     private static final String PETS_PATH = "/pets";
@@ -622,13 +623,19 @@ public class V2ConverterTest {
     public void testIssue756() throws Exception {
         OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_756_JSON);
           assertNotNull(oas);
-      }
+    }
 
     @Test(description = "OpenAPI v2 converter - NPE when 'enum' field is available and 'type' field is missing in query parameter")
     public void testIssue758() throws Exception {
         final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_758_JSON);
         assertNotNull(oas);
     }
+  
+    @Test(description = "OpenAPI v2 Converter: NPE when type is array and 'items' field is missing in array property")
+    public void testIssue762() throws Exception {
+        final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_762_JSON);
+        assertNotNull(oas);
+    } 
 
     @Test(description = "OpenAPI v2 converter - Missing Parameter.style property")
     public void testParameterConversion() throws Exception {
diff --git a/modules/swagger-parser-v2-converter/src/test/resources/issue-762.json b/modules/swagger-parser-v2-converter/src/test/resources/issue-762.json
new file mode 100644
index 00000000..411aea43
--- /dev/null
+++ b/modules/swagger-parser-v2-converter/src/test/resources/issue-762.json
@@ -0,0 +1,47 @@
+{
+  "swagger": "2.0",
+  "info": {
+    "version": "1.0.0",
+    "title": "PetStore on Heroku",
+    "description": "**This example has a working backend hosted in Heroku**\n\nYou can try all HTTP operation described in this Swagger spec.\n\nFind source code of this API [here](https://github.com/mohsen1/petstore-api)\n"
+  },
+  "host": "petstore-api.herokuapp.com",
+  "basePath": "/pet",
+  "schemes": [
+    "http",
+    "https"
+  ],
+  "paths": {
+    "/": {
+      "put": {
+        "parameters": [
+          {
+            "name": "pet",
+            "in": "body",
+            "description": "The pet JSON you want to post",
+            "schema": {
+              "$ref": "#/definitions/Pet"
+            },
+            "required": true
+          }
+        ],
+        "responses": {
+          "200": {
+            "description": "Updates the pet"
+          }
+        }
+      }
+    }
+  },
+  "definitions": {
+    "Pet": {
+      "type": "object",
+      "required" : ["name"],
+      "properties": {
+        "name": {
+          "type": "array"
+        }
+      }
+    }
+  }
+}
\ No newline at end of file