From 7aad8193787f591cd3186b2f86e0f9bec23f4dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20Ormaechea?= Date: Tue, 1 Sep 2020 11:15:33 -0300 Subject: [PATCH] feat(AWS API Gateway): Allow to opt-out from default request templates (PR #8159) --- docs/providers/aws/events/apigateway.md | 16 ++++++++++ .../apiGateway/lib/method/index.test.js | 31 +++++++++++++++++++ .../apiGateway/lib/method/integration.js | 8 ++++- 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/docs/providers/aws/events/apigateway.md b/docs/providers/aws/events/apigateway.md index 77c99fa11b9..6680fc31cca 100644 --- a/docs/providers/aws/events/apigateway.md +++ b/docs/providers/aws/events/apigateway.md @@ -994,6 +994,22 @@ functions: You can then access the query string `https://example.com/dev/whatever?bar=123` by `event.foo` in the lambda function. If you want to spread a string into multiple lines, you can use the `>` or `|` syntax, but the following strings have to be all indented with the same amount, [read more about `>` syntax](http://stackoverflow.com/questions/3790454/in-yaml-how-do-i-break-a-string-over-multiple-lines). +In order to remove one of the default request templates you just need to pass it as null, as follows: + +```yml +functions: + create: + handler: posts.create + events: + - http: + method: get + path: whatever + integration: lambda + request: + template: + application/x-www-form-urlencoded: null +``` + #### Pass Through Behavior [API Gateway](https://serverless.com/amazon-api-gateway/) provides multiple ways to handle requests where the Content-Type header does not match any of the specified mapping templates. When this happens, the request payload will either be passed through the integration request _without transformation_ or rejected with a `415 - Unsupported Media Type`, depending on the configuration. diff --git a/lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.test.js b/lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.test.js index acfde4b271a..c1ab20a1c2e 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.test.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.test.js @@ -1304,6 +1304,37 @@ describe('#compileMethods()', () => { }); }); + it('should delete the default "application/x-www-form-urlencoded" template if it\'s overriden with null', () => { + awsCompileApigEvents.validated.events = [ + { + functionName: 'Second', + http: { + method: 'get', + path: 'users/list', + integration: 'AWS', + request: { + template: { + 'application/x-www-form-urlencoded': null, + }, + }, + response: { + statusCodes: { + 200: { + pattern: '', + }, + }, + }, + }, + }, + ]; + return awsCompileApigEvents.compileMethods().then(() => { + expect( + awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources + .ApiGatewayMethodUsersListGet.Properties.Integration.RequestTemplates + ).to.not.have.key('application/x-www-form-urlencoded'); + }); + }); + it('should use defined pass-through behavior', () => { awsCompileApigEvents.validated.events = [ { diff --git a/lib/plugins/aws/package/compile/events/apiGateway/lib/method/integration.js b/lib/plugins/aws/package/compile/events/apiGateway/lib/method/integration.js index 3be22495910..140b18edd6b 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/method/integration.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/method/integration.js @@ -209,7 +209,13 @@ module.exports = { // set custom request templates if provided if (http.request && typeof http.request.template === 'object') { - Object.assign(integrationRequestTemplates, http.request.template); + _.entries(http.request.template).forEach(([contentType, template]) => { + if (template === null) { + delete integrationRequestTemplates[contentType]; + } else { + integrationRequestTemplates[contentType] = template; + } + }); } return Object.keys(integrationRequestTemplates).length