From 2c0a962c4fc0ad82ecbc6a266d56bfaa81ad8054 Mon Sep 17 00:00:00 2001 From: Sudipto Das Date: Wed, 15 Dec 2021 21:46:28 +0530 Subject: [PATCH] fix(AWS API Gateway): Meaningfully reject missing `restApiRootResourceId` (PR #10371) --- .../compile/events/apiGateway/lib/validate.js | 8 +++++ .../events/apiGateway/lib/validate.test.js | 35 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js index 5b99559e8d3..92623082528 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js @@ -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) => { diff --git a/test/unit/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js b/test/unit/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js index 8cb9534b1d5..fa26161a646 100644 --- a/test/unit/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js +++ b/test/unit/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js @@ -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; @@ -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' + ); + }); });