Skip to content

Commit 618bc3e

Browse files
committed
fix: avoid double encoding
1 parent 27ac461 commit 618bc3e

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,10 @@ def build(self, event: ResponseEventT, cors: Optional[CORSConfig] = None) -> Dic
783783
logger.debug("Encoding bytes response with base64")
784784
self.response.base64_encoded = True
785785
self.response.body = base64.b64encode(self.response.body).decode()
786-
elif self.response.is_json():
786+
787+
# We only apply the serializer when the content type is JSON and the
788+
# body is not a str, to avoid double encoding
789+
elif self.response.is_json() and not isinstance(self.response.body, str):
787790
self.response.body = self.serializer(self.response.body)
788791

789792
return {

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

tests/functional/event_handler/test_openapi_validation_middleware.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def handler() -> str:
163163
# THEN the body must be a string
164164
result = app(LOAD_GW_EVENT, {})
165165
assert result["statusCode"] == 200
166-
assert result["body"] == json.dumps(sample_path.as_posix())
166+
assert result["body"] == sample_path.as_posix()
167167

168168

169169
def test_validate_return_enum():
@@ -184,7 +184,7 @@ def handler() -> Model:
184184
# THEN the body must be a string
185185
result = app(LOAD_GW_EVENT, {})
186186
assert result["statusCode"] == 200
187-
assert result["body"] == '"powertools"'
187+
assert result["body"] == "powertools"
188188

189189

190190
def test_validate_return_dataclass():

0 commit comments

Comments
 (0)