Skip to content

Commit e57b59e

Browse files
committed
feat(event-handler): Use shared json Encoder
Also use more concise seperators
1 parent 0fe00f6 commit e57b59e

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from enum import Enum
66
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
77

8+
from aws_lambda_powertools.shared.json_encoder import Encoder
89
from aws_lambda_powertools.utilities.data_classes import ALBEvent, APIGatewayProxyEvent, APIGatewayProxyEventV2
910
from aws_lambda_powertools.utilities.data_classes.common import BaseProxyEvent
1011
from aws_lambda_powertools.utilities.typing import LambdaContext
@@ -104,7 +105,11 @@ def resolve(self, event, context) -> Dict[str, Any]:
104105
if isinstance(result, Response):
105106
response = result
106107
elif isinstance(result, dict):
107-
response = Response(status_code=200, content_type="application/json", body=json.dumps(result))
108+
response = Response(
109+
status_code=200,
110+
content_type="application/json",
111+
body=json.dumps(result, separators=(",", ":"), cls=Encoder),
112+
)
108113
else:
109114
response = Response(*result)
110115

tests/functional/event_handler/test_api_gateway.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import base64
22
import json
33
import zlib
4+
from decimal import Decimal
45
from pathlib import Path
56
from typing import Dict, Tuple
67

78
import pytest
89

910
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver, ProxyEventType, Response
11+
from aws_lambda_powertools.shared.json_encoder import Encoder
1012
from aws_lambda_powertools.utilities.data_classes import ALBEvent, APIGatewayProxyEvent, APIGatewayProxyEventV2
1113

1214

@@ -293,18 +295,20 @@ def handler(event, context):
293295
def test_rest_api():
294296
# GIVEN a function that returns a Dict
295297
app = ApiGatewayResolver(proxy_type=ProxyEventType.http_api_v1)
298+
expected_dict = {"foo": "value", "second": Decimal("100.01")}
296299

297300
@app.get("/my/path")
298301
def rest_func() -> Dict:
299-
return {"foo": "value"}
302+
return expected_dict
300303

301304
# WHEN calling the event handler
302305
result = app(LOAD_GW_EVENT, {})
303306

304307
# THEN automatically process this as a json rest api response
305308
assert result["statusCode"] == 200
306309
assert result["headers"]["Content-Type"] == APPLICATION_JSON
307-
assert result["body"] == json.dumps({"foo": "value"})
310+
expected_str = json.dumps(expected_dict, separators=(",", ":"), indent=None, cls=Encoder)
311+
assert result["body"] == expected_str
308312

309313

310314
def test_handling_response_type():

0 commit comments

Comments
 (0)