Skip to content

Commit 1764b27

Browse files
author
Ankit Saini
authored
Merge pull request #431 from postmanlabs/fix/json-indentation
Use proper indentation for JSON bodies in Javascript and Nodejs codegens
2 parents b8c2d10 + bd5e8b3 commit 1764b27

File tree

15 files changed

+34
-28
lines changed

15 files changed

+34
-28
lines changed

codegens/js-fetch/lib/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,17 @@ function parseFormData (body, trim) {
6262
* @param {Object} body Raw body data
6363
* @param {boolean} trim trim body option
6464
* @param {String} contentType Content type of the body being sent
65+
* @param {String} indentString Indentation string
6566
*/
66-
function parseRawBody (body, trim, contentType) {
67+
function parseRawBody (body, trim, contentType, indentString) {
6768
var bodySnippet = 'var raw = ';
6869
// Match any application type whose underlying structure is json
6970
// For example application/vnd.api+json
7071
// All of them have +json as suffix
7172
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
7273
try {
7374
let jsonBody = JSON.parse(body);
74-
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody)});\n`;
75+
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody, null, indentString.length)});\n`;
7576
}
7677
catch (error) {
7778
bodySnippet += `"${sanitize(body.toString(), trim)}";\n`;
@@ -130,7 +131,7 @@ function parseBody (body, trim, indentString, contentType) {
130131
case 'urlencoded':
131132
return parseURLEncodedBody(body.urlencoded, trim);
132133
case 'raw':
133-
return parseRawBody(body.raw, trim, contentType);
134+
return parseRawBody(body.raw, trim, contentType, indentString);
134135
case 'graphql':
135136
return parseGraphQL(body.graphql, trim, indentString);
136137
case 'formdata':

codegens/js-fetch/test/unit/convert.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe('js-fetch convert function for test collection', function () {
7979
expect.fail(null, null, error);
8080
}
8181
expect(snippet).to.be.a('string');
82-
expect(snippet).to.contain('var raw = JSON.stringify({"data":{"hello":"world"}});');
82+
expect(snippet).to.contain('JSON.stringify({\n "data": {\n "hello": "world"\n }\n});');
8383
});
8484
});
8585

@@ -211,7 +211,7 @@ describe('js-fetch convert function for test collection', function () {
211211
expect.fail(null, null, error);
212212
}
213213
expect(snippet).to.be.a('string');
214-
expect(snippet).to.include('var raw = JSON.stringify({"json":"Test-Test"})');
214+
expect(snippet).to.include('JSON.stringify({\n "json": "Test-Test"\n})');
215215
});
216216
});
217217

codegens/js-jquery/lib/util/parseBody.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module.exports = function (request, trimRequestBody, indentation, contentType) {
2727
// eslint-disable-next-line max-depth
2828
try {
2929
let jsonBody = JSON.parse(request.body[request.body.mode]);
30-
requestBody += `${indentation}"data": JSON.stringify(${JSON.stringify(jsonBody)}),\n`;
30+
requestBody += `${indentation}"data": JSON.stringify(${JSON.stringify(jsonBody,
31+
null, indentation.length).replace(/\n/g, `\n${indentation}`)}),\n`;
3132
}
3233
catch (error) {
3334
requestBody += `${indentation}"data": ` +

codegens/js-jquery/test/unit/converter.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('jQuery converter', function () {
103103
expect.fail(null, null, error);
104104
}
105105
expect(snippet).to.be.a('string');
106-
expect(snippet).to.contain('"data": JSON.stringify({"data":{"hello":"world"}})');
106+
expect(snippet).to.contain('JSON.stringify({\n "data": {\n "hello": "world"\n }\n })');
107107
});
108108
});
109109

@@ -163,7 +163,7 @@ describe('jQuery converter', function () {
163163
expect.fail(null, null, error);
164164
}
165165
expect(snippet).to.be.a('string');
166-
expect(snippet).to.include('"data": JSON.stringify({"json":"Test-Test"})');
166+
expect(snippet).to.include('"data": JSON.stringify({\n "json": "Test-Test"\n }');
167167
});
168168
});
169169

