Skip to content

Commit

Permalink
Use existing API id only when defined
Browse files Browse the repository at this point in the history
This fixes a bug where having any custom data defined in provider
will trigger a ValidationError because the existing API id is null
or undefined.
  • Loading branch information
attila committed Jan 10, 2019
1 parent c019c7e commit 4e6e8f3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -314,7 +314,7 @@ class ServerlessCustomDomain {
let apiGatewayRef = { Ref: 'ApiGatewayRestApi' };

// If user has specified an existing API Gateway API, then attach to that
if (service.provider.apiGateway) {
if (service.provider.apiGateway && service.provider.apiGateway.restApiId) {
this.serverless.cli.log(`Mapping custom domain to existing API ${service.provider.apiGateway.restApiId}.`);
apiGatewayRef = service.provider.apiGateway.restApiId;
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions test/unit-tests/index.test.js
Expand Up @@ -163,6 +163,24 @@ describe('Custom Domain Plugin', () => {
const cf = noStagePlugin.serverless.service.provider.compiledCloudFormationTemplate.Resources;
expect(cf.pathmapping.Properties.Stage).to.equal('providerStage');
});

it('references the id of the API being created', () => {
plugin.addResources(deploymentId);
const cf = plugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources;
expect(cf.pathmapping.Properties.RestApiId).to.deep.equal({ Ref: 'ApiGatewayRestApi' });
});

it('uses existing API id when defined in provider data', () => {
const apiIdExistingPlugin = constructPlugin('test_basepath', null, true, true);
apiIdExistingPlugin.serverless.service.provider.apiGateway = {
restApiId: 'some-id',
};
apiIdExistingPlugin.addResources(deploymentId);
const cf = apiIdExistingPlugin.serverless.service.provider.compiledCloudFormationTemplate
.Resources;
expect(cf.pathmapping.Properties.RestApiId).to.equal('some-id');
});
});

describe('Create a New Domain Name', () => {
Expand Down

0 comments on commit 4e6e8f3

Please sign in to comment.