Skip to content

Commit 462dbf2

Browse files
author
Michael Brewer
committed
fix(event_handler): Allow for event_source support
Changes: - Allow for event being of type 'BaseProxyEvent' - Add test case closes #1152
1 parent a6db70c commit 462dbf2

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Diff for: aws_lambda_powertools/event_handler/api_gateway.py

+2
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ def resolve(self, event, context) -> Dict[str, Any]:
491491
dict
492492
Returns the dict response
493493
"""
494+
if isinstance(event, BaseProxyEvent):
495+
event = event.raw_event
494496
if self._debug:
495497
print(self._json_dump(event), end="")
496498
BaseRouter.current_event = self._to_proxy_event(event)

Diff for: tests/functional/event_handler/test_api_gateway.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@
3131
)
3232
from aws_lambda_powertools.shared import constants
3333
from aws_lambda_powertools.shared.json_encoder import Encoder
34-
from aws_lambda_powertools.utilities.data_classes import ALBEvent, APIGatewayProxyEvent, APIGatewayProxyEventV2
34+
from aws_lambda_powertools.utilities.data_classes import (
35+
ALBEvent,
36+
APIGatewayProxyEvent,
37+
APIGatewayProxyEventV2,
38+
event_source,
39+
)
3540
from tests.functional.utils import load_event
3641

3742

@@ -1210,3 +1215,23 @@ def handle_not_found(_) -> Response:
12101215

12111216
# THEN call the @app.not_found() function
12121217
assert result["statusCode"] == 404
1218+
1219+
1220+
def test_event_source_compatibility():
1221+
# GIVEN
1222+
app = APIGatewayHttpResolver()
1223+
1224+
@app.post("/my/path")
1225+
def my_path():
1226+
assert isinstance(app.current_event, APIGatewayProxyEventV2)
1227+
return {}
1228+
1229+
# WHEN
1230+
@event_source(data_class=APIGatewayProxyEventV2)
1231+
def handler(event: APIGatewayProxyEventV2, context):
1232+
assert isinstance(event, APIGatewayProxyEventV2)
1233+
return app.resolve(event, context)
1234+
1235+
# THEN
1236+
result = handler(load_event("apiGatewayProxyV2Event.json"), None)
1237+
assert result["statusCode"] == 200

0 commit comments

Comments
 (0)