From ebd8cab9068231b5e3436553a7eb9f006deb67b1 Mon Sep 17 00:00:00 2001 From: Alexander Kotschenreuther Date: Fri, 18 Feb 2022 10:33:10 +0100 Subject: [PATCH 1/4] Update schemapack.js --- lib/schemapack.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/schemapack.js b/lib/schemapack.js index c4227a3d0..575c9059d 100644 --- a/lib/schemapack.js +++ b/lib/schemapack.js @@ -263,7 +263,15 @@ class SchemaPack { openapi.baseUrl = _.get(openapi, 'servers.0.url', '{{baseURL}}'); // TODO: Multiple server variables need to be saved as environments - openapi.baseUrlVariables = _.get(openapi, 'servers.0.variables'); + + //Handle variables from multiple server definitions + openapi.servers.forEach((server, index) => { + openapi.baseUrlVariables = _.merge(openapi.baseUrlVariables, server.variables); + }); + + if(!Object.keys(openapi.baseUrlVariables) > 0){ + openapi.baseUrlVariables = undefined; + } // Fix {scheme} and {path} vars in the URL to :scheme and :path openapi.baseUrl = schemaUtils.fixPathVariablesInUrl(openapi.baseUrl); From 09b17dcd37cd1c845d19b20fa88fd890b69ee566 Mon Sep 17 00:00:00 2001 From: Alexander Kotschenreuther Date: Fri, 17 Jun 2022 16:34:24 +0200 Subject: [PATCH 2/4] Fix linting errors --- lib/schemapack.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/schemapack.js b/lib/schemapack.js index 575c9059d..a0cecd58f 100644 --- a/lib/schemapack.js +++ b/lib/schemapack.js @@ -264,12 +264,12 @@ class SchemaPack { // TODO: Multiple server variables need to be saved as environments - //Handle variables from multiple server definitions - openapi.servers.forEach((server, index) => { + // Handle variables from multiple server definitions + openapi.servers.forEach((server) => { openapi.baseUrlVariables = _.merge(openapi.baseUrlVariables, server.variables); }); - if(!Object.keys(openapi.baseUrlVariables) > 0){ + if (!Object.keys(openapi.baseUrlVariables) > 0){ openapi.baseUrlVariables = undefined; } From abe073e5e37b8198a4210e627bab2355c2d97002 Mon Sep 17 00:00:00 2001 From: Alexander Kotschenreuther Date: Mon, 20 Jun 2022 08:23:49 +0200 Subject: [PATCH 3/4] More lint --- lib/schemapack.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/schemapack.js b/lib/schemapack.js index a0cecd58f..10015d170 100644 --- a/lib/schemapack.js +++ b/lib/schemapack.js @@ -269,7 +269,7 @@ class SchemaPack { openapi.baseUrlVariables = _.merge(openapi.baseUrlVariables, server.variables); }); - if (!Object.keys(openapi.baseUrlVariables) > 0){ + if (!Object.keys(openapi.baseUrlVariables) > 0) { openapi.baseUrlVariables = undefined; } From a241f35e72c8322ec5a1566cf0514dcca88a5513 Mon Sep 17 00:00:00 2001 From: Alexander Kotschenreuther Date: Mon, 11 Jul 2022 08:31:25 +0200 Subject: [PATCH 4/4] Add tests for multiple server variables --- .../multiple_servers_with_variables.json | 42 +++++++++++++++++++ test/unit/base.test.js | 19 +++++++++ 2 files changed, 61 insertions(+) create mode 100644 test/data/valid_openapi/multiple_servers_with_variables.json diff --git a/test/data/valid_openapi/multiple_servers_with_variables.json b/test/data/valid_openapi/multiple_servers_with_variables.json new file mode 100644 index 000000000..93af3b3e9 --- /dev/null +++ b/test/data/valid_openapi/multiple_servers_with_variables.json @@ -0,0 +1,42 @@ +{ + "openapi": "3.0.2", + "info": { + "title": "Multiple servers with different URL variables", + "version": "2.0.0" + }, + "servers": [ + { + "url": "{FIRST_VAR}/api", + "description": "Development server", + "variables": { + "FIRST_VAR": { + "default": "https://api.example.com/v1", + "description": "Server base URI" + } + } + }, + { + "url": "{SECOND_VAR}/api", + "description": "Development server 2", + "variables": { + "SECOND_VAR": { + "default": "https://api.example.com/v2", + "description": "Server base URI" + } + } + } + ], + "paths": { + "/primary-domain/works": { + "get": { + "operationId": "get_authorize", + "summary": "Should keep the same domain", + "responses": { + "201": { + "description": "Null response" + } + } + } + } + } +} diff --git a/test/unit/base.test.js b/test/unit/base.test.js index d39cdba71..a6d1c1d3b 100644 --- a/test/unit/base.test.js +++ b/test/unit/base.test.js @@ -44,6 +44,7 @@ describe('CONVERT FUNCTION TESTS ', function() { securityTestCases = path.join(__dirname, VALID_OPENAPI_PATH + '/security-test-cases.yaml'), emptySecurityTestCase = path.join(__dirname, VALID_OPENAPI_PATH + '/empty-security-test-case.yaml'), rootUrlServerWithVariables = path.join(__dirname, VALID_OPENAPI_PATH + '/root_url_server_with_variables.json'), + multipleServerUrlsWithVariables = path.join(__dirname, VALID_OPENAPI_PATH + '/multiple_servers_with_variables.json'), parameterExamples = path.join(__dirname, VALID_OPENAPI_PATH + '/parameteres_with_examples.yaml'), issue10229 = path.join(__dirname, VALID_OPENAPI_PATH, '/issue#10229.json'), deepObjectLengthProperty = path.join(__dirname, VALID_OPENAPI_PATH, '/deepObjectLengthProperty.yaml'), @@ -1041,6 +1042,24 @@ describe('CONVERT FUNCTION TESTS ', function() { }); }); + it('Should correctly define collectionVars for all servers', function (done) { + var openapi = fs.readFileSync(multipleServerUrlsWithVariables, 'utf8'); + Converter.convert({ type: 'string', data: openapi }, {}, (err, conversionResult) => { + let requestUrl, + collectionVars; + expect(err).to.be.null; + expect(conversionResult.result).to.be.true; + + requestUrl = conversionResult.output[0].data.item[0].request.url; + collectionVars = conversionResult.output[0].data.variable; + expect(requestUrl.host).to.eql(['{{baseUrl}}']); + expect(_.find(collectionVars, { key: 'baseUrl' }).value).to.eql('{{BASE_URI}}/api'); + expect(_.find(collectionVars, { key: 'FIRST_VAR' }).value).to.eql('https://api.example.com/v1'); + expect(_.find(collectionVars, { key: 'SECOND_VAR' }).value).to.eql('https://api.example.com/v2'); + done(); + }); + }); + it('[Github #31] & [GitHub #337] - should set optional params as disabled', function(done) { let options = { schemaFaker: true, disableOptionalParameters: true }; Converter.convert({ type: 'file', data: requiredInParams }, options, (err, conversionResult) => {