Skip to content

fix(apigateway): support nested router decorators #1709

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

Merged
merged 2 commits into from
Nov 14, 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
1 change: 1 addition & 0 deletions aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ def register_route(func: Callable):
# Convert methods to tuple. It needs to be hashable as its part of the self._routes dict key
methods = (method,) if isinstance(method, str) else tuple(method)
self._routes[(rule, methods, cors, compress, cache_control)] = func
return func

return register_route

Expand Down
80 changes: 80 additions & 0 deletions tests/events/apiGatewayProxyEventAnotherPath.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"version": "1.0",
"resource": "/my/anotherPath",
"path": "/my/anotherPath",
"httpMethod": "GET",
"headers": {
"Header1": "value1",
"Header2": "value2"
},
"multiValueHeaders": {
"Header1": [
"value1"
],
"Header2": [
"value1",
"value2"
]
},
"queryStringParameters": {
"parameter1": "value1",
"parameter2": "value"
},
"multiValueQueryStringParameters": {
"parameter1": [
"value1",
"value2"
],
"parameter2": [
"value"
]
},
"requestContext": {
"accountId": "123456789012",
"apiId": "id",
"authorizer": {
"claims": null,
"scopes": null
},
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"extendedRequestId": "request-id",
"httpMethod": "GET",
"identity": {
"accessKey": null,
"accountId": null,
"caller": null,
"cognitoAuthenticationProvider": null,
"cognitoAuthenticationType": null,
"cognitoIdentityId": null,
"cognitoIdentityPoolId": null,
"principalOrgId": null,
"sourceIp": "192.168.0.1/32",
"user": null,
"userAgent": "user-agent",
"userArn": null,
"clientCert": {
"clientCertPem": "CERT_CONTENT",
"subjectDN": "www.example.com",
"issuerDN": "Example issuer",
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
"validity": {
"notBefore": "May 28 12:30:02 2019 GMT",
"notAfter": "Aug 5 09:36:04 2021 GMT"
}
}
},
"path": "/my/anotherPath",
"protocol": "HTTP/1.1",
"requestId": "id=",
"requestTime": "04/Mar/2020:19:15:17 +0000",
"requestTimeEpoch": 1583349317135,
"resourceId": null,
"resourcePath": "/my/anotherPath",
"stage": "$default"
},
"pathParameters": null,
"stageVariables": null,
"body": "Hello from Lambda!",
"isBase64Encoded": true
}
43 changes: 43 additions & 0 deletions tests/functional/event_handler/test_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -1476,3 +1476,46 @@ def test_include_router_merges_context():
app.include_router(router)

assert app.context == router.context


def test_nested_app_decorator():
# GIVEN a Http API V1 proxy type event
# with a function registered with two distinct routes
app = APIGatewayRestResolver()

@app.get("/my/path")
@app.get("/my/anotherPath")
def get_lambda() -> Response:
return Response(200, content_types.APPLICATION_JSON, json.dumps({"foo": "value"}))

# WHEN calling the event handler
result = app(LOAD_GW_EVENT, {})
result2 = app(load_event("apiGatewayProxyEventAnotherPath.json"), {})

# THEN process event correctly
# AND set the current_event type as APIGatewayProxyEvent
assert result["statusCode"] == 200
assert result2["statusCode"] == 200


def test_nested_router_decorator():
# GIVEN a Http API V1 proxy type event
# with a function registered with two distinct routes
app = APIGatewayRestResolver()
router = Router()

@router.get("/my/path")
@router.get("/my/anotherPath")
def get_lambda() -> Response:
return Response(200, content_types.APPLICATION_JSON, json.dumps({"foo": "value"}))

app.include_router(router)

# WHEN calling the event handler
result = app(LOAD_GW_EVENT, {})
result2 = app(load_event("apiGatewayProxyEventAnotherPath.json"), {})

# THEN process event correctly
# AND set the current_event type as APIGatewayProxyEvent
assert result["statusCode"] == 200
assert result2["statusCode"] == 200