Repository: swagger-api/swagger-parser
Pull request #775 (2018-07-21 05:17:28)

Fix for issue 765
Hi team,

This PR is to solve the issue #765 (requestBody not correctly populated when Parameters is a list of $refs (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 e94b9895..dcef9315 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
@@ -80,6 +80,7 @@ public class SwaggerConverter implements SwaggerParserExtension {
     private List<String> globalConsumes = new ArrayList<>();
     private List<String> globalProduces = new ArrayList<>();
     private Components components = new Components();
+    private Map<String, io.swagger.models.parameters.Parameter> globalV2Parameters = new HashMap<>();
 
     @Override
     public SwaggerParseResult readLocation(String url, List<AuthorizationValue> auths, ParseOptions options) {
@@ -185,6 +186,7 @@ public class SwaggerConverter implements SwaggerParserExtension {
         }
 
         if (swagger.getParameters() != null) {
+            globalV2Parameters.putAll(swagger.getParameters());
             swagger.getParameters().forEach((k, v) -> {
                 if ("body".equals(v.getIn())) {
                     components.addRequestBodies(k, convertParameterToRequestBody(v));
@@ -513,6 +515,16 @@ public class SwaggerConverter implements SwaggerParserExtension {
         return v3Path;
     }
 
+    private boolean isRefABodyParam(io.swagger.models.parameters.Parameter param) {
+        if (param instanceof RefParameter) {
+            RefParameter refParameter = (RefParameter) param;
+            String simpleRef = refParameter.getSimpleRef();
+            io.swagger.models.parameters.Parameter parameter = globalV2Parameters.get(simpleRef);
+            return "body".equals(parameter.getIn());
+        }
+        return false;
+    }
+
     public Operation convert(io.swagger.models.Operation v2Operation) {
         Operation operation = new Operation();
         if (StringUtils.isNotBlank(v2Operation.getDescription())) {
@@ -535,7 +547,14 @@ public class SwaggerConverter implements SwaggerParserExtension {
                 } else if ("body".equals(param.getIn())) {
                     operation.setRequestBody(convertParameterToRequestBody(param, v2Operation.getConsumes()));
                 } else {
-                    operation.addParametersItem(convert(param));
+                    Parameter convert = convert(param);
+                    String $ref = convert.get$ref();
+                    if ($ref != null && $ref.startsWith("#/components/requestBodies/") && isRefABodyParam(param)) {
+                        operation.setRequestBody(new RequestBody().$ref($ref));
+                    } else {
+                        operation.addParametersItem(convert);
+                    }
+                    //operation.addParametersItem(convert(param));
                 }
             }
 


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 c2458b1a..35c108be 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
@@ -80,6 +80,7 @@ public class V2ConverterTest {
     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 ISSUE_765_YAML = "issue-765.yaml";
 
     private static final String API_BATCH_PATH = "/api/batch/";
     private static final String PETS_PATH = "/pets";
@@ -636,6 +637,15 @@ public class V2ConverterTest {
         final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_762_JSON);
         assertNotNull(oas);
     } 
+  
+    @Test(description = "requestBody not correctly populated when Parameters is a list of $refs (OAS 2 to 3 conversion)")
+    public void testIssue765() throws Exception {
+        final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_765_YAML);
+        assertNotNull(oas);
+        RequestBody requestBody = oas.getPaths().get("/ping/{ActivityTypePath}").getPost().getRequestBody();
+        assertNotNull(requestBody);
+        assertEquals("#/components/requestBodies/Block", requestBody.get$ref());
+    }
 
     @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-765.yaml b/modules/swagger-parser-v2-converter/src/test/resources/issue-765.yaml
new file mode 100644
index 00000000..ff046ffe
--- /dev/null
+++ b/modules/swagger-parser-v2-converter/src/test/resources/issue-765.yaml
@@ -0,0 +1,43 @@
+swagger: '2.0'
+info:
+  description: 'Test'
+  version: 1.0.0
+  title: OpenAPI Test
+  license:
+    name: Apache-2.0
+    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
+host: petstore.swagger.io
+basePath: /v2
+schemes:
+  - http
+parameters:
+  ActivityTypePath:
+    in: path
+    name: type
+    required: true
+    type: string
+  Block:
+    in: body
+    name: block
+    required: true
+    schema:
+      properties:
+        visual_type:
+          type: string
+      type: object
+paths:
+  /ping/{ActivityTypePath}:
+    post:
+      summary: test
+      description: 'test it'
+      operationId: pingOp
+      consumes:
+        - application/json
+      produces:
+        - application/json
+      parameters:
+        - $ref: '#/parameters/Block'
+        - $ref: '#/parameters/ActivityTypePath'
+      responses:
+        '200':
+          description: OK
\ No newline at end of file