Skip to content

Commit

Permalink
fix(event_handler): Allow for event_source support (#1159)
Browse files Browse the repository at this point in the history
Co-authored-by: heitorlessa <[email protected]>
  • Loading branch information
Michael Brewer and heitorlessa authored Apr 28, 2022
1 parent f720559 commit 850e3bc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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

0 comments on commit 850e3bc

Please sign in to comment.