-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Description:
I have this function:
fakeFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/handlers/handlers.fake
Description: A Lambda function.
Events:
Api:
Type: HttpApi
Properties:
Path: /my-path/
Method: get
When I test this in local, even if I call the function without any queryString parameters, the event object still has the property queryStringParameters. However, after deployment, the property is not created at all.
so as an example, if my function was in Node.js, attempting this check: if (event.queryStringParameters.hasOwnProperty('myproperty')){...} will work in SAM local, but after deployment will result in the error “cannot read property ‘myproperty’ of undefined”.
the only way it would work after deployment, is this:
if (event.hasOwnProperty('queryStringParameters') && event.queryStringParameters.hasOwnProperty('myproperty'))
I think the emulator should work in the same way as "real world" Api Gateway, so queryStringParameters should be either undefined or non-empty.
Steps to reproduce:
- create a function defined as in the
template.ymlsnippet above, then include this check in the function:
if (event.queryStringParameters.hasOwnProperty('myproperty')) {
console.log("checking)
}
- run
sam local start-apiand call the function without any query string parameters. It will work. - deploy the function, then call it without any query string parameters. You will get 500 error.
Observed result:
Go to CloudWatch and check the function logs after completing step 3 above.
In CloudWatch you will find an error message such as the one below:
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'hasOwnProperty' of undefined",
"stack": [
"TypeError: Cannot read property 'hasOwnProperty' of undefined",
" at Runtime.exports.fakeFunction [as handler] (/var/task/src/handlers/handlers.js:43:37)",
" at processTicksAndRejections (internal/process/task_queues.js:95:5)"
]
}
However there will be no error when executing the function locally, as observed at point 2 above.
Expected result:
The expected result is that the same error "Cannot read property 'hasOwnProperty' of undefined" should also be thrown in local simulator.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: Windows 10
sam --version: SAM CLI, version 1.23.0- AWS region: ap-southeast-2