Repository: swagger-api/swagger-parser
Pull request #757 (2018-07-06 21:59:32)

Fix for issue 756
Hi team,

This PR is to solve the issue 'OpenAPI v2 converter: NullPointerException when schema field is missing in body parameter' as mentioned in #756 

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 72929847..49008a07 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
@@ -1051,6 +1051,9 @@ public class SwaggerConverter implements SwaggerParserExtension {
     }
 
     public Schema convert(io.swagger.models.Model v2Model) {
+        if (v2Model == null) {
+            return null;
+        }
         Schema result;
 
         if (v2Model instanceof ArrayModel) {


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 a9506d5c..070de49a 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
@@ -77,6 +77,7 @@ public class V2ConverterTest {
     private static final String ISSUE_673_YAML = "issue-673.yaml";
     private static final String ISSUE_676_JSON = "issue-676.json";
     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 API_BATCH_PATH = "/api/batch/";
@@ -617,6 +618,12 @@ public class V2ConverterTest {
         assertEquals(schema.getPattern(), "^[0-9]+$");
     }
 
+    @Test(description = "OpenAPI v2 converter - no model in body parameter")
+    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);
diff --git a/modules/swagger-parser-v2-converter/src/test/resources/issue-756.json b/modules/swagger-parser-v2-converter/src/test/resources/issue-756.json
new file mode 100644
index 00000000..e1d90dc5
--- /dev/null
+++ b/modules/swagger-parser-v2-converter/src/test/resources/issue-756.json
@@ -0,0 +1,38 @@
+{
+  "swagger": "2.0",
+  "info": {
+    "version": "1",
+    "title": "EchoSoap"
+  },
+  "paths": {
+    "/echo": {
+      "post": {
+        "consumes": [
+          "application/json",
+          "application/xml"
+        ],
+        "produces": [
+          "application/json",
+          "application/xml"
+        ],
+        "parameters": [
+          {
+            "in": "body",
+            "required": false,
+            "x-examples": {
+              "application/json": "{}"
+            }
+          }
+        ],
+        "responses": {
+          "200": {
+            "examples": {
+              "application/json": "{}"
+            }
+          }
+        }
+      },
+      "parameters": []
+    }
+  }
+}
\ No newline at end of file