Skip to content

Commit

Permalink
fix(AWS API Gateway): Meaningfully reject missing `restApiRootResourc…
Browse files Browse the repository at this point in the history
…eId`

(PR #10371)
  • Loading branch information
sdas13 committed Dec 15, 2021
1 parent dd033e8 commit 2c0a962
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Expand Up @@ -41,6 +41,14 @@ module.exports = {
validate() {
const events = [];
const corsPreflight = {};
const apiGateway = this.serverless.service.provider.apiGateway;

if (apiGateway && apiGateway.restApiId && !apiGateway.restApiRootResourceId) {
throw new ServerlessError(
'Missing required "provider.apiGateway.restApiRootResourceId" property (needed if "provider.apiGateway.restApiId" is provided)',
'API_GATEWAY_MISSING_REST_API_ROOT_RESOURCE_ID'
);
}

Object.entries(this.serverless.service.functions).forEach(([functionName, functionObject]) => {
(functionObject.events || []).forEach((event) => {
Expand Down
Expand Up @@ -8,6 +8,9 @@ const Serverless = require('../../../../../../../../../../lib/Serverless');
const AwsProvider = require('../../../../../../../../../../lib/plugins/aws/provider');
const ServerlessError = require('../../../../../../../../../../lib/serverless-error');

const chai = require('chai');
chai.use(require('chai-as-promised'));

describe('#validate()', () => {
let serverless;
let awsCompileApigEvents;
Expand Down Expand Up @@ -1471,4 +1474,36 @@ describe('test/unit/lib/plugins/aws/package/compile/events/apiGateway/lib/valida
.IntegrationResponses[0].ResponseParameters
).to.deep.eq(expected);
});

it('should throw an error when restApiRootResourceId is not provided with restApiId', async () => {
await expect(
runServerless({
fixture: 'function',
command: 'package',
configExt: {
provider: {
apiGateway: {
restApiId: 'ivrcdpj7y2',
},
},
functions: {
first: {
handler: 'index.handler',
events: [
{
http: {
method: 'GET',
path: 'foo/bar',
},
},
],
},
},
},
})
).to.be.eventually.rejected.and.have.property(
'code',
'API_GATEWAY_MISSING_REST_API_ROOT_RESOURCE_ID'
);
});
});

0 comments on commit 2c0a962

Please sign in to comment.