Skip to content

Commit 8883e33

Browse files
committed
fix: avoid double encoding
1 parent 362bafd commit 8883e33

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

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

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

tests/functional/event_handler/test_api_gateway.py

+1-1
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

+6-6
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)