Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws apiGateway event - make sure http request parameters have boolean value #8329

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,7 +14,13 @@ module.exports = {
const requestParameters = {};
if (event.http.request && event.http.request.parameters) {
Object.entries(event.http.request.parameters).forEach(([key, value]) => {
requestParameters[key] = value.required === undefined ? value : value.required;
/* eslint-disable no-nested-ternary */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not disable lint. Technically this rule is turned on, on purpose, so code style like that is not introduced :)

I know I've written comment abusing that rule, but it was just for quick reference.

We can write it differently, probably with IIFE:

requestParameters[key] = (() => {
  if (!_.isObject(value)) return value;
  return value.required != null ? value.required : true
})();

requestParameters[key] = _.isObject(value)
? value.required != null
? value.required
: true
: value;
/* eslint-enable no-nested-ternary */
});
}

Expand Down
Expand Up @@ -603,6 +603,35 @@ describe('#compileMethods()', () => {
});
});

it('should set required to true when omitted from mapped value', () => {
awsCompileApigEvents.validated.events = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All new tests should be introduced with runServerless util, see https://github.com/serverless/serverless/tree/master/test#unit-tests

Relying on mocked awsCompileApigEvents and other classes like that, is very painful for eventual future refactors

{
functionName: 'First',
http: {
path: 'users/create',
method: 'post',
integration: 'HTTP_PROXY',
request: {
uri: 'https://example.com',
parameters: {
'method.request.querystring.foo': {
mappedValue: 'fooValue',
},
},
},
},
},
];
return awsCompileApigEvents.compileMethods().then(() => {
expect(
awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources
.ApiGatewayMethodUsersCreatePost.Properties.RequestParameters[
'method.request.querystring.foo'
]
).to.equal(true);
});
});

it('should set authorizer config for AWS_IAM', () => {
awsCompileApigEvents.validated.events = [
{
Expand Down