From 22c5bf0997e76d032eb8726d153a4c2a87635bf9 Mon Sep 17 00:00:00 2001 From: Ankit Saini Date: Sun, 11 Apr 2021 16:09:52 +0530 Subject: [PATCH 1/3] In Dart codegen, use json.convert for application/json body --- codegens/dart-http/lib/index.js | 41 ++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/codegens/dart-http/lib/index.js b/codegens/dart-http/lib/index.js index 61857ee4c..8639c1638 100644 --- a/codegens/dart-http/lib/index.js +++ b/codegens/dart-http/lib/index.js @@ -30,8 +30,19 @@ function parseUrlEncoded (body, indent, trim) { * * @param {Object} body Raw body data * @param {Boolean} trim indicates whether to trim string or not + * @param {String} contentType the content-type of request body */ -function parseRawBody (body, trim) { +function parseRawBody (body, trim, contentType) { + if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) { + try { + let jsonBody = JSON.parse(body); + return `request.body = json.encode(${JSON.stringify(jsonBody, null, 4)});`; + + } + catch (error) { + // Do nothing + } + } return `request.body = '''${sanitize(body, trim)}''';`; } @@ -109,15 +120,16 @@ function parseFormData (body, indent, trim) { * * @param {Object} body body object from request. * @param {String} indent indentation required for code snippet - * @param {trim} trim indicates whether to trim string or not + * @param {Boolean} trim indicates whether to trim string or not + * @param {String} contentType the content-type of the request body */ -function parseBody (body, indent, trim) { +function parseBody (body, indent, trim, contentType) { if (!_.isEmpty(body)) { switch (body.mode) { case 'urlencoded': return parseUrlEncoded(body.urlencoded, indent, trim); case 'raw': - return parseRawBody(body.raw, trim); + return parseRawBody(body.raw, trim, contentType); case 'formdata': return parseFormData(body.formdata, indent, trim); case 'graphql': @@ -167,13 +179,10 @@ self = module.exports = { footerSnippet = '', trim, timeout, - followRedirect; + followRedirect, + contentType; options = sanitizeOptions(options, self.getOptions()); - if (options.includeBoilerplate) { - headerSnippet = 'import \'package:http/http.dart\' as http;\n\n'; - headerSnippet += 'void main() async {\n'; - footerSnippet = '}\n'; - } + trim = options.trimRequestBody; indent = options.indentType === 'Tab' ? '\t' : ' '; indent = indent.repeat(options.indentCount); @@ -199,6 +208,16 @@ self = module.exports = { } } + contentType = request.headers.get('Content-Type'); + if (options.includeBoilerplate) { + if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) { + headerSnippet = 'import \'dart:convert\';\n'; + } + headerSnippet += 'import \'package:http/http.dart\' as http;\n\n'; + headerSnippet += 'void main() async {\n'; + footerSnippet = '}\n'; + } + // The following code handles multiple files in the same formdata param. // It removes the form data params where the src property is an array of filepath strings // Splits that array into different form data params with src set as a single filepath string @@ -243,7 +262,7 @@ self = module.exports = { const headers = parseHeaders(request.headers.toJSON(), indent, trim), requestBody = request.body ? request.body.toJSON() : {}, - body = parseBody(requestBody, indent, trim) + '\n'; + body = parseBody(requestBody, indent, trim, contentType) + '\n'; codeSnippet += headers; From 26a479f41f955eaeec0ef8b41f7b689637f015d3 Mon Sep 17 00:00:00 2001 From: Ankit Saini Date: Sun, 11 Apr 2021 17:58:33 +0530 Subject: [PATCH 2/3] Preserve formatting of JSON bodies in Csharp --- codegens/csharp-restsharp/lib/parseRequest.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/codegens/csharp-restsharp/lib/parseRequest.js b/codegens/csharp-restsharp/lib/parseRequest.js index c900f42b7..4a8fec79c 100644 --- a/codegens/csharp-restsharp/lib/parseRequest.js +++ b/codegens/csharp-restsharp/lib/parseRequest.js @@ -78,8 +78,12 @@ function parseBody (request, trimFields) { case 'formdata': return parseFormData(requestBody, trimFields); case 'raw': - return `request.AddParameter("${parseContentType(request)}", ` + - `${JSON.stringify(requestBody[requestBody.mode])}, ParameterType.RequestBody);\n`; + return `var body = ${requestBody[requestBody.mode] + .split('\n') + .map((line) => { return '@"' + line.replace(/"/g, '""') + '"'; }) + .join(' + "\\n" +\n')};\n` + + `request.AddParameter("${parseContentType(request)}", ` + + 'body, ParameterType.RequestBody);\n'; case 'graphql': return parseGraphQL(requestBody, trimFields); /* istanbul ignore next */ From b3f9126d49980c1c82c85a6cdd2f1d6b1c895d3c Mon Sep 17 00:00:00 2001 From: Ankit Saini Date: Thu, 6 May 2021 15:16:19 +0530 Subject: [PATCH 3/3] Use indent count for indent JSON in Dart --- codegens/dart-http/lib/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/codegens/dart-http/lib/index.js b/codegens/dart-http/lib/index.js index 8639c1638..5e0105131 100644 --- a/codegens/dart-http/lib/index.js +++ b/codegens/dart-http/lib/index.js @@ -31,12 +31,13 @@ function parseUrlEncoded (body, indent, trim) { * @param {Object} body Raw body data * @param {Boolean} trim indicates whether to trim string or not * @param {String} contentType the content-type of request body + * @param {Integer} indentCount the number of space to use */ -function parseRawBody (body, trim, contentType) { +function parseRawBody (body, trim, contentType, indentCount) { if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) { try { let jsonBody = JSON.parse(body); - return `request.body = json.encode(${JSON.stringify(jsonBody, null, 4)});`; + return `request.body = json.encode(${JSON.stringify(jsonBody, null, indentCount)});`; } catch (error) { @@ -129,7 +130,7 @@ function parseBody (body, indent, trim, contentType) { case 'urlencoded': return parseUrlEncoded(body.urlencoded, indent, trim); case 'raw': - return parseRawBody(body.raw, trim, contentType); + return parseRawBody(body.raw, trim, contentType, indent.length); case 'formdata': return parseFormData(body.formdata, indent, trim); case 'graphql':