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

feat(data-classes): decode json_body if based64 encoded #560

Merged
merged 2 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we fix the older type annotation you Dict[str, Any] here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It could be a list too :)

"""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