Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variables from all server definitions to the collections #560

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/schemapack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions test/data/valid_openapi/multiple_servers_with_variables.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}
19 changes: 19 additions & 0 deletions test/unit/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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) => {
Expand Down