Skip to content

Commit

Permalink
feat(AWS API Gateway): Allow to opt-out from default request templates
Browse files Browse the repository at this point in the history
(PR #8159)
  • Loading branch information
jormaechea committed Sep 1, 2020
1 parent a2d1031 commit 7aad819
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/providers/aws/events/apigateway.md
Expand Up @@ -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.
Expand Down
Expand Up @@ -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 = [
{
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit 7aad819

Please sign in to comment.