Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(event_handler): allow event source data class events #1159

Merged
merged 2 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,11 @@ def resolve(self, event, context) -> Dict[str, Any]:
dict
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="")
BaseRouter.current_event = self._to_proxy_event(event)
Expand Down
27 changes: 26 additions & 1 deletion tests/functional/event_handler/test_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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