Repository: swagger-api/swagger-parser
Pull request #787 (2018-08-06 05:28:50)

Fix for issue 786 (Open API V2 Converter: NPE when flow field is missing in securityDefinitions)
Hi team,

This PR is to solve the issue #786 (Open API V2 Converter: NPE when flow field is missing in securityDefinitions (OAS 2 to 3 conversion))

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 5607edfe..556ad7b3 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
@@ -297,25 +297,28 @@ public class SwaggerConverter implements SwaggerParserExtension {
         OAuthFlow oAuthFlow = new OAuthFlow();
 
         securityScheme.setType(SecurityScheme.Type.OAUTH2);
-
-        switch (oAuth2Definition.getFlow()) {
-            case "implicit":
-                oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
-                oAuthFlows.setImplicit(oAuthFlow);
-                break;
-            case "password":
-                oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
-                oAuthFlows.setPassword(oAuthFlow);
-                break;
-            case "application":
-                oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
-                oAuthFlows.setClientCredentials(oAuthFlow);
-                break;
-            case "accessCode":
-                oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
-                oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
-                oAuthFlows.setAuthorizationCode(oAuthFlow);
-                break;
+        String flow = oAuth2Definition.getFlow();
+
+        if (flow != null) {
+            switch (flow) {
+                case "implicit":
+                    oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
+                    oAuthFlows.setImplicit(oAuthFlow);
+                    break;
+                case "password":
+                    oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
+                    oAuthFlows.setPassword(oAuthFlow);
+                    break;
+                case "application":
+                    oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
+                    oAuthFlows.setClientCredentials(oAuthFlow);
+                    break;
+                case "accessCode":
+                    oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
+                    oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
+                    oAuthFlows.setAuthorizationCode(oAuthFlow);
+                    break;
+            }
         }
 
         Scopes scopes = new Scopes();


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 c97cb664..3758e77b 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
@@ -82,6 +82,7 @@ public class V2ConverterTest {
     private static final String ISSUE_758_JSON = "issue-758.json";
     private static final String ISSUE_762_JSON = "issue-762.json";
     private static final String ISSUE_765_YAML = "issue-765.yaml";
+    private static final String ISSUE_768_JSON = "issue-786.json";
 
     private static final String API_BATCH_PATH = "/api/batch/";
     private static final String PETS_PATH = "/pets";
@@ -692,6 +693,12 @@ public class V2ConverterTest {
         assertNotNull(result.getMessages());
     }
 
+    @Test(description = "OpenAPI v2 converter - Migrate minLength, maxLength and pattern of String property")
+    public void testIssue786() throws Exception {
+        final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_768_JSON);
+        assertNotNull(oas);
+    }
+
     private OpenAPI getConvertedOpenAPIFromJsonFile(String file) throws IOException, URISyntaxException {
         SwaggerConverter converter = new SwaggerConverter();
         String swaggerAsString = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource(file).toURI())));
diff --git a/modules/swagger-parser-v2-converter/src/test/resources/issue-786.json b/modules/swagger-parser-v2-converter/src/test/resources/issue-786.json
new file mode 100644
index 00000000..4d71c0b2
--- /dev/null
+++ b/modules/swagger-parser-v2-converter/src/test/resources/issue-786.json
@@ -0,0 +1,32 @@
+{
+  "swagger": "2.0",
+  "host" : "localhost",
+  "info": {
+    "version": "1.0.9-abcd",
+    "title": "Swagger Sample API Security Example",
+    "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
+    "termsOfService": "http://swagger.io/terms/",
+    "contact": {
+      "name": "Swagger API Team",
+      "url": "http://swagger.io"
+    },
+    "license": {
+      "name": "Creative Commons 4.0 International",
+      "url": "http://creativecommons.org/licenses/by/4.0/"
+    }
+  },
+  "basePath": "/v1",
+  "schemes": [
+    "http",
+    "https"
+  ],
+  "securityDefinitions": {
+    "petstoreImplicit": {
+      "type": "oauth2",
+      "scopes": {
+        "user": "Grants read/write access to profile info only. Note that this scope includes user:email and user:follow."
+      },
+      "authorizationUrl": "http://petstore.swagger.io/oauth/dialog"
+    }
+  }
+}
\ No newline at end of file