Skip to content
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
8 changes: 6 additions & 2 deletions codegens/csharp-restsharp/lib/parseRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
42 changes: 31 additions & 11 deletions codegens/dart-http/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}''';`;
}

Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down