Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 362bafd

Browse files
committedNov 22, 2023
chore(event_handler): only apply serialization at the end
1 parent 6a47ee8 commit 362bafd

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed
 

‎aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,8 @@ 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():
792+
self.response.body = self.serializer(self.response.body)
791793

792794
# We only apply the serializer when the content type is JSON and the
793795
# body is not a str, to avoid double encoding

‎tests/functional/event_handler/test_api_gateway.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def test_override_route_compress_parameter():
367367
# AND the Response object with compress=False
368368
app = ApiGatewayResolver()
369369
mock_event = {"path": "/my/request", "httpMethod": "GET", "headers": {"Accept-Encoding": "deflate, gzip"}}
370-
expected_value = '{"test": "value"}'
370+
expected_value = {"test": "value"}
371371

372372
@app.get("/my/request", compress=True)
373373
def with_compression() -> Response:
@@ -381,7 +381,7 @@ def handler(event, context):
381381

382382
# THEN the response is not compressed
383383
assert result["isBase64Encoded"] is False
384-
assert result["body"] == expected_value
384+
assert json.loads(result["body"]) == expected_value
385385
assert result["multiValueHeaders"].get("Content-Encoding") is None
386386

387387

@@ -681,7 +681,7 @@ def another_one():
681681
def test_no_content_response():
682682
# GIVEN a response with no content-type or body
683683
response = Response(status_code=204, content_type=None, body=None, headers=None)
684-
response_builder = ResponseBuilder(response)
684+
response_builder = ResponseBuilder(response, serializer=json.dumps)
685685

686686
# WHEN calling to_dict
687687
result = response_builder.build(APIGatewayProxyEvent(LOAD_GW_EVENT))
@@ -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():
@@ -1778,11 +1778,11 @@ def test_route_match_prioritize_full_match():
17781778

17791779
@router.get("/my/{path}")
17801780
def dynamic_handler() -> Response:
1781-
return Response(200, content_types.APPLICATION_JSON, json.dumps({"hello": "dynamic"}))
1781+
return Response(200, content_types.APPLICATION_JSON, {"hello": "dynamic"})
17821782

17831783
@router.get("/my/path")
17841784
def static_handler() -> Response:
1785-
return Response(200, content_types.APPLICATION_JSON, json.dumps({"hello": "static"}))
1785+
return Response(200, content_types.APPLICATION_JSON, {"hello": "static"})
17861786

17871787
app.include_router(router)
17881788

‎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)
Please sign in to comment.