Skip to content

Use proper indentation for JSON bodies in Javascript and Nodejs codegens #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions codegens/js-fetch/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ function parseFormData (body, trim) {
* @param {Object} body Raw body data
* @param {boolean} trim trim body option
* @param {String} contentType Content type of the body being sent
* @param {String} indentString Indentation string
*/
function parseRawBody (body, trim, contentType) {
function parseRawBody (body, trim, contentType, indentString) {
var bodySnippet = 'var raw = ';
// Match any application type whose underlying structure is json
// For example application/vnd.api+json
// All of them have +json as suffix
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
try {
let jsonBody = JSON.parse(body);
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody)});\n`;
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody, null, indentString.length)});\n`;
}
catch (error) {
bodySnippet += `"${sanitize(body.toString(), trim)}";\n`;
Expand Down Expand Up @@ -130,7 +131,7 @@ function parseBody (body, trim, indentString, contentType) {
case 'urlencoded':
return parseURLEncodedBody(body.urlencoded, trim);
case 'raw':
return parseRawBody(body.raw, trim, contentType);
return parseRawBody(body.raw, trim, contentType, indentString);
case 'graphql':
return parseGraphQL(body.graphql, trim, indentString);
case 'formdata':
Expand Down
4 changes: 2 additions & 2 deletions codegens/js-fetch/test/unit/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('js-fetch convert function for test collection', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.contain('var raw = JSON.stringify({"data":{"hello":"world"}});');
expect(snippet).to.contain('JSON.stringify({\n "data": {\n "hello": "world"\n }\n});');
});
});

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

Expand Down
3 changes: 2 additions & 1 deletion codegens/js-jquery/lib/util/parseBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = function (request, trimRequestBody, indentation, contentType) {
// eslint-disable-next-line max-depth
try {
let jsonBody = JSON.parse(request.body[request.body.mode]);
requestBody += `${indentation}"data": JSON.stringify(${JSON.stringify(jsonBody)}),\n`;
requestBody += `${indentation}"data": JSON.stringify(${JSON.stringify(jsonBody,
null, indentation.length).replace(/\n/g, `\n${indentation}`)}),\n`;
}
catch (error) {
requestBody += `${indentation}"data": ` +
Expand Down
4 changes: 2 additions & 2 deletions codegens/js-jquery/test/unit/converter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('jQuery converter', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.contain('"data": JSON.stringify({"data":{"hello":"world"}})');
expect(snippet).to.contain('JSON.stringify({\n "data": {\n "hello": "world"\n }\n })');
});
});

Expand Down Expand Up @@ -163,7 +163,7 @@ describe('jQuery converter', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('"data": JSON.stringify({"json":"Test-Test"})');
expect(snippet).to.include('"data": JSON.stringify({\n "json": "Test-Test"\n }');
});
});

Expand Down
2 changes: 1 addition & 1 deletion codegens/js-jquery/test/unit/fixtures/snippetFixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"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",
"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",
"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",
"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",
"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",
"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",
"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",
"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",
Expand Down
7 changes: 4 additions & 3 deletions codegens/js-xhr/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ function parseURLEncodedBody (body) {
* @param {*} body Raw body data
* @param {*} trim trim body option
* @param {String} contentType Content type of the body being sent
* @param {String} indentString Indentation string
*/
function parseRawBody (body, trim, contentType) {
function parseRawBody (body, trim, contentType, indentString) {
var bodySnippet = 'var data = ';
// Match any application type whose underlying structure is json
// For example application/vnd.api+json
// All of them have +json as suffix
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
try {
let jsonBody = JSON.parse(body);
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody)});\n`;
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody, null, indentString.length)});\n`;
}
catch (error) {
bodySnippet += `"${sanitize(body.toString(), trim)}";\n`;
Expand Down Expand Up @@ -125,7 +126,7 @@ function parseBody (body, trim, indentString, contentType) {
case 'urlencoded':
return parseURLEncodedBody(body.urlencoded, trim);
case 'raw':
return parseRawBody(body.raw, trim, contentType);
return parseRawBody(body.raw, trim, contentType, indentString);
case 'graphql':
return parseGraphQL(body.graphql, trim, indentString);
case 'formdata':
Expand Down
4 changes: 2 additions & 2 deletions codegens/js-xhr/test/unit/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('js-xhr convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('var data = JSON.stringify({"json":"Test-Test"})');
expect(snippet).to.include('JSON.stringify({\n "json": "Test-Test"\n})');
});
});

Expand Down Expand Up @@ -99,7 +99,7 @@ describe('js-xhr convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.contain('var data = JSON.stringify({"data":{"hello":"world"}});');
expect(snippet).to.contain('JSON.stringify({\n "data": {\n "hello": "world"\n }\n})');
});
});

