Skip to content

Commit

Permalink
feat(data-classes): decode json_body if based64 encoded (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Brewer committed Jul 27, 2021
1 parent 6240957 commit 1135314
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion aws_lambda_powertools/utilities/data_classes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/test_data_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down

0 comments on commit 1135314

Please sign in to comment.