Repository: postmanlabs/postman-code-generators
Pull request #143 (2019-12-23 11:11:49)

Empty formdata bugfix ruby


Patch:
diff --git a/codegens/java-okhttp/lib/parseRequest.js b/codegens/java-okhttp/lib/parseRequest.js
index 5897cd7..b380595 100644
--- a/codegens/java-okhttp/lib/parseRequest.js
+++ b/codegens/java-okhttp/lib/parseRequest.js
@@ -85,8 +85,11 @@ function parseBody (requestBody, indentString, trimFields) {
           variables: graphqlVariables
         }), trimFields)}");\n`;
       case 'formdata':
-        return 'RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)\n' +
-                        `${parseFormData(requestBody, indentString, trimFields)};\n`;
+        return requestBody.formdata.length ?
+          'RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)\n' +
+            `${parseFormData(requestBody, indentString, trimFields)};\n` :
+          'MediaType JSON = MediaType.parse("application/json; charset=utf-8");\n' +
+          'RequestBody body = RequestBody.create(JSON, "{}");\n';
         /* istanbul ignore next */
       case 'file':
         // return 'RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)\n' +
diff --git a/codegens/nodejs-native/lib/parseRequest.js b/codegens/nodejs-native/lib/parseRequest.js
index 07140e1..56c3b8e 100644
--- a/codegens/nodejs-native/lib/parseRequest.js
+++ b/codegens/nodejs-native/lib/parseRequest.js
@@ -36,7 +36,10 @@ function extractFormData (dataArray, indentString, trimBody) {
  */
 function generateMultipartFormData (requestbody) {
   const boundary = '------WebKitFormBoundary7MA4YWxkTrZu0gW\\r\\nContent-Disposition: form-data; ',
-    dataArray = requestbody[requestbody.mode],
+    dataArray = requestbody[requestbody.mode];
+  var postData = '';
+
+  if (dataArray.length) {
     postData = '"' + boundary + _.reduce(dataArray, (accumalator, dataArrayElement) => {
       if (!dataArrayElement.disabled || dataArrayElement.disabled === false) {
         const key = dataArrayElement.key.replace(/"/g, '\'');
@@ -60,6 +63,7 @@ function generateMultipartFormData (requestbody) {
       return accumalator;
       // eslint-disable-next-line no-useless-escape
     }, []).join(`${boundary}`) + '------WebKitFormBoundary7MA4YWxkTrZu0gW--\"';
+  }
 
   return postData;
 }
diff --git a/codegens/ocaml-cohttp/lib/ocaml.js b/codegens/ocaml-cohttp/lib/ocaml.js
index 3f670a4..01c1bce 100644
--- a/codegens/ocaml-cohttp/lib/ocaml.js
+++ b/codegens/ocaml-cohttp/lib/ocaml.js
@@ -77,7 +77,6 @@ function parseFormData (body, trim, indent) {
       if (!data.disabled || data.disabled === false) {
         const key = sanitize(data.key, 'formdata-key', trim);
 
-        /* istanbul ignore next */
         if (data.type === 'file') {
           const filename = data.src;
           accumalator.push(`${indent}[| ("name", "${key}"); ("fileName", "${filename}") |]`);
@@ -88,7 +87,6 @@ function parseFormData (body, trim, indent) {
         }
       }
       return accumalator;
-      // eslint-disable-next-line no-useless-escape
     }, []).join(';\n') + '\n|];;',
     bodySnippet = '';
 
@@ -117,7 +115,6 @@ function parseFormData (body, trim, indent) {
   return bodySnippet;
 }
 
-/* istanbul ignore next */
 /**
  * Parses file body from the Request
  *
diff --git a/codegens/powershell-restmethod/lib/index.js b/codegens/powershell-restmethod/lib/index.js
index 38ab003..579db4a 100644
--- a/codegens/powershell-restmethod/lib/index.js
+++ b/codegens/powershell-restmethod/lib/index.js
@@ -38,6 +38,10 @@ function parseURLEncodedBody (body) {
  * @param {boolean} trim trim body option
  */
 function parseFormData (body, trim) {
+  if (_.isEmpty(body)) {
+    return '$body = $null\n';
+  }
+
   var bodySnippet = '$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()\n';
   _.forEach(body, function (data) {
     if (!data.disabled) {
diff --git a/codegens/ruby/lib/util/parseBody.js b/codegens/ruby/lib/util/parseBody.js
index 9ab66f4..80af08e 100644
--- a/codegens/ruby/lib/util/parseBody.js
+++ b/codegens/ruby/lib/util/parseBody.js
@@ -12,7 +12,7 @@ module.exports = function (request, trimRequestBody) {
   // used to check whether body is present in the request and return accordingly
   if (request.body) {
     var requestBody = '',
-      bodyMap,
+      bodyMap = [],
       enabledBodyList;
 
     switch (request.body.mode) {
diff --git a/codegens/swift/lib/swift.js b/codegens/swift/lib/swift.js
index bb21458..b47d203 100644
--- a/codegens/swift/lib/swift.js
+++ b/codegens/swift/lib/swift.js
@@ -14,6 +14,9 @@ var _ = require('./lodash'),
  * @returns {String} request body in the desired format
  */
 function parseRawBody (body, mode, trim) {
+  if (_.isEmpty(body)) {
+    return '';
+  }
   var bodySnippet;
   bodySnippet = `let parameters = ${sanitize(body, mode, trim)}\n`;
   bodySnippet += 'let postData = parameters.data(using: .utf8)';
@@ -29,6 +32,9 @@ function parseRawBody (body, mode, trim) {
  * @returns {String} request body in the desired format
  */
 function parseGraphQL (body, mode, trim) {
+  if (_.isEmpty(body)) {
+    return '';
+  }
   let query = body.query,
     graphqlVariables, bodySnippet;
   try {
@@ -54,6 +60,9 @@ function parseGraphQL (body, mode, trim) {
  * @returns {String} request body in the desired format
  */
 function parseURLEncodedBody (body, mode, trim) {
+  if (_.isEmpty(body)) {
+    return '';
+  }
   var payload, bodySnippet;
   payload = _.reduce(body, function (accumulator, data) {
     if (!data.disabled) {
@@ -150,7 +159,7 @@ function parseFile () {
  * @returns {String} utility function for getting request body in the desired format
  */
 function parseBody (body, trim, indent) {
-  if (!_.isEmpty(body) && !_.isEmpty(body[body.mode])) {
+  if (!_.isEmpty(body)) {
     switch (body.mode) {
       case 'urlencoded':
         return parseURLEncodedBody(body.urlencoded, body.mode, trim);


Test patch:
diff --git a/test/codegen/newman/fixtures/formdataCollection.json b/test/codegen/newman/fixtures/formdataCollection.json
index 5a99232..09698c3 100644
--- a/test/codegen/newman/fixtures/formdataCollection.json
+++ b/test/codegen/newman/fixtures/formdataCollection.json
@@ -59,6 +59,29 @@
 				}
 			},
 			"response": []
+		},
+		{
+			"name": "POST empty Formdata",
+			"request": {
+				"method": "POST",
+				"header": [],
+				"body": {
+					"mode": "formdata",
+					"formdata": []
+				},
+				"url": {
+					"raw": "https://postman-echo.com/post",
+					"protocol": "https",
+					"host": [
+						"postman-echo",
+						"com"
+					],
+					"path": [
+						"post"
+					]
+				}
+			},
+			"response": []
 		}
 	],
 	"protocolProfileBehavior": {}