diff --git a/lib/schemapack.js b/lib/schemapack.js index c4227a3d0..10015d170 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) => { + 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); 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) => {