From 462dbf263b0a3123986e0bd8a7fffb1272b544ca Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Tue, 26 Apr 2022 10:18:55 -0700 Subject: [PATCH 1/2] fix(event_handler): Allow for event_source support Changes: - Allow for event being of type 'BaseProxyEvent' - Add test case closes #1152 --- .../event_handler/api_gateway.py | 2 ++ .../event_handler/test_api_gateway.py | 27 ++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/aws_lambda_powertools/event_handler/api_gateway.py b/aws_lambda_powertools/event_handler/api_gateway.py index 5d57c645b27..fad6589b3da 100644 --- a/aws_lambda_powertools/event_handler/api_gateway.py +++ b/aws_lambda_powertools/event_handler/api_gateway.py @@ -491,6 +491,8 @@ def resolve(self, event, context) -> Dict[str, Any]: dict Returns the dict response """ + if isinstance(event, BaseProxyEvent): + event = event.raw_event if self._debug: print(self._json_dump(event), end="") BaseRouter.current_event = self._to_proxy_event(event) diff --git a/tests/functional/event_handler/test_api_gateway.py b/tests/functional/event_handler/test_api_gateway.py index 9b5835c8b20..3738cd11456 100644 --- a/tests/functional/event_handler/test_api_gateway.py +++ b/tests/functional/event_handler/test_api_gateway.py @@ -31,7 +31,12 @@ ) from aws_lambda_powertools.shared import constants from aws_lambda_powertools.shared.json_encoder import Encoder -from aws_lambda_powertools.utilities.data_classes import ALBEvent, APIGatewayProxyEvent, APIGatewayProxyEventV2 +from aws_lambda_powertools.utilities.data_classes import ( + ALBEvent, + APIGatewayProxyEvent, + APIGatewayProxyEventV2, + event_source, +) from tests.functional.utils import load_event @@ -1210,3 +1215,23 @@ def handle_not_found(_) -> Response: # THEN call the @app.not_found() function assert result["statusCode"] == 404 + + +def test_event_source_compatibility(): + # GIVEN + app = APIGatewayHttpResolver() + + @app.post("/my/path") + def my_path(): + assert isinstance(app.current_event, APIGatewayProxyEventV2) + return {} + + # WHEN + @event_source(data_class=APIGatewayProxyEventV2) + def handler(event: APIGatewayProxyEventV2, context): + assert isinstance(event, APIGatewayProxyEventV2) + return app.resolve(event, context) + + # THEN + result = handler(load_event("apiGatewayProxyV2Event.json"), None) + assert result["statusCode"] == 200 From 8c241096b1e0a686d8bb989fc7fc4c036fde65eb Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Thu, 28 Apr 2022 14:30:54 +0200 Subject: [PATCH 2/2] fix: warn customers about unnecessary perf op --- aws_lambda_powertools/event_handler/api_gateway.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aws_lambda_powertools/event_handler/api_gateway.py b/aws_lambda_powertools/event_handler/api_gateway.py index fad6589b3da..0c738354fd4 100644 --- a/aws_lambda_powertools/event_handler/api_gateway.py +++ b/aws_lambda_powertools/event_handler/api_gateway.py @@ -492,6 +492,9 @@ def resolve(self, event, context) -> Dict[str, Any]: Returns the dict response """ if isinstance(event, BaseProxyEvent): + warnings.warn( + "You don't need to serialize event to Event Source Data Class when using Event Handler; see issue #1152" + ) event = event.raw_event if self._debug: print(self._json_dump(event), end="")