Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix/issue#540] Curl codesnippet's JSON body must follow multiLine option's configuration #774

Merged
merged 3 commits into from
Oct 10, 2024
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
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
Loading