From 6628272a47c45f6f889b72f015b484bebffeeca8 Mon Sep 17 00:00:00 2001 From: Travis Dimmig Date: Sat, 20 May 2017 11:02:48 -0400 Subject: [PATCH 1/6] Fix for https://github.com/serverless/serverless/issues/2882 Allow the 'parameters' field of the request object to be configured when using LAMBDA_PROXY integration --- .../compile/events/apiGateway/lib/validate.js | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) 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 365cafe8e..c565ebc3d 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js @@ -95,15 +95,33 @@ module.exports = { } } else if (http.integration === 'AWS_PROXY') { // show a warning when request / response config is used with AWS_PROXY (LAMBDA-PROXY) - if (http.request || http.response) { + if (http.request) { + let keys = Object.keys(http.request); + if (!(keys.length === 1 && keys[0] === "parameters")) { + const requestWarningMessage = [ + 'Warning! You\'re using the LAMBDA-PROXY in combination with request', + ` configuration in your function "${functionName}" Only the 'request.parameters'.`, + ' options are available in conjunction with LAMBDA-PROXY.', + ' Serverless will remove this configuration automatically before deployment.' + ].join(''); + this.serverless.cli.log(requestWarningMessage); + + for(let key of keys) { + if(key === "parameters") { + continue; + } + delete http.request[key]; + } + } + } + if (http.response) { const warningMessage = [ - 'Warning! You\'re using the LAMBDA-PROXY in combination with request / response', + 'Warning! You\'re using the LAMBDA-PROXY in combination with response', ` configuration in your function "${functionName}".`, ' Serverless will remove this configuration automatically before deployment.', ].join(''); this.serverless.cli.log(warningMessage); - delete http.request; delete http.response; } } else if (http.integration === 'HTTP' || http.integration === 'HTTP_PROXY') { @@ -244,7 +262,7 @@ module.exports = { const integration = this.getIntegration(http); if (integration === 'AWS_PROXY' - && typeof arn === 'string' && arn.match(/^arn:aws:cognito-idp/) && authorizer.claims) { + && typeof arn === 'string' && arn.match(/^arn:aws:cognito-idp/) && authorizer.claims) { const errorMessage = [ 'Cognito claims can\'t be retrieved when using lambda-proxy as the integration type', ]; From 8c35a7c757b5ac49e5256e98b787f784ec3fd79e Mon Sep 17 00:00:00 2001 From: Travis Dimmig Date: Sat, 20 May 2017 12:28:27 -0400 Subject: [PATCH 2/6] update to correctly validate the remainder of the request object after stripping non-parameter object off of it and formatting correctly for the cloudformation stack --- .../package/compile/events/apiGateway/lib/validate.js | 10 ++++++++-- package.json | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) 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 c565ebc3d..aab3f34e3 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js @@ -106,13 +106,19 @@ module.exports = { ].join(''); this.serverless.cli.log(requestWarningMessage); - for(let key of keys) { - if(key === "parameters") { + for (let key of keys) { + if (key === "parameters") { continue; } delete http.request[key]; } } + + http.request = this.getRequest(http); + + if (http.request.parameters) { + http.request.parameters = this.getRequestParameters(http.request); + } } if (http.response) { const warningMessage = [ diff --git a/package.json b/package.json index 6fa119cd7..5bdefbf1e 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "serverless", + "name": "@impulse/serverless", "version": "1.13.2", "engines": { "node": ">=4.0" From 5aa2e4fb05a7438bc4716d79233eb8116ed22916 Mon Sep 17 00:00:00 2001 From: Travis Dimmig Date: Thu, 1 Jun 2017 11:16:12 -0400 Subject: [PATCH 3/6] Cleanup and tests --- .../compile/events/apiGateway/lib/validate.js | 12 ++- .../events/apiGateway/lib/validate.test.js | 98 ++++++++++++++++++- package.json | 2 +- 3 files changed, 104 insertions(+), 8 deletions(-) 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 a236f27dc..f2e84e25c 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js @@ -114,11 +114,17 @@ module.exports = { } } - http.request = this.getRequest(http); + if (Object.keys(http.request).length === 0) { + // No keys left, delete the request object + delete http.request; + } else { + http.request = this.getRequest(http); - if (http.request.parameters) { - http.request.parameters = this.getRequestParameters(http.request); + if (http.request.parameters) { + http.request.parameters = this.getRequestParameters(http.request); + } } + } if (http.response) { const warningMessage = [ diff --git a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js index fcbbb1e4d..c61683c83 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js @@ -903,7 +903,7 @@ describe('#validate()', () => { expect(() => awsCompileApigEvents.validate()).to.throw(Error); }); - it('should process request parameters', () => { + it('should process request parameters for lambda integration', () => { awsCompileApigEvents.serverless.service.functions = { first: { events: [ @@ -946,6 +946,49 @@ describe('#validate()', () => { }); }); + it('should process request parameters for lambda-proxy integration', () => { + awsCompileApigEvents.serverless.service.functions = { + first: { + events: [ + { + http: { + integration: 'lambda-proxy', + path: 'foo/bar', + method: 'GET', + request: { + parameters: { + querystrings: { + foo: true, + bar: false, + }, + paths: { + foo: true, + bar: false, + }, + headers: { + foo: true, + bar: false, + }, + }, + }, + }, + }, + ], + }, + }; + + const validated = awsCompileApigEvents.validate(); + expect(validated.events).to.be.an('Array').with.length(1); + expect(validated.events[0].http.request.parameters).to.deep.equal({ + 'method.request.querystring.foo': true, + 'method.request.querystring.bar': false, + 'method.request.path.foo': true, + 'method.request.path.bar': false, + 'method.request.header.foo': true, + 'method.request.header.bar': false, + }); + }); + it('should throw an error if the provided response config is not an object', () => { awsCompileApigEvents.serverless.service.functions = { first: { @@ -1220,11 +1263,51 @@ describe('#validate()', () => { awsCompileApigEvents.validate(); - expect(logStub.calledOnce).to.be.equal(true); + expect(logStub.calledTwice).to.be.equal(true); expect(logStub.args[0][0].length).to.be.at.least(1); }); - it('should remove request/response config with LAMBDA-PROXY', () => { + it('should not show a warning message when using request.parameter config with LAMBDA-PROXY', () => { + awsCompileApigEvents.serverless.service.functions = { + first: { + events: [ + { + http: { + method: 'GET', + path: 'users/list', + integration: 'lambda-proxy', + request: { + parameters: { + querystrings: { + foo: true, + bar: false, + }, + paths: { + foo: true, + bar: false, + }, + headers: { + foo: true, + bar: false, + }, + }, + }, + }, + }, + ], + }, + }; + // initialize so we get the log method from the CLI in place + serverless.init(); + + const logStub = sinon.stub(serverless.cli, 'log'); + + awsCompileApigEvents.validate(); + + expect(logStub.called).to.be.equal(false); + }); + + it('should remove non-parameter request/response config with LAMBDA-PROXY', () => { awsCompileApigEvents.serverless.service.functions = { first: { events: [ @@ -1237,6 +1320,11 @@ describe('#validate()', () => { template: { 'template/1': '{ "stage" : "$context.stage" }', }, + parameters: { + paths: { + foo: true, + }, + }, }, response: {}, }, @@ -1252,8 +1340,10 @@ describe('#validate()', () => { const validated = awsCompileApigEvents.validate(); expect(validated.events).to.be.an('Array').with.length(1); - expect(validated.events[0].http.request).to.equal(undefined); expect(validated.events[0].http.response).to.equal(undefined); + expect(validated.events[0].http.request.parameters).to.deep.equal({ + 'method.request.path.foo': true, + }); }); it('should throw an error when an invalid integration type was provided', () => { diff --git a/package.json b/package.json index 76c77ae9b..51a326446 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "@impulse/serverless", + "name": "serverless", "version": "1.14.0", "engines": { "node": ">=4.0" From d636c083dc8584c2b26f924077b3da4dd6faf42e Mon Sep 17 00:00:00 2001 From: Travis Dimmig Date: Thu, 1 Jun 2017 13:07:43 -0400 Subject: [PATCH 4/6] Cleanup to make the linter happy --- .../compile/events/apiGateway/lib/validate.js | 21 ++++++++----------- .../events/apiGateway/lib/validate.test.js | 2 +- 2 files changed, 10 insertions(+), 13 deletions(-) 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 f2e84e25c..16794a7f8 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js @@ -96,24 +96,22 @@ module.exports = { } else if (http.integration === 'AWS_PROXY') { // show a warning when request / response config is used with AWS_PROXY (LAMBDA-PROXY) if (http.request) { - let keys = Object.keys(http.request); - if (!(keys.length === 1 && keys[0] === "parameters")) { + const keys = Object.keys(http.request); + if (!(keys.length === 1 && keys[0] === 'parameters')) { const requestWarningMessage = [ 'Warning! You\'re using the LAMBDA-PROXY in combination with request', - ` configuration in your function "${functionName}" Only the 'request.parameters'.`, - ' options are available in conjunction with LAMBDA-PROXY.', - ' Serverless will remove this configuration automatically before deployment.' + ` configuration in your function "${functionName}" Only the`, + ' \'request.parameters\'.options are available in conjunction with', + ' LAMBDA-PROXY. Serverless will remove this configuration automatically', + ' before deployment.', ].join(''); this.serverless.cli.log(requestWarningMessage); - - for (let key of keys) { - if (key === "parameters") { - continue; + for (const key of keys) { + if (key !== 'parameters') { + delete http.request[key]; } - delete http.request[key]; } } - if (Object.keys(http.request).length === 0) { // No keys left, delete the request object delete http.request; @@ -124,7 +122,6 @@ module.exports = { http.request.parameters = this.getRequestParameters(http.request); } } - } if (http.response) { const warningMessage = [ diff --git a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js index c61683c83..5369e5277 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.test.js @@ -1267,7 +1267,7 @@ describe('#validate()', () => { expect(logStub.args[0][0].length).to.be.at.least(1); }); - it('should not show a warning message when using request.parameter config with LAMBDA-PROXY', () => { + it('should not show a warning message when using request.parameter with LAMBDA-PROXY', () => { awsCompileApigEvents.serverless.service.functions = { first: { events: [ From f854e33885c3de4c240fc3e367219d5ba24c0a2e Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Thu, 6 Jul 2017 12:39:57 +0200 Subject: [PATCH 5/6] Update formatting of warning message --- .../aws/package/compile/events/apiGateway/lib/validate.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 16794a7f8..f3c596801 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js @@ -99,9 +99,9 @@ module.exports = { const keys = Object.keys(http.request); if (!(keys.length === 1 && keys[0] === 'parameters')) { const requestWarningMessage = [ - 'Warning! You\'re using the LAMBDA-PROXY in combination with request', - ` configuration in your function "${functionName}" Only the`, - ' \'request.parameters\'.options are available in conjunction with', + 'Warning! You\'re using the LAMBDA-PROXY in combination with a request', + ` configuration in your function "${functionName}". Only the`, + ' \'request.parameters\' configs are available in conjunction with', ' LAMBDA-PROXY. Serverless will remove this configuration automatically', ' before deployment.', ].join(''); From 22292f4a320308b2ffab22fb41e1f32803de7665 Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Thu, 6 Jul 2017 12:50:27 +0200 Subject: [PATCH 6/6] Update documentation --- docs/providers/aws/events/apigateway.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/docs/providers/aws/events/apigateway.md b/docs/providers/aws/events/apigateway.md index 79f7a7c0f..3f7022f99 100644 --- a/docs/providers/aws/events/apigateway.md +++ b/docs/providers/aws/events/apigateway.md @@ -332,10 +332,6 @@ Please note that those are the API keys names, not the actual values. Once you d Clients connecting to this Rest API will then need to set any of these API keys values in the `x-api-key` header of their request. This is only necessary for functions where the `private` property is set to true. -## Lambda Integration - -This method is more complicated and involves a lot more configuration of the `http` event syntax. - ### Request Parameters To pass optional and required parameters to your functions, so you can use them in API Gateway tests and SDK generation, marking them as `true` will make them required, `false` will make them optional. @@ -348,7 +344,6 @@ functions: - http: path: posts/create method: post - integration: lambda request: parameters: querystrings: @@ -369,13 +364,16 @@ functions: - http: path: posts/{id} method: get - integration: lambda request: parameters: paths: id: true ``` +## Lambda Integration + +This method is more complicated and involves a lot more configuration of the `http` event syntax. + ### Request templates #### Default Request Templates