From 9e8cedc11a3198576762765162a7af571d8471fb Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Fri, 23 Jul 2021 16:46:58 -0700 Subject: [PATCH 1/2] feat(data-classes): use decoded_body for json_body --- aws_lambda_powertools/utilities/data_classes/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws_lambda_powertools/utilities/data_classes/common.py b/aws_lambda_powertools/utilities/data_classes/common.py index 66c8f15324f..fbf0502125e 100644 --- a/aws_lambda_powertools/utilities/data_classes/common.py +++ b/aws_lambda_powertools/utilities/data_classes/common.py @@ -65,7 +65,7 @@ def body(self) -> Optional[str]: @property def json_body(self) -> Any: """Parses the submitted body as json""" - return json.loads(self["body"]) + return json.loads(self.decoded_body) @property def decoded_body(self) -> str: From 350ef0328767640ad5e8ae2d8febd4f5d070abd0 Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Fri, 23 Jul 2021 16:56:09 -0700 Subject: [PATCH 2/2] test(data-classes): add json_body test case for isBase64Encoded --- tests/functional/test_data_classes.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/functional/test_data_classes.py b/tests/functional/test_data_classes.py index cbbaf834379..f9bb1fdef73 100644 --- a/tests/functional/test_data_classes.py +++ b/tests/functional/test_data_classes.py @@ -1037,6 +1037,18 @@ def test_base_proxy_event_decode_body_encoded_true(): assert event.decoded_body == data +def test_base_proxy_event_json_body_with_base64_encoded_data(): + # GIVEN a base64 encoded json body + data = {"message": "Foo"} + data_str = json.dumps(data) + encoded_data = base64.b64encode(data_str.encode()).decode() + event = BaseProxyEvent({"body": encoded_data, "isBase64Encoded": True}) + + # WHEN calling json_body + # THEN then base64 decode and json load + assert event.json_body == data + + def test_kinesis_stream_event(): event = KinesisStreamEvent(load_event("kinesisStreamEvent.json"))