From e5d420c647b92538713596f2ffb353509d751373 Mon Sep 17 00:00:00 2001 From: Yi Zhao <11612917@mail.sustech.edu.cn> Date: Sat, 25 May 2024 11:23:49 -0700 Subject: [PATCH 1/9] fix python.http-client generate code snippet using http --- .../lib/python-httpclient.js | 7 +++++- .../test/unit/converter.test.js | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/codegens/python-http.client/lib/python-httpclient.js b/codegens/python-http.client/lib/python-httpclient.js index f0d80f621..9b8559cc9 100644 --- a/codegens/python-http.client/lib/python-httpclient.js +++ b/codegens/python-http.client/lib/python-httpclient.js @@ -140,7 +140,12 @@ self = module.exports = { snippet += 'from codecs import encode\n'; } snippet += '\n'; - snippet += `conn = http.client.HTTPSConnection("${sanitize(host)}"`; + if (request.url.protocol === 'http') { + snippet += `conn = http.client.HTTPConnection("${sanitize(host)}"`; + } + else { + snippet += `conn = http.client.HTTPSConnection("${sanitize(host)}"`; + } snippet += url.port ? `, ${request.url.port}` : ''; snippet += options.requestTimeout !== 0 ? `, timeout = ${options.requestTimeout})\n` : ')\n'; diff --git a/codegens/python-http.client/test/unit/converter.test.js b/codegens/python-http.client/test/unit/converter.test.js index 8d37b9282..d9b686f99 100644 --- a/codegens/python-http.client/test/unit/converter.test.js +++ b/codegens/python-http.client/test/unit/converter.test.js @@ -336,6 +336,29 @@ describe('Python-http.client converter', function () { }); }); + it('should generate valid snippets when url uses http protocol', function () { + var request = new sdk.Request({ + 'method': 'GET', + 'header': [], + 'url': { + 'raw': 'http://localhost:3000', + 'protocol': 'http', + 'host': [ + 'localhost' + ], + 'port': '3000' + }, + 'response': [] + }); + convert(request, {}, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + expect(snippet).to.be.a('string'); + expect(snippet).to.include('conn = http.client.HTTPConnection("localhost", 3000)'); + }); + }); + }); describe('parseBody function', function () { From 9732ee8a2dd9a8c9d9dd2e7026d015b4ba055df5 Mon Sep 17 00:00:00 2001 From: Divesh Thapa <66884874+diveshthapa@users.noreply.github.com> Date: Sat, 27 Jul 2024 19:23:54 +0545 Subject: [PATCH 2/9] cropped image has been removed from heading and table of content. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 607cf4166..18684d8c0 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ _Manage all of your organization's APIs in Postman, with the industry's most com *Supercharge your API workflow.* *Modern software is built on APIs. Postman helps you develop APIs faster.* -# postman-code-generators [![Build Status](https://travis-ci.com/postmanlabs/postman-code-generators.svg?branch=master)](https://travis-ci.com/postmanlabs/postman-code-generators) +# postman-code-generators This module converts a [Postman SDK](https://github.com/postmanlabs/postman-collection) Request [Object](https://www.postmanlabs.com/postman-collection/Request.html) into a code snippet of chosen language. @@ -52,7 +52,7 @@ List of supported code generators: | Swift | URLSession | ## Table of contents -- [postman-code-generators ![Build Status](https://travis-ci.com/postmanlabs/postman-code-generators)](#postman-code-generators-) +- [postman-code-generators - [Table of contents](#table-of-contents) - [Getting Started](#getting-started) - [Prerequisite](#prerequisite) From 2ffde7a3841851db737c15ae072257b642291dfb Mon Sep 17 00:00:00 2001 From: Vishal Shingala Date: Mon, 30 Sep 2024 10:56:28 +0530 Subject: [PATCH 3/9] Added support for usage of --data-binary flag when using long format option for body typr binary --- codegens/curl/lib/index.js | 2 +- codegens/curl/test/unit/convert.test.js | 32 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/codegens/curl/lib/index.js b/codegens/curl/lib/index.js index 8abc73df6..d55ad5550 100644 --- a/codegens/curl/lib/index.js +++ b/codegens/curl/lib/index.js @@ -201,7 +201,7 @@ self = module.exports = { }); break; case 'file': - snippet += indent + form('-d', format); + snippet += indent + (format ? '--data-binary' : '-d'); snippet += ` ${quoteType}@${sanitize(body[body.mode].src, trim)}${quoteType}`; break; default: diff --git a/codegens/curl/test/unit/convert.test.js b/codegens/curl/test/unit/convert.test.js index 849395a5e..ae2111acc 100644 --- a/codegens/curl/test/unit/convert.test.js +++ b/codegens/curl/test/unit/convert.test.js @@ -1144,5 +1144,37 @@ describe('curl convert function', function () { }); }); }); + + it('should use --data-binary when request body type is binary', function () { + var request = new Request({ + 'method': 'POST', + 'header': [], + 'body': { + 'mode': 'file', + 'file': { + 'src': 'file-path/collection123.json' + } + }, + 'url': { + 'raw': 'https://postman-echo.com/get', + 'protocol': 'https', + 'host': [ + 'postman-echo', + 'com' + ], + 'path': [ + 'get' + ] + } + }); + + convert(request, { longFormat: true }, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + expect(snippet).to.be.a('string'); + expect(snippet).to.include('--data-binary \'@file-path/collection123.json\''); + }); + }); }); }); From a77985152a83ef25412222ecfee0a0d9929582c0 Mon Sep 17 00:00:00 2001 From: Vishal Shingala Date: Tue, 1 Oct 2024 12:32:35 +0530 Subject: [PATCH 4/9] Fixed failing test due to incorrect usage of collection request --- codegens/python-http.client/test/unit/converter.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegens/python-http.client/test/unit/converter.test.js b/codegens/python-http.client/test/unit/converter.test.js index 459aa428d..b409c31e6 100644 --- a/codegens/python-http.client/test/unit/converter.test.js +++ b/codegens/python-http.client/test/unit/converter.test.js @@ -337,7 +337,7 @@ describe('Python-http.client converter', function () { }); it('should generate valid snippets when url uses http protocol', function () { - var request = new sdk.Request({ + var request = new Request({ 'method': 'GET', 'header': [], 'url': { From d00d3076e5919178ff1e0cc5bdbf064cb074df67 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Thu, 10 Oct 2024 10:38:55 +0530 Subject: [PATCH 5/9] 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 6/9] 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 7/9] 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({ From c1afa8ac8278318cf52c6f73c9c0917001e0307e Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Thu, 10 Oct 2024 12:36:32 +0530 Subject: [PATCH 8/9] Updated Changelog for v1.14.0 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee9afb1ae..fb823f31e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## [Unreleased] +### Fixed + +- Fix for - [#5330](https://github.com/postmanlabs/postman-app-support/issues/5330) Added support for usage of --data-binary flag when using long format option for body type binary. + +- Fix for - [#540](https://github.com/postmanlabs/postman-code-generators/issues/540) Curl codesnippet's JSON body must follow multiLine option's configuration. + ## [v1.13.0] - 2024-09-11 ### Fixed From d2c0a5148c40fedfe4884beb1e15f1b50cade403 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Thu, 10 Oct 2024 07:40:54 +0000 Subject: [PATCH 9/9] Prepare release v1.14.0 --- CHANGELOG.md | 10 +++++++--- package-lock.json | 2 +- package.json | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb823f31e..f3dfd98ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,13 @@ ## [Unreleased] +## [v1.14.0] - 2024-10-10 + ### Fixed -- Fix for - [#5330](https://github.com/postmanlabs/postman-app-support/issues/5330) Added support for usage of --data-binary flag when using long format option for body type binary. +- Fix for - [#5330](https://github.com/postmanlabs/postman-app-support/issues/5330) Added support for usage of --data-binary flag when using long format option for body type binary. -- Fix for - [#540](https://github.com/postmanlabs/postman-code-generators/issues/540) Curl codesnippet's JSON body must follow multiLine option's configuration. +- Fix for - [#540](https://github.com/postmanlabs/postman-code-generators/issues/540) Curl codesnippet's JSON body must follow multiLine option's configuration. ## [v1.13.0] - 2024-09-11 @@ -182,7 +184,9 @@ v1.0.0 (May 29, 2020) - Add ES6 syntax support for NodeJS Request, NodeJS Native and NodeJS Unirest - Fix snippet generation for powershell and jquery, where form data params had no type field -[Unreleased]: https://github.com/postmanlabs/postman-code-generators/compare/v1.13.0...HEAD +[Unreleased]: https://github.com/postmanlabs/postman-code-generators/compare/v1.14.0...HEAD + +[v1.14.0]: https://github.com/postmanlabs/postman-code-generators/compare/v1.13.0...v1.14.0 [v1.13.0]: https://github.com/postmanlabs/postman-code-generators/compare/v1.12.0...v1.13.0 diff --git a/package-lock.json b/package-lock.json index c5c0623c9..d3540ab45 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "postman-code-generators", - "version": "1.13.0", + "version": "1.14.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index cb23c862a..1bf813ba1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postman-code-generators", - "version": "1.13.0", + "version": "1.14.0", "description": "Generates code snippets for a postman collection", "main": "index.js", "directories": {