Skip to content

Bug: APIGatewayProxyEventV2.decoded_body should not cause KeyError #3706

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

Closed
aahung opened this issue Feb 5, 2024 · 6 comments · Fixed by #3937
Closed

Bug: APIGatewayProxyEventV2.decoded_body should not cause KeyError #3706

aahung opened this issue Feb 5, 2024 · 6 comments · Fixed by #3937
Labels
need-more-information Pending information to continue

Comments

@aahung
Copy link

aahung commented Feb 5, 2024

Expected Behaviour

For event of type APIGatewayProxyEventV2, event.decoded_body or event.json_body (which calls the former) could result in KeyError when there is no body.

Current Behaviour

image

Code snippet

`event.decoded_body` or `event.json_body`

Possible Solution

  • event.decoded_body should return empty string or None
  • event.json_body should raise JSONDecodeError

Steps to Reproduce

As described.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.11

Packaging format used

PyPi

Debugging logs

No response

@aahung aahung added bug Something isn't working triage Pending triage from maintainers labels Feb 5, 2024
Copy link

boring-cyborg bot commented Feb 5, 2024

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@leandrodamascena
Copy link
Contributor

Hey @aahung! Thanks for reporting this bug. You're right, we should use body: str = self.body instead of body: str = self["body"] in this line https://github.com/aws-powertools/powertools-lambda-python/blob/develop/aws_lambda_powertools/utilities/data_classes/common.py#L149.

Do you want to send a PR to fix this? We would love it.

Thanks

@leandrodamascena leandrodamascena removed the triage Pending triage from maintainers label Feb 5, 2024
@leandrodamascena leandrodamascena moved this from Triage to Pending customer in Powertools for AWS Lambda (Python) Feb 5, 2024
@heitorlessa
Copy link
Contributor

Hi @aahung could you share how you ended up with the body key missing in the first place? Was this an actual request from API Gateway HTTP API, or a fake event?

Thank you

@heitorlessa heitorlessa added need-more-information Pending information to continue and removed bug Something isn't working labels Feb 9, 2024
@isaacasancheza
Copy link

Hello there,

In my case it's an actual request from API Gateway HTTP API. Sending a POST without body throws the error (in my case using Postman with Body as none). I'm using aws_lambda_powertools.event_handler.APIGatewayHttpResolver with enable_validation=True.

Traceback (most recent call last):
  File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 2002, in _call_route
    route(router_middlewares=self._router_middlewares, app=self, route_arguments=route_arguments),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 400, in __call__
    return self._middleware_stack(app)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 1291, in __call__
    return self.current_middleware(app, self.next_middleware)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/python/aws_lambda_powertools/event_handler/middlewares/base.py", line 121, in __call__
    return self.handler(app, next_middleware)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/python/aws_lambda_powertools/event_handler/middlewares/openapi_validation.py", line 117, in handler
    received_body=self._get_body(app),
                  ^^^^^^^^^^^^^^^^^^^
  File "/opt/python/aws_lambda_powertools/event_handler/middlewares/openapi_validation.py", line 243, in _get_body
    return app.current_event.json_body
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/lang/lib/python3.12/functools.py", line 995, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/opt/python/aws_lambda_powertools/utilities/data_classes/common.py", line 153, in json_body
    return self._json_deserializer(self.decoded_body)
                                   ^^^^^^^^^^^^^^^^^
  File "/var/lang/lib/python3.12/functools.py", line 995, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/opt/python/aws_lambda_powertools/utilities/data_classes/common.py", line 158, in decoded_body
    body: str = self["body"]
                ~~~~^^^^^^^^
  File "/opt/python/aws_lambda_powertools/utilities/data_classes/common.py", line 32, in __getitem__
    return self._data[key]
           ~~~~~~~~~~^^^^^
KeyError: 'body'

Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Mar 13, 2024
@leandrodamascena leandrodamascena moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Mar 18, 2024
Copy link
Contributor

This is now released under 2.36.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need-more-information Pending information to continue
Projects
Status: Shipped
4 participants