Skip to content

Commit 0c26417

Browse files
committed
fix: avoid double encoding
1 parent 4619fe8 commit 0c26417

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,10 @@ def build(self, event: ResponseEventT, cors: Optional[CORSConfig] = None) -> Dic
789789
logger.debug("Encoding bytes response with base64")
790790
self.response.base64_encoded = True
791791
self.response.body = base64.b64encode(self.response.body).decode()
792-
elif self.response.is_json():
792+
793+
# We only apply the serializer when the content type is JSON and the
794+
# body is not a str, to avoid double encoding
795+
elif self.response.is_json() and not isinstance(self.response.body, str):
793796
self.response.body = self.serializer(self.response.body)
794797

795798
# We only apply the serializer when the content type is JSON and the

tests/functional/event_handler/test_api_gateway.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ def get_lambda() -> Response:
14821482
# THEN call the exception_handler
14831483
assert result["statusCode"] == 500
14841484
assert result["multiValueHeaders"]["Content-Type"] == [content_types.APPLICATION_JSON]
1485-
assert result["body"] == '"CUSTOM ERROR FORMAT"'
1485+
assert result["body"] == "CUSTOM ERROR FORMAT"
14861486

14871487

14881488
def test_exception_handler_not_found():

tests/functional/event_handler/test_base_path.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def handle():
2121

2222
result = app(event, {})
2323
assert result["statusCode"] == 200
24-
assert result["body"] == '""'
24+
assert result["body"] == ""
2525

2626

2727
def test_base_path_api_gateway_http():
@@ -38,7 +38,7 @@ def handle():
3838

3939
result = app(event, {})
4040
assert result["statusCode"] == 200
41-
assert result["body"] == '""'
41+
assert result["body"] == ""
4242

4343

4444
def test_base_path_alb():
@@ -53,7 +53,7 @@ def handle():
5353

5454
result = app(event, {})
5555
assert result["statusCode"] == 200
56-
assert result["body"] == '""'
56+
assert result["body"] == ""
5757

5858

5959
def test_base_path_lambda_function_url():
@@ -70,7 +70,7 @@ def handle():
7070

7171
result = app(event, {})
7272
assert result["statusCode"] == 200
73-
assert result["body"] == '""'
73+
assert result["body"] == ""
7474

7575

7676
def test_vpc_lattice():
@@ -85,7 +85,7 @@ def handle():
8585

8686
result = app(event, {})
8787
assert result["statusCode"] == 200
88-
assert result["body"] == '""'
88+
assert result["body"] == ""
8989

9090

9191
def test_vpc_latticev2():
@@ -100,4 +100,4 @@ def handle():
100100

101101
result = app(event, {})
102102
assert result["statusCode"] == 200
103-
assert result["body"] == '""'
103+
assert result["body"] == ""

0 commit comments

Comments
 (0)