Expand Down
7 changes: 4 additions & 3 deletions codegens/nodejs-axios/lib/parseRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ function parseFormData (body, trim, ES6_enabled) {
* @param {boolean} trim trim body option
* @param {String} contentType Content type of the body being sent
* @param {boolean} ES6_enabled ES6 syntax option
* @param {String} indentString Indentation string
*/
function parseRawBody (body, trim, contentType, ES6_enabled) {
function parseRawBody (body, trim, contentType, ES6_enabled, indentString) {
var varDeclare = ES6_enabled ? 'let' : 'var',
bodySnippet = varDeclare + ' data = ';
// Match any application type whose underlying structure is json
Expand All @@ -86,7 +87,7 @@ function parseRawBody (body, trim, contentType, ES6_enabled) {
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
try {
let jsonBody = JSON.parse(body);
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody)});\n`;
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody, null, indentString.length)});\n`;
}
catch (error) {
bodySnippet += `'${sanitize(body.toString(), trim)}';\n`;
Expand Down Expand Up @@ -151,7 +152,7 @@ function parseBody (body, trim, indentString, contentType, ES6_enabled) {
case 'urlencoded':
return parseURLEncodedBody(body.urlencoded, trim, ES6_enabled);
case 'raw':
return parseRawBody(body.raw, trim, contentType, ES6_enabled);
return parseRawBody(body.raw, trim, contentType, ES6_enabled, indentString);
case 'graphql':
return parseGraphQL(body.graphql, trim, indentString, ES6_enabled);
case 'formdata':
Expand Down
4 changes: 2 additions & 2 deletions codegens/nodejs-axios/test/unit/snippet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('nodejs-axios convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.contain('var data = JSON.stringify({"data":{"hello":"world"}});');
expect(snippet).to.contain('JSON.stringify({\n "data": {\n "hello": "world"\n }\n})');
});
});

Expand Down Expand Up @@ -371,7 +371,7 @@ describe('nodejs-axios convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('var data = JSON.stringify({"json":"Test-Test"});');
expect(snippet).to.include('data = JSON.stringify({\n "json": "Test-Test"\n})');
});
});

Expand Down
2 changes: 1 addition & 1 deletion codegens/nodejs-native/lib/parseRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function parseBody (requestbody, indentString, trimBody, contentType) {
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
try {
let jsonBody = JSON.parse(requestbody[requestbody.mode]);
return `JSON.stringify(${JSON.stringify(jsonBody)})`;
return `JSON.stringify(${JSON.stringify(jsonBody, null, indentString.length)})`;
}
catch (error) {
return ` ${JSON.stringify(requestbody[requestbody.mode])}`;
Expand Down
4 changes: 2 additions & 2 deletions codegens/nodejs-native/test/unit/snippet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('nodejs-native convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.contain('var postData = JSON.stringify({"data":{"hello":"world"}});');
expect(snippet).to.contain('JSON.stringify({\n "data": {\n "hello": "world"\n }\n})');
});
});

Expand Down Expand Up @@ -267,7 +267,7 @@ describe('nodejs-native convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('var postData = JSON.stringify({"json":"Test-Test"})');
expect(snippet).to.include('var postData = JSON.stringify({\n "json": "Test-Test"\n})');
});
});
it('should generate snippets for no files in form data', function () {
Expand Down
3 changes: 2 additions & 1 deletion codegens/nodejs-request/lib/parseRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ function parseBody (requestbody, indentString, trimBody, contentType) {
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
try {
let jsonBody = JSON.parse(requestbody[requestbody.mode]);
return `body: JSON.stringify(${JSON.stringify(jsonBody)})\n`;
return `body: JSON.stringify(${JSON.stringify(jsonBody, null,
indentString.length).replace(/\n/g, '\n' + indentString)})\n`;
}
catch (error) {
return `body: '${sanitize(requestbody[requestbody.mode])}'\n`;
Expand Down
4 changes: 2 additions & 2 deletions codegens/nodejs-request/test/unit/snippet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('nodejs-request convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.contain('body: JSON.stringify({"data":{"hello":"world"}})');
expect(snippet).to.contain('body: JSON.stringify({\n "data": {\n "hello": "world"\n }\n })');
});
});

Expand Down Expand Up @@ -356,7 +356,7 @@ describe('nodejs-request convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('body: JSON.stringify({"json":"Test-Test"})');
expect(snippet).to.include('body: JSON.stringify({\n "json": "Test-Test"\n })');
});
});

Expand Down
3 changes: 2 additions & 1 deletion codegens/nodejs-unirest/lib/parseRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ function parseBody (requestbody, indentString, trimBody, contentType) {
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
try {
let jsonBody = JSON.parse(requestbody[requestbody.mode]);
return `${indentString}.send(JSON.stringify(${JSON.stringify(jsonBody)}))\n`;
return `${indentString}.send(JSON.stringify(${JSON.stringify(jsonBody, null,
indentString.length).replace(/\n/g, '\n' + indentString)}))\n`;
}
catch (error) {
return indentString + '.send(' + JSON.stringify(requestbody[requestbody.mode]) + ')\n';
Expand Down
4 changes: 2 additions & 2 deletions codegens/nodejs-unirest/test/unit/snippet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('nodejs unirest convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.contain('send(JSON.stringify({"data":{"hello":"world"}}))');
expect(snippet).to.contain('JSON.stringify({\n "data": {\n "hello": "world"\n }\n })');
});
});

Expand Down Expand Up @@ -193,7 +193,7 @@ describe('nodejs unirest convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('.send(JSON.stringify({"json":"Test-Test"}))');
expect(snippet).to.include('.send(JSON.stringify({\n "json": "Test-Test"\n }))');
});
});

Expand Down