From ed549a9b3ffd2a1c6c1e670848b6186984dcb8da Mon Sep 17 00:00:00 2001 From: david g Date: Fri, 6 Sep 2019 09:55:15 -0400 Subject: [PATCH 1/2] #46 - Support transforming swagger variable names to specified Postman variables. --- .gitignore | 4 ++ convert.js | 55 +++++++++++----- test/converter-spec.js | 25 ++++++- .../swagger2-with-transformed-params.json | 65 +++++++++++++++++++ 4 files changed, 132 insertions(+), 17 deletions(-) create mode 100644 test/data/swagger2-with-transformed-params.json diff --git a/.gitignore b/.gitignore index 77450cb..d4a38ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,13 @@ # Idea .idea/ +#VS Code +.vscode + ################# ## npm ################# npm-debug.log node_modules/ + diff --git a/convert.js b/convert.js index d8fbb0e..f606aa9 100644 --- a/convert.js +++ b/convert.js @@ -28,6 +28,7 @@ var uuidv4 = require('uuid/v4'), }; this.options = options || {}; + this.transforms = this.options.transforms || {}; this.options.includeQueryParams = typeof (this.options.includeQueryParams) == 'undefined' ? true : this.options.includeQueryParams; @@ -55,6 +56,29 @@ var uuidv4 = require('uuid/v4'), return new ConvertResult('passed', ''); }, + transformParameter: function (parameter, transforms) { + transforms = transforms || {}; + + if (parameter in transforms) { + return transforms[parameter]; + } + else { + return '{{' + parameter + '}}'; + } + }, + + getPostmanVariable: function (thisParams, param, transforms) { + var postmanVariableName = this.transformParameter(thisParams[param].name, transforms), + // Get default value for .in = query/header/path/formData + defaultVal = postmanVariableName; + + if (thisParams[param].hasOwnProperty('default')) { + defaultVal = thisParams[param].default; + } + + return defaultVal; + }, + setBasePath: function (json) { this.basePath = ''; if (json.host) { @@ -193,10 +217,10 @@ var uuidv4 = require('uuid/v4'), hasQueryParams = false, param, requestAttr, - defaultVal, thisConsumes = root.globalConsumes, thisProduces = root.globalProduces, - tempBasePath; + tempBasePath, + transforms = this.options.transforms || {}; if (path.length > 0 && path[0] === '/') { path = path.substring(1); @@ -254,29 +278,27 @@ var uuidv4 = require('uuid/v4'), if (thisParams.hasOwnProperty(param) && thisParams[param]) { this.logger('Processing param: ' + JSON.stringify(param)); - // Get default value for .in = query/header/path/formData - defaultVal = '{{' + thisParams[param].name + '}}'; - if (thisParams[param].hasOwnProperty('default')) { - defaultVal = thisParams[param].default; - } - if (thisParams[param].in === 'query' && this.options.includeQueryParams !== false) { if (!hasQueryParams) { hasQueryParams = true; request.url += '?'; } - request.url += thisParams[param].name + '=' + defaultVal + '&'; - } + request.url += thisParams[param].name + + '=' + + this.getPostmanVariable(thisParams, param, transforms.query) + + '&'; + } else if (thisParams[param].in === 'header') { - request.headers += thisParams[param].name + ': ' + defaultVal + '\n'; + request.headers += thisParams[param].name + + ': ' + + this.getPostmanVariable(thisParams, param, transforms.header) + + '\n'; } - else if (thisParams[param].in === 'body') { request.dataMode = 'raw'; - request.rawModeData = thisParams[param].description; + request.rawModeData = this.getPostmanVariable(thisParams, param, transforms.body); } - else if (thisParams[param].in === 'formData') { if (thisConsumes.indexOf('application/x-www-form-urlencoded') > -1) { request.dataMode = 'urlencoded'; @@ -286,7 +308,7 @@ var uuidv4 = require('uuid/v4'), } request.data.push({ 'key': thisParams[param].name, - 'value': defaultVal, + 'value': this.getPostmanVariable(thisParams, param, transforms.formData), 'type': 'text', 'enabled': true }); @@ -295,7 +317,8 @@ var uuidv4 = require('uuid/v4'), if (!request.hasOwnProperty('pathVariables')) { request.pathVariables = {}; } - request.pathVariables[thisParams[param].name] = defaultVal; + request.pathVariables[thisParams[param].name] = + this.getPostmanVariable(thisParams, param, transforms.path); } } } diff --git a/test/converter-spec.js b/test/converter-spec.js index 8379f79..8230728 100644 --- a/test/converter-spec.js +++ b/test/converter-spec.js @@ -56,7 +56,7 @@ describe('the converter', function () { expect(convertWithoutOptionsResult.collection.requests[3].url.indexOf('{') > 0).to.be(true); }); - it('should convert path paramters to postman-compatible paramters', function () { + it('should convert path parameters to postman-compatible parameters', function () { var samplePath = path.join(__dirname, 'data', 'swagger2-with-params.json'), swagger = require(samplePath), converter = new Swagger2Postman(), @@ -66,4 +66,27 @@ describe('the converter', function () { expect(convertResult.collection.requests[0].url.indexOf(':ownerId') > -1).to.be(true); expect(convertResult.collection.requests[0].url.indexOf(':petId') > -1).to.be(true); }); + + it('should transform parameters based on options.transforms', function () { + var samplePath = path.join(__dirname, 'data', 'swagger2-with-transformed-params.json'), + swagger = require(samplePath), + converter = new Swagger2Postman({ + transforms: { + header: { + 'api-key': '{{API_KEY}}', + 'Authorization': 'Bearer {{ACCESS_TOKEN}}', + 'withDefaultValue': '{{WITH_DEFAULT_VALUE}}' + }, + path: { + 'ownerId': '{{OWNER_ID}}' + } + } + }), + convertResult = converter.convert(swagger); + + expect(convertResult.collection.requests[0].pathVariables.ownerId).to.be('{{OWNER_ID}}'); + expect(convertResult.collection.requests[0].pathVariables.petId).to.be('{{petId}}'); + expect(convertResult.collection.requests[0].headers).to.be( + 'api-key: {{API_KEY}}\nAuthorization: Bearer {{ACCESS_TOKEN}}\nwithDefaultValue: 42\n'); + }); }); diff --git a/test/data/swagger2-with-transformed-params.json b/test/data/swagger2-with-transformed-params.json new file mode 100644 index 0000000..f56fa1c --- /dev/null +++ b/test/data/swagger2-with-transformed-params.json @@ -0,0 +1,65 @@ +{ + "swagger": "2.0", + "info": { + "description": "My API", + "version": "1.0.0", + "title": "Awesome Pets API", + "termsOfService": "http://www.domain.com", + "contact": { + "name": "support@domain.com" + } + }, + "basePath": "/", + "schemes": [ + "http" + ], + "paths": { + "/owner/{ownerId}/pet/{petId}": { + "post": { + "summary": "Find pets belonging to a owner", + "description": "", + "operationId": "findPetsOfOwners", + "parameters": [{ + "in": "path", + "name": "ownerId", + "description": "Should be transformed to Postman variable: {{OWNER_ID}}", + "required": true, + "type": "integer" + }, { + "in": "path", + "name": "petId", + "description": "Do NOT transform! Postman variable: {{petId}}", + "required": true, + "type": "integer" + }, { + "in": "header", + "name": "api-key", + "description": "Transform to Postman variable: {{API_KEY}}", + "required": true, + "type": "string" + }, { + "in": "header", + "name": "Authorization", + "description": "Transform to Postman variable: Bearer {{ACCESS_TOKEN}}", + "required": true, + "type": "string" + }, { + "in": "header", + "name": "withDefaultValue", + "description": "Should inject the default value and not any variable: 42", + "required": true, + "type": "integer", + "default": 42 + }], + "responses": { + "200": { + "description": "Pet found successfully.", + "schema": { + "$ref": "#/definitions/Pet" + } + } + } + } + } + } +} From 725089fafa67a6b0dd94084ff2c304aef6c8d774 Mon Sep 17 00:00:00 2001 From: david g Date: Fri, 6 Sep 2019 10:11:17 -0400 Subject: [PATCH 2/2] #26 - Updated README to include information on using options.transforms --- README.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7fa4703..28df762 100644 --- a/README.md +++ b/README.md @@ -55,5 +55,54 @@ var options = { var swaggerConverter = new Swagger2Postman(options); ``` -**valid options are:** -includeQueryParams - (default true) Include query string parameters in the request url. +**Valid options are:** + +* includeQueryParams - (default true) Include query string parameters in the request url. +* transforms - (default empty) Map used to transform Swagger variables to differently named Postman variables. See [Transforms](#transforms) section below for details. + +**Transforms** + +```js +this.options = { + transforms: { + header: { + '': '', + }, path: { + '': '', + }, formData: { + '': '', + }, body: { + '': '', + } + } +} +``` + +The must contain the wrapping double-curly braces ({{...}}). The may contain any other hardcoded text/values as well. For instance, a common need for this is setting an Authorization HTTP header, that requires a Bearer prefix, for example: + +```js +this.options = { + transforms: { + header: { + 'Authorization': 'Bearer {{ACCESS_TOKEN}}' + } + } +} +``` + +An example initializing the Swagger2Postman converter w/ transform parameters is as follows: + +```js +converter = new Swagger2Postman({ + includeQueryParams: false, + transforms: { + header: { + 'api-key': '{{API_KEY}}', + 'Authorization': 'Bearer {{ACCESS_TOKEN}}', + }, + path: { + 'ownerId': '{{OWNER_ID}}' + } + } +}); +```