@@ -1579,6 +1579,7 @@ def __init__(
15791579 strip_prefixes : list [str | Pattern ] | None = None ,
15801580 enable_validation : bool = False ,
15811581 response_validation_error_http_code : HTTPStatus | int | None = None ,
1582+ json_body_deserializer : Callable [[str ], dict ] | None = None ,
15821583 ):
15831584 """
15841585 Parameters
@@ -1600,6 +1601,9 @@ def __init__(
16001601 Enables validation of the request body against the route schema, by default False.
16011602 response_validation_error_http_code
16021603 Sets the returned status code if response is not validated. enable_validation must be True.
1604+ json_body_deserializer: Callable[[str], dict], optional
1605+ function to deserialize `str`, `bytes`, `bytearray` containing a JSON document to a Python `dict`,
1606+ by default json.loads when integrating with EventSource data class
16031607 """
16041608 self ._proxy_type = proxy_type
16051609 self ._dynamic_routes : list [Route ] = []
@@ -1625,6 +1629,7 @@ def __init__(
16251629
16261630 # Allow for a custom serializer or a concise json serialization
16271631 self ._serializer = serializer or partial (json .dumps , separators = ("," , ":" ), cls = Encoder )
1632+ self ._json_body_deserializer = json_body_deserializer
16281633
16291634 if self ._enable_validation :
16301635 from aws_lambda_powertools .event_handler .middlewares .openapi_validation import OpenAPIValidationMiddleware
@@ -2436,24 +2441,24 @@ def _to_proxy_event(self, event: dict) -> BaseProxyEvent: # noqa: PLR0911 # ig
24362441 """Convert the event dict to the corresponding data class"""
24372442 if self ._proxy_type == ProxyEventType .APIGatewayProxyEvent :
24382443 logger .debug ("Converting event to API Gateway REST API contract" )
2439- return APIGatewayProxyEvent (event )
2444+ return APIGatewayProxyEvent (event , self . _json_body_deserializer )
24402445 if self ._proxy_type == ProxyEventType .APIGatewayProxyEventV2 :
24412446 logger .debug ("Converting event to API Gateway HTTP API contract" )
2442- return APIGatewayProxyEventV2 (event )
2447+ return APIGatewayProxyEventV2 (event , self . _json_body_deserializer )
24432448 if self ._proxy_type == ProxyEventType .BedrockAgentEvent :
24442449 logger .debug ("Converting event to Bedrock Agent contract" )
2445- return BedrockAgentEvent (event )
2450+ return BedrockAgentEvent (event , self . _json_body_deserializer )
24462451 if self ._proxy_type == ProxyEventType .LambdaFunctionUrlEvent :
24472452 logger .debug ("Converting event to Lambda Function URL contract" )
2448- return LambdaFunctionUrlEvent (event )
2453+ return LambdaFunctionUrlEvent (event , self . _json_body_deserializer )
24492454 if self ._proxy_type == ProxyEventType .VPCLatticeEvent :
24502455 logger .debug ("Converting event to VPC Lattice contract" )
2451- return VPCLatticeEvent (event )
2456+ return VPCLatticeEvent (event , self . _json_body_deserializer )
24522457 if self ._proxy_type == ProxyEventType .VPCLatticeEventV2 :
24532458 logger .debug ("Converting event to VPC LatticeV2 contract" )
2454- return VPCLatticeEventV2 (event )
2459+ return VPCLatticeEventV2 (event , self . _json_body_deserializer )
24552460 logger .debug ("Converting event to ALB contract" )
2456- return ALBEvent (event )
2461+ return ALBEvent (event , self . _json_body_deserializer )
24572462
24582463 def _resolve (self ) -> ResponseBuilder :
24592464 """Resolves the response or return the not found response"""
@@ -2870,6 +2875,7 @@ def __init__(
28702875 strip_prefixes : list [str | Pattern ] | None = None ,
28712876 enable_validation : bool = False ,
28722877 response_validation_error_http_code : HTTPStatus | int | None = None ,
2878+ json_body_deserializer : Callable [[str ], dict ] | None = None ,
28732879 ):
28742880 """Amazon API Gateway REST and HTTP API v1 payload resolver"""
28752881 super ().__init__ (
@@ -2880,6 +2886,7 @@ def __init__(
28802886 strip_prefixes ,
28812887 enable_validation ,
28822888 response_validation_error_http_code ,
2889+ json_body_deserializer = json_body_deserializer ,
28832890 )
28842891
28852892 def _get_base_path (self ) -> str :
@@ -2956,6 +2963,7 @@ def __init__(
29562963 strip_prefixes : list [str | Pattern ] | None = None ,
29572964 enable_validation : bool = False ,
29582965 response_validation_error_http_code : HTTPStatus | int | None = None ,
2966+ json_body_deserializer : Callable [[str ], dict ] | None = None ,
29592967 ):
29602968 """Amazon API Gateway HTTP API v2 payload resolver"""
29612969 super ().__init__ (
@@ -2966,6 +2974,7 @@ def __init__(
29662974 strip_prefixes ,
29672975 enable_validation ,
29682976 response_validation_error_http_code ,
2977+ json_body_deserializer = json_body_deserializer ,
29692978 )
29702979
29712980 def _get_base_path (self ) -> str :
@@ -2995,6 +3004,7 @@ def __init__(
29953004 strip_prefixes : list [str | Pattern ] | None = None ,
29963005 enable_validation : bool = False ,
29973006 response_validation_error_http_code : HTTPStatus | int | None = None ,
3007+ json_body_deserializer : Callable [[str ], dict ] | None = None ,
29983008 ):
29993009 """Amazon Application Load Balancer (ALB) resolver"""
30003010 super ().__init__ (
@@ -3005,6 +3015,7 @@ def __init__(
30053015 strip_prefixes ,
30063016 enable_validation ,
30073017 response_validation_error_http_code ,
3018+ json_body_deserializer = json_body_deserializer ,
30083019 )
30093020
30103021 def _get_base_path (self ) -> str :
0 commit comments