codegens/js-jquery/test/unit/fixtures/snippetFixtures.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"Resolve URL (Quotes + Special Characters) Copy": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%3Fa%3D%21@%24%5E*%28%29_-%60%2526%26b%3D%2C./%27%3B%5B%5D%7D%7B%5C%22%3A/%3F%3E%3C%7C%7C%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B",
88
"POST Raw Text": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22application/x-www-form-urlencoded%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20%22Duis%20posuere%20augue%20vel%20cursus%20pharetra.%20In%20luctus%20a%20ex%20nec%20pretium.%20Praesent%20neque%20quam%2C%20tincidunt%20nec%20leo%20eget%2C%20rutrum%20vehicula%20magna.%5CnMaecenas%20consequat%20elementum%20elit%2C%20id%20semper%20sem%20tristique%20et.%20Integer%20pulvinar%20enim%20quis%20consectetur%20interdum%20volutpat.%21@%23%24%25%5E%26*%28%29+POL%3A%7D%2C%27%27%3B%2C%5B%3B%5B%3B%22%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B",
99
"POST urlencoded data with disabled entries": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22application/x-www-form-urlencoded%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%221%22%3A%20%22%27a%27%22%2C%0A%20%20%20%20%20%20%20%20%222%22%3A%20%22%5C%22b%5C%22%22%0A%20%20%20%20%7D%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B",
10-
"POST json with raw": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22application/json%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20JSON.stringify%28%7B%22json%22%3A%22Test-Test%21@%23%24%25%5E%26*%28%29+POL%3A%7D%2C%27%27%3B%2C%5B%3B%5B%3B%3A%3E%22%7D%29%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B",
10+
"POST json with raw": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22application/json%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20JSON.stringify%28%7B%0A%20%20%20%20%20%20%20%20%22json%22%3A%20%22Test-Test%21@%23%24%25%5E%26*%28%29+POL%3A%7D%2C%27%27%3B%2C%5B%3B%5B%3B%3A%3E%22%0A%20%20%20%20%7D%29%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B",
1111
"POST javascript with raw": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22application/javascript%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20%22var%20val%20%3D%206%3B%5Cnconsole.log%28val%29%3B%22%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B",
1212
"POST text/xml with raw": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22text/xml%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20%22%3Cxml%3E%5Cn%5CtTest%20Test%21@%23%24%25%5E%26*%28%29+POL%3A%7D%2C%27%27%3B%2C%5B%3B%5B%3B%5Cn%3C/xml%3E%22%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B",
1313
"POST text/html with raw": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22text/html%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20%22%3Chtml%3E%5Cn%20%20Test%20Test%20%21@%23%24%25%5E%26*%28%29+POL%3A%7D%2C%27%27%3B%2C%5B%3B%5B%3B%5Cn%3C/html%3E%22%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B",

codegens/js-xhr/lib/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@ function parseURLEncodedBody (body) {
2727
* @param {*} body Raw body data
2828
* @param {*} trim trim body option
2929
* @param {String} contentType Content type of the body being sent
30+
* @param {String} indentString Indentation string
3031
*/
31-
function parseRawBody (body, trim, contentType) {
32+
function parseRawBody (body, trim, contentType, indentString) {
3233
var bodySnippet = 'var data = ';
3334
// Match any application type whose underlying structure is json
3435
// For example application/vnd.api+json
3536
// All of them have +json as suffix
3637
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
3738
try {
3839
let jsonBody = JSON.parse(body);
39-
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody)});\n`;
40+
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody, null, indentString.length)});\n`;
4041
}
4142
catch (error) {
4243
bodySnippet += `"${sanitize(body.toString(), trim)}";\n`;
@@ -125,7 +126,7 @@ function parseBody (body, trim, indentString, contentType) {
125126
case 'urlencoded':
126127
return parseURLEncodedBody(body.urlencoded, trim);
127128
case 'raw':
128-
return parseRawBody(body.raw, trim, contentType);
129+
return parseRawBody(body.raw, trim, contentType, indentString);
129130
case 'graphql':
130131
return parseGraphQL(body.graphql, trim, indentString);
131132
case 'formdata':

codegens/js-xhr/test/unit/convert.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('js-xhr convert function', function () {
6565
expect.fail(null, null, error);
6666
}
6767
expect(snippet).to.be.a('string');
68-
expect(snippet).to.include('var data = JSON.stringify({"json":"Test-Test"})');
68+
expect(snippet).to.include('JSON.stringify({\n "json": "Test-Test"\n})');
6969
});
7070
});
7171

