Skip to content

Commit

Permalink
Merge pull request #774 from postmanlabs/fix/issue#540
Browse files Browse the repository at this point in the history
[Fix/issue#540] Curl codesnippet's JSON body must follow multiLine option's configuration
  • Loading branch information
aman-v-singh authored Oct 10, 2024
2 parents a23f21d + c74d605 commit c78bfa8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
17 changes: 14 additions & 3 deletions codegens/curl/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,20 @@ self = module.exports = {
let rawBody = body.raw.toString(),
isAsperandPresent = _.includes(rawBody, '@'),
// Use the long option if `@` is present in the request body otherwise follow user setting
optionName = isAsperandPresent ? '--data-raw' : form('-d', format);
// eslint-disable-next-line max-len
snippet += indent + `${optionName} ${quoteType}${sanitize(rawBody, trim, quoteType)}${quoteType}`;
optionName = isAsperandPresent ? '--data-raw' : form('-d', format),
sanitizedBody = sanitize(rawBody, trim, quoteType);

if (!multiLine) {
try {
sanitizedBody = JSON.stringify(JSON.parse(sanitizedBody));
}
catch (e) {
// Do nothing
}
}

snippet += indent + `${optionName} ${quoteType}${sanitizedBody}${quoteType}`;

break;
}

Expand Down
44 changes: 44 additions & 0 deletions codegens/curl/test/unit/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,50 @@ describe('curl convert function', function () {
});
});

it('should return snippet with JSON body in single line if multiline option is false', function () {
request = new Request({
'method': 'POST',
'header': [],
'body': {
'mode': 'raw',
'raw': '{\n "name": "John",\n "type": "names",\n "id": "123sdaw"\n}',
'options': {
'raw': {
'language': 'json'
}
}
},
'url': {
'raw': 'https://postman-echo.com/post',
'protocol': 'https',
'host': [
'postman-echo',
'com'
],
'path': [
'post'
]
}
});
options = {
multiLine: false,
longFormat: false,
lineContinuationCharacter: '\\',
quoteType: 'single',
requestTimeoutInSeconds: 0,
followRedirect: true,
followOriginalHttpMethod: false
};

convert(request, options, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.contain('-d \'{"name":"John","type":"names","id":"123sdaw"}\'');
});
});

it('should return snippet with backslash(\\) character as line continuation ' +
'character for multiline code generation', function () {
request = new Request({
Expand Down

0 comments on commit c78bfa8

Please sign in to comment.