Skip to content

Commit 1b64234

Browse files
fix(event_handler): make decoded_body field optional in ApiGateway resolver (#3937)
Making the field optional
1 parent ff7f37e commit 1b64234

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

aws_lambda_powertools/utilities/data_classes/common.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,16 @@ def body(self) -> Optional[str]:
150150
@cached_property
151151
def json_body(self) -> Any:
152152
"""Parses the submitted body as json"""
153-
return self._json_deserializer(self.decoded_body)
153+
if self.decoded_body:
154+
return self._json_deserializer(self.decoded_body)
155+
156+
return None
154157

155158
@cached_property
156-
def decoded_body(self) -> str:
157-
"""Dynamically base64 decode body as a str"""
158-
body: str = self["body"]
159-
if self.is_base64_encoded:
159+
def decoded_body(self) -> Optional[str]:
160+
"""Decode the body from base64 if encoded, otherwise return it as is."""
161+
body: Optional[str] = self.body
162+
if self.is_base64_encoded and body:
160163
return base64.b64decode(body.encode()).decode()
161164
return body
162165

tests/unit/test_data_classes.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -324,27 +324,13 @@ def test_base_proxy_event_get_header_value_case_insensitive():
324324
assert value is None
325325

326326

327-
def test_base_proxy_event_json_body_key_error():
328-
event = BaseProxyEvent({})
329-
with pytest.raises(KeyError) as ke:
330-
assert not event.json_body
331-
assert str(ke.value) == "'body'"
332-
333-
334327
def test_base_proxy_event_json_body():
335328
data = {"message": "Foo"}
336329
event = BaseProxyEvent({"body": json.dumps(data)})
337330
assert event.json_body == data
338331
assert event.json_body["message"] == "Foo"
339332

340333

341-
def test_base_proxy_event_decode_body_key_error():
342-
event = BaseProxyEvent({})
343-
with pytest.raises(KeyError) as ke:
344-
assert not event.decoded_body
345-
assert str(ke.value) == "'body'"
346-
347-
348334
def test_base_proxy_event_decode_body_encoded_false():
349335
data = "Foo"
350336
event = BaseProxyEvent({"body": data, "isBase64Encoded": False})

0 commit comments

Comments
 (0)