Skip to content

Commit c78bfa8

Browse files
authored
Merge pull request #774 from postmanlabs/fix/issue#540
[Fix/issue#540] Curl codesnippet's JSON body must follow multiLine option's configuration
2 parents a23f21d + c74d605 commit c78bfa8

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

codegens/curl/lib/index.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,20 @@ self = module.exports = {
151151
let rawBody = body.raw.toString(),
152152
isAsperandPresent = _.includes(rawBody, '@'),
153153
// Use the long option if `@` is present in the request body otherwise follow user setting
154-
optionName = isAsperandPresent ? '--data-raw' : form('-d', format);
155-
// eslint-disable-next-line max-len
156-
snippet += indent + `${optionName} ${quoteType}${sanitize(rawBody, trim, quoteType)}${quoteType}`;
154+
optionName = isAsperandPresent ? '--data-raw' : form('-d', format),
155+
sanitizedBody = sanitize(rawBody, trim, quoteType);
156+
157+
if (!multiLine) {
158+
try {
159+
sanitizedBody = JSON.stringify(JSON.parse(sanitizedBody));
160+
}
161+
catch (e) {
162+
// Do nothing
163+
}
164+
}
165+
166+
snippet += indent + `${optionName} ${quoteType}${sanitizedBody}${quoteType}`;
167+
157168
break;
158169
}
159170

codegens/curl/test/unit/convert.test.js

+44
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,50 @@ describe('curl convert function', function () {
289289
});
290290
});
291291

292+
it('should return snippet with JSON body in single line if multiline option is false', function () {
293+
request = new Request({
294+
'method': 'POST',
295+
'header': [],
296+
'body': {
297+
'mode': 'raw',
298+
'raw': '{\n "name": "John",\n "type": "names",\n "id": "123sdaw"\n}',
299+
'options': {
300+
'raw': {
301+
'language': 'json'
302+
}
303+
}
304+
},
305+
'url': {
306+
'raw': 'https://postman-echo.com/post',
307+
'protocol': 'https',
308+
'host': [
309+
'postman-echo',
310+
'com'
311+
],
312+
'path': [
313+
'post'
314+
]
315+
}
316+
});
317+
options = {
318+
multiLine: false,
319+
longFormat: false,
320+
lineContinuationCharacter: '\\',
321+
quoteType: 'single',
322+
requestTimeoutInSeconds: 0,
323+
followRedirect: true,
324+
followOriginalHttpMethod: false
325+
};
326+
327+
convert(request, options, function (error, snippet) {
328+
if (error) {
329+
expect.fail(null, null, error);
330+
}
331+
expect(snippet).to.be.a('string');
332+
expect(snippet).to.contain('-d \'{"name":"John","type":"names","id":"123sdaw"}\'');
333+
});
334+
});
335+
292336
it('should return snippet with backslash(\\) character as line continuation ' +
293337
'character for multiline code generation', function () {
294338
request = new Request({

0 commit comments

Comments
 (0)