Skip to content

Commit

Permalink
Fixing both rust and kotlin codegen by escaping strings properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhwaneet Bhatt committed Mar 20, 2023
1 parent a04a47d commit 1f36eaa
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
13 changes: 7 additions & 6 deletions codegens/kotlin-okhttp/lib/okhttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ const METHODS_WITHOUT_BODY = ['GET', 'HEAD', 'COPY', 'UNLOCK', 'UNLINK', 'PURGE'
function makeSnippet (request, indentString, options) {
let isBodyRequired = !(_.includes(METHODS_WITHOUT_BODY, request.method)),
snippet = 'val client = OkHttpClient',
hasNoOptions = !(options.requestTimeout || options.followRedirects),
requestBody;
hasNoOptions = !(options.requestTimeout || options.followRedirects);

if (hasNoOptions) {
snippet += '()\n';
Expand All @@ -30,7 +29,7 @@ function makeSnippet (request, indentString, options) {
snippet += indentString + `.connectTimeout(${options.requestTimeout}, TimeUnit.SECONDS)\n`;
}

if (!options.followRedirect) {
if (_.get(request, 'protocolProfileBehavior.followRedirects', options.followRedirect) === false) {
snippet += indentString + '.followRedirects(false)\n';
}

Expand Down Expand Up @@ -73,10 +72,12 @@ function makeSnippet (request, indentString, options) {
formdata: formdataArray
});
}
requestBody = (request.body ? request.body.toJSON() : {});

const contentType = parseRequest.parseContentType(request),
requestBody = (request.body ? request.body.toJSON() : {});
// snippet for creating mediatype object in java based on content-type of request
snippet += `val mediaType = "${parseRequest.parseContentType(request)}".toMediaType()\n`;
snippet += parseRequest.parseBody(requestBody, indentString, options.trimRequestBody);
snippet += `val mediaType = "${contentType}".toMediaType()\n`;
snippet += parseRequest.parseBody(requestBody, indentString, options.trimRequestBody, contentType);
}

snippet += 'val request = Request.Builder()\n';
Expand Down
21 changes: 19 additions & 2 deletions codegens/kotlin-okhttp/lib/parseRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,38 @@ function parseFormData (requestBody, indentString, trimFields) {
}, '') + indentString + '.build()';
}

/**
* Parses request object and returns kotlin okhttp code snippet for raw body
*
* @param {Object} requestBody - JSON object representing body of request
* @param {Boolean} trimFields - indicates whether to trim fields of body
* @param {String} contentType - content type of request body
*/
function parseRawBody (requestBody, trimFields, contentType) {
if (contentType && contentType.startsWith('application/json')) {
return `val body = ${JSON.stringify(requestBody[requestBody.mode])}.toRequestBody(mediaType)\n`;
}

return `val body = "${sanitize(requestBody[requestBody.mode], trimFields)}".toRequestBody(mediaType)\n`;
}

/**
* parses request object and returns java okhttp code snippet for adding request body
*
* @param {Object} requestBody - JSON object representing body of request
* @param {String} indentString - string for indentation
* @param {Boolean} trimFields - indicates whether to trim fields of body
* @param {String} contentType - content type of request body
*
* @returns {String} - code snippet of java okhttp parsed from request object
*/
function parseBody (requestBody, indentString, trimFields) {
function parseBody (requestBody, indentString, trimFields, contentType) {
if (!_.isEmpty(requestBody)) {
switch (requestBody.mode) {
case 'urlencoded':
return `val body = "${parseUrlencode(requestBody, trimFields)}".toRequestBody(mediaType)\n`;
case 'raw':
return `val body = ${JSON.stringify(requestBody[requestBody.mode])}.toRequestBody(mediaType)\n`;
return parseRawBody(requestBody, trimFields, contentType);
case 'graphql':
// eslint-disable-next-line no-case-declarations
let query = requestBody[requestBody.mode].query,
Expand Down
8 changes: 7 additions & 1 deletion codegens/kotlin-okhttp/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ module.exports = {
if (typeof inputString !== 'string') {
return '';
}
inputString = inputString.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
inputString = inputString
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\$/g, '\\$')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t');
return trim ? inputString.trim() : inputString;

},
Expand Down
2 changes: 1 addition & 1 deletion codegens/rust-reqwest/lib/reqwest.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function makeSnippet (request, indentation, options) {
snippet += `${indentation}let client = reqwest::Client::builder()\n`;

// Disable redirects if option is set
if (options.followRedirect === false) {
if (_.get(request, 'protocolProfileBehavior.followRedirects', options.followRedirect) === false) {
snippet += `${indentation.repeat(2)}.redirect(reqwest::redirect::Policy::none())\n`;
}

Expand Down
4 changes: 2 additions & 2 deletions codegens/rust-reqwest/lib/util/parseRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ function parseRawBody (body, trim, indentation, contentType) {
requestBodySnippet += `${indentation.repeat(2)}.json(&json)\n`;
}
catch (e) {
bodySnippet = `${indentation}let data = r#"${trim ? body.trim() : body}"#;\n\n`;
bodySnippet = `${indentation}let data = "${sanitize(body, trim)}";\n\n`;
requestBodySnippet += `${indentation.repeat(2)}.body(data)\n`;
}
}
else {
bodySnippet = `${indentation}let data = r#"${trim ? body.trim() : body}"#;\n\n`;
bodySnippet = `${indentation}let data = "${sanitize(body, trim)}";\n\n`;
requestBodySnippet += `${indentation.repeat(2)}.body(data)\n`;
}

Expand Down
2 changes: 1 addition & 1 deletion test/codegen/newman/fixtures/basicCollection.json
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@
],
"body": {
"mode": "raw",
"raw": "var val = 6;\nconsole.log(val);console.log('text\r\n');\n"
"raw": "var val = 6;\nconsole.log(val);console.log('$text\r\n');\n"
},
"url": {
"raw": "https://postman-echo.com/post",
Expand Down

0 comments on commit 1f36eaa

Please sign in to comment.