@@ -99,7 +99,7 @@ describe('js-xhr convert function', function () {
9999
expect.fail(null, null, error);
100100
}
101101
expect(snippet).to.be.a('string');
102-
expect(snippet).to.contain('var data = JSON.stringify({"data":{"hello":"world"}});');
102+
expect(snippet).to.contain('JSON.stringify({\n "data": {\n "hello": "world"\n }\n})');
103103
});
104104
});
105105

codegens/nodejs-axios/lib/parseRequest.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ function parseFormData (body, trim, ES6_enabled) {
7777
* @param {boolean} trim trim body option
7878
* @param {String} contentType Content type of the body being sent
7979
* @param {boolean} ES6_enabled ES6 syntax option
80+
* @param {String} indentString Indentation string
8081
*/
81-
function parseRawBody (body, trim, contentType, ES6_enabled) {
82+
function parseRawBody (body, trim, contentType, ES6_enabled, indentString) {
8283
var varDeclare = ES6_enabled ? 'let' : 'var',
8384
bodySnippet = varDeclare + ' data = ';
8485
// Match any application type whose underlying structure is json
@@ -87,7 +88,7 @@ function parseRawBody (body, trim, contentType, ES6_enabled) {
8788
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
8889
try {
8990
let jsonBody = JSON.parse(body);
90-
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody)});\n`;
91+
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody, null, indentString.length)});\n`;
9192
}
9293
catch (error) {
9394
bodySnippet += `'${sanitize(body.toString(), trim)}';\n`;
@@ -152,7 +153,7 @@ function parseBody (body, trim, indentString, contentType, ES6_enabled) {
152153
case 'urlencoded':
153154
return parseURLEncodedBody(body.urlencoded, trim, ES6_enabled, indentString);
154155
case 'raw':
155-
return parseRawBody(body.raw, trim, contentType, ES6_enabled);
156+
return parseRawBody(body.raw, trim, contentType, ES6_enabled, indentString);
156157
case 'graphql':
157158
return parseGraphQL(body.graphql, trim, indentString, ES6_enabled);
158159
case 'formdata':

codegens/nodejs-axios/test/unit/snippet.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('nodejs-axios convert function', function () {
8080
expect.fail(null, null, error);
8181
}
8282
expect(snippet).to.be.a('string');
83-
expect(snippet).to.contain('var data = JSON.stringify({"data":{"hello":"world"}});');
83+
expect(snippet).to.contain('JSON.stringify({\n "data": {\n "hello": "world"\n }\n})');
8484
});
8585
});
8686

@@ -371,7 +371,7 @@ describe('nodejs-axios convert function', function () {
371371
expect.fail(null, null, error);
372372
}
373373
expect(snippet).to.be.a('string');
374-
expect(snippet).to.include('var data = JSON.stringify({"json":"Test-Test"});');
374+
expect(snippet).to.include('data = JSON.stringify({\n "json": "Test-Test"\n})');
375375
});
376376
});
377377

codegens/nodejs-native/lib/parseRequest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function parseBody (requestbody, indentString, trimBody, contentType) {
9191
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
9292
try {
9393
let jsonBody = JSON.parse(requestbody[requestbody.mode]);
94-
return `JSON.stringify(${JSON.stringify(jsonBody)})`;
94+
return `JSON.stringify(${JSON.stringify(jsonBody, null, indentString.length)})`;
9595
}
9696
catch (error) {
9797
return ` ${JSON.stringify(requestbody[requestbody.mode])}`;

0 commit comments

Comments
 (0)