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 */ diff --git a/codegens/dart-http/lib/index.js b/codegens/dart-http/lib/index.js index 61857ee4c..5e0105131 100644 --- a/codegens/dart-http/lib/index.js +++ b/codegens/dart-http/lib/index.js @@ -30,8 +30,20 @@ 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) { +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, indentCount)});`; + + } + catch (error) { + // Do nothing + } + } return `request.body = '''${sanitize(body, trim)}''';`; } @@ -109,15 +121,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, indent.length); case 'formdata': return parseFormData(body.formdata, indent, trim); case 'graphql': @@ -167,13 +180,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 +209,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 +263,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;