Skip to content

Commit 24d796d

Browse files
author
Ankit Saini
authored
Merge pull request #477 from postmanlabs/feature/json-formatting
Add proper formatting of JSON in Dart and C#
2 parents 34ebe47 + b3f9126 commit 24d796d

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

codegens/csharp-restsharp/lib/parseRequest.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ function parseBody (request, trimFields) {
7878
case 'formdata':
7979
return parseFormData(requestBody, trimFields);
8080
case 'raw':
81-
return `request.AddParameter("${parseContentType(request)}", ` +
82-
`${JSON.stringify(requestBody[requestBody.mode])}, ParameterType.RequestBody);\n`;
81+
return `var body = ${requestBody[requestBody.mode]
82+
.split('\n')
83+
.map((line) => { return '@"' + line.replace(/"/g, '""') + '"'; })
84+
.join(' + "\\n" +\n')};\n` +
85+
`request.AddParameter("${parseContentType(request)}", ` +
86+
'body, ParameterType.RequestBody);\n';
8387
case 'graphql':
8488
return parseGraphQL(requestBody, trimFields);
8589
/* istanbul ignore next */

codegens/dart-http/lib/index.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,20 @@ function parseUrlEncoded (body, indent, trim) {
3030
*
3131
* @param {Object} body Raw body data
3232
* @param {Boolean} trim indicates whether to trim string or not
33+
* @param {String} contentType the content-type of request body
34+
* @param {Integer} indentCount the number of space to use
3335
*/
34-
function parseRawBody (body, trim) {
36+
function parseRawBody (body, trim, contentType, indentCount) {
37+
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
38+
try {
39+
let jsonBody = JSON.parse(body);
40+
return `request.body = json.encode(${JSON.stringify(jsonBody, null, indentCount)});`;
41+
42+
}
43+
catch (error) {
44+
// Do nothing
45+
}
46+
}
3547
return `request.body = '''${sanitize(body, trim)}''';`;
3648
}
3749

@@ -109,15 +121,16 @@ function parseFormData (body, indent, trim) {
109121
*
110122
* @param {Object} body body object from request.
111123
* @param {String} indent indentation required for code snippet
112-
* @param {trim} trim indicates whether to trim string or not
124+
* @param {Boolean} trim indicates whether to trim string or not
125+
* @param {String} contentType the content-type of the request body
113126
*/
114-
function parseBody (body, indent, trim) {
127+
function parseBody (body, indent, trim, contentType) {
115128
if (!_.isEmpty(body)) {
116129
switch (body.mode) {
117130
case 'urlencoded':
118131
return parseUrlEncoded(body.urlencoded, indent, trim);
119132
case 'raw':
120-
return parseRawBody(body.raw, trim);
133+
return parseRawBody(body.raw, trim, contentType, indent.length);
121134
case 'formdata':
122135
return parseFormData(body.formdata, indent, trim);
123136
case 'graphql':
@@ -167,13 +180,10 @@ self = module.exports = {
167180
footerSnippet = '',
168181
trim,
169182
timeout,
170-
followRedirect;
183+
followRedirect,
184+
contentType;
171185
options = sanitizeOptions(options, self.getOptions());
172-
if (options.includeBoilerplate) {
173-
headerSnippet = 'import \'package:http/http.dart\' as http;\n\n';
174-
headerSnippet += 'void main() async {\n';
175-
footerSnippet = '}\n';
176-
}
186+
177187
trim = options.trimRequestBody;
178188
indent = options.indentType === 'Tab' ? '\t' : ' ';
179189
indent = indent.repeat(options.indentCount);
@@ -199,6 +209,16 @@ self = module.exports = {
199209
}
200210
}
201211

212+
contentType = request.headers.get('Content-Type');
213+
if (options.includeBoilerplate) {
214+
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
215+
headerSnippet = 'import \'dart:convert\';\n';
216+
}
217+
headerSnippet += 'import \'package:http/http.dart\' as http;\n\n';
218+
headerSnippet += 'void main() async {\n';
219+
footerSnippet = '}\n';
220+
}
221+
202222
// The following code handles multiple files in the same formdata param.
203223
// It removes the form data params where the src property is an array of filepath strings
204224
// Splits that array into different form data params with src set as a single filepath string
@@ -243,7 +263,7 @@ self = module.exports = {
243263

244264
const headers = parseHeaders(request.headers.toJSON(), indent, trim),
245265
requestBody = request.body ? request.body.toJSON() : {},
246-
body = parseBody(requestBody, indent, trim) + '\n';
266+
body = parseBody(requestBody, indent, trim, contentType) + '\n';
247267

248268
codeSnippet += headers;
249269

0 commit comments

Comments
 (0)