Skip to content

Commit f31e1da

Browse files
author
Dhwaneet Bhatt
committed
Fix kotlin okhttp codegen for raw bodies
1 parent 2293771 commit f31e1da

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

codegens/kotlin-okhttp/lib/okhttp.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ const METHODS_WITHOUT_BODY = ['GET', 'HEAD', 'COPY', 'UNLOCK', 'UNLINK', 'PURGE'
1818
function makeSnippet (request, indentString, options) {
1919
let isBodyRequired = !(_.includes(METHODS_WITHOUT_BODY, request.method)),
2020
snippet = 'val client = OkHttpClient',
21-
hasNoOptions = !(options.requestTimeout || options.followRedirects),
22-
requestBody;
21+
hasNoOptions = !(options.requestTimeout || options.followRedirects);
2322

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

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

@@ -73,10 +72,12 @@ function makeSnippet (request, indentString, options) {
7372
formdata: formdataArray
7473
});
7574
}
76-
requestBody = (request.body ? request.body.toJSON() : {});
75+
76+
const contentType = parseRequest.parseContentType(request),
77+
requestBody = (request.body ? request.body.toJSON() : {});
7778
// snippet for creating mediatype object in java based on content-type of request
78-
snippet += `val mediaType = "${parseRequest.parseContentType(request)}".toMediaType()\n`;
79-
snippet += parseRequest.parseBody(requestBody, indentString, options.trimRequestBody);
79+
snippet += `val mediaType = "${contentType}".toMediaType()\n`;
80+
snippet += parseRequest.parseBody(requestBody, indentString, options.trimRequestBody, contentType);
8081
}
8182

8283
snippet += 'val request = Request.Builder()\n';

codegens/kotlin-okhttp/lib/parseRequest.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,38 @@ function parseFormData (requestBody, indentString, trimFields) {
5858
}, '') + indentString + '.build()';
5959
}
6060

61+
/**
62+
* Parses request object and returns kotlin okhttp code snippet for raw body
63+
*
64+
* @param {Object} requestBody - JSON object representing body of request
65+
* @param {Boolean} trimFields - indicates whether to trim fields of body
66+
* @param {String} contentType - content type of request body
67+
*/
68+
function parseRawBody (requestBody, trimFields, contentType) {
69+
if (contentType && contentType.startsWith('application/json')) {
70+
return `val body = ${JSON.stringify(requestBody[requestBody.mode])}.toRequestBody(mediaType)\n`;
71+
}
72+
73+
return `val body = "${sanitize(requestBody[requestBody.mode], trimFields)}".toRequestBody(mediaType)\n`;
74+
}
75+
6176
/**
6277
* parses request object and returns java okhttp code snippet for adding request body
6378
*
6479
* @param {Object} requestBody - JSON object representing body of request
6580
* @param {String} indentString - string for indentation
6681
* @param {Boolean} trimFields - indicates whether to trim fields of body
82+
* @param {String} contentType - content type of request body
83+
*
6784
* @returns {String} - code snippet of java okhttp parsed from request object
6885
*/
69-
function parseBody (requestBody, indentString, trimFields) {
86+
function parseBody (requestBody, indentString, trimFields, contentType) {
7087
if (!_.isEmpty(requestBody)) {
7188
switch (requestBody.mode) {
7289
case 'urlencoded':
7390
return `val body = "${parseUrlencode(requestBody, trimFields)}".toRequestBody(mediaType)\n`;
7491
case 'raw':
75-
return `val body = ${JSON.stringify(requestBody[requestBody.mode])}.toRequestBody(mediaType)\n`;
92+
return parseRawBody(requestBody, trimFields, contentType);
7693
case 'graphql':
7794
// eslint-disable-next-line no-case-declarations
7895
let query = requestBody[requestBody.mode].query,

codegens/kotlin-okhttp/lib/util.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ module.exports = {
1111
if (typeof inputString !== 'string') {
1212
return '';
1313
}
14-
inputString = inputString.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
14+
inputString = inputString
15+
.replace(/\\/g, '\\\\')
16+
.replace(/"/g, '\\"')
17+
.replace(/\$/g, '\\$')
18+
.replace(/\n/g, '\\n')
19+
.replace(/\r/g, '\\r')
20+
.replace(/\t/g, '\\t');
1521
return trim ? inputString.trim() : inputString;
1622

1723
},

codegens/kotlin-okhttp/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "@postman/codegen-kotlin-okhttp",
33
"version": "0.0.1",
4-
"description": "Converts postman request into kotlin ktor code snippet",
4+
"description": "Converts postman request into kotlin okhttp code snippet",
55
"com_postman_plugin": {
66
"type": "code_generator",
7-
"lang": "kotlin",
8-
"variant": "Ktor",
9-
"syntax_mode": "java"
7+
"lang": "Kotlin",
8+
"variant": "Okhttp",
9+
"syntax_mode": "kotlin"
1010
},
1111
"main": "index.js",
1212
"directories": {

test/codegen/newman/fixtures/basicCollection.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@
330330
],
331331
"body": {
332332
"mode": "raw",
333-
"raw": "var val = 6;\nconsole.log(val);console.log('text\r\n');\n"
333+
"raw": "var val = 6;\nconsole.log(val);console.log('$text\r\n');\n"
334334
},
335335
"url": {
336336
"raw": "https://postman-echo.com/post",
@@ -685,4 +685,4 @@
685685
}
686686
}
687687
]
688-
}
688+
}

0 commit comments

Comments
 (0)