From d00d3076e5919178ff1e0cc5bdbf064cb074df67 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Thu, 10 Oct 2024 10:38:55 +0530 Subject: [PATCH 1/3] Curl Codesnippet JSON body must not be multilined if disabled --- codegens/curl/lib/index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/codegens/curl/lib/index.js b/codegens/curl/lib/index.js index d55ad5550..bbed5f80f 100644 --- a/codegens/curl/lib/index.js +++ b/codegens/curl/lib/index.js @@ -152,8 +152,14 @@ self = module.exports = { 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}`; + + if (!multiLine && body.options.raw && body.options.raw.language === 'json') { + // eslint-disable-next-line max-len + snippet += indent + `${optionName} ${quoteType}${JSON.stringify(JSON.parse(sanitize(rawBody, trim, quoteType)))}${quoteType}`; + } + else { + snippet += indent + `${optionName} ${quoteType}${sanitize(rawBody, trim, quoteType)}${quoteType}`; + } break; } From 393eefd8bd6256c23ede307de8041b910255a5d8 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Thu, 10 Oct 2024 11:25:18 +0530 Subject: [PATCH 2/3] make json body single line for multiline disabled config --- codegens/curl/lib/index.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/codegens/curl/lib/index.js b/codegens/curl/lib/index.js index bbed5f80f..af57ca4d3 100644 --- a/codegens/curl/lib/index.js +++ b/codegens/curl/lib/index.js @@ -151,15 +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); + optionName = isAsperandPresent ? '--data-raw' : form('-d', format), + sanitizedBody = sanitize(rawBody, trim, quoteType); - if (!multiLine && body.options.raw && body.options.raw.language === 'json') { - // eslint-disable-next-line max-len - snippet += indent + `${optionName} ${quoteType}${JSON.stringify(JSON.parse(sanitize(rawBody, trim, quoteType)))}${quoteType}`; - } - else { - snippet += indent + `${optionName} ${quoteType}${sanitize(rawBody, trim, quoteType)}${quoteType}`; + if (!multiLine) { + try { + sanitizedBody = JSON.stringify(JSON.parse(sanitizedBody)); + } + catch (e) { + // Do nothing + } } + + snippet += indent + `${optionName} ${quoteType}${sanitizedBody}${quoteType}`; + break; } From c74d605a29a4b3796e4ee22ea0c25b16445d1586 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Thu, 10 Oct 2024 11:25:44 +0530 Subject: [PATCH 3/3] Added tests --- codegens/curl/test/unit/convert.test.js | 44 +++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/codegens/curl/test/unit/convert.test.js b/codegens/curl/test/unit/convert.test.js index ae2111acc..d3c4c5061 100644 --- a/codegens/curl/test/unit/convert.test.js +++ b/codegens/curl/test/unit/convert.test.js @@ -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({