Skip to content

Commit 4cd416a

Browse files
fix(api_gateway): allow whitespace in routes' path parameter (#1099)
Co-authored-by: Heitor Lessa <[email protected]>
1 parent 862e3fc commit 4cd416a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
_DYNAMIC_ROUTE_PATTERN = r"(<\w+>)"
2727
_SAFE_URI = "-._~()'!*:@,;" # https://www.ietf.org/rfc/rfc3986.txt
2828
# API GW/ALB decode non-safe URI chars; we must support them too
29-
_UNSAFE_URI = "%<>\[\]{}|^" # noqa: W605
29+
_UNSAFE_URI = "%<> \[\]{}|^" # noqa: W605
3030
_NAMED_GROUP_BOUNDARY_PATTERN = fr"(?P\1[{_SAFE_URI}{_UNSAFE_URI}\\w]+)"
3131

3232

tests/functional/event_handler/test_api_gateway.py

+36
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,42 @@ def get_network_account(account_id: str, network_id: str):
715715
app.resolve(event, {})
716716

717717

718+
def test_similar_dynamic_routes_with_whitespaces():
719+
# GIVEN
720+
app = ApiGatewayResolver()
721+
event = deepcopy(LOAD_GW_EVENT)
722+
723+
# WHEN
724+
# r'^/accounts/(?P<account_id>\\w+\\b)$' # noqa: E800
725+
@app.get("/accounts/<account_id>")
726+
def get_account(account_id: str):
727+
assert account_id == "single account"
728+
729+
# r'^/accounts/(?P<account_id>\\w+\\b)/source_networks$' # noqa: E800
730+
@app.get("/accounts/<account_id>/source_networks")
731+
def get_account_networks(account_id: str):
732+
assert account_id == "nested account"
733+
734+
# r'^/accounts/(?P<account_id>\\w+\\b)/source_networks/(?P<network_id>\\w+\\b)$' # noqa: E800
735+
@app.get("/accounts/<account_id>/source_networks/<network_id>")
736+
def get_network_account(account_id: str, network_id: str):
737+
assert account_id == "nested account"
738+
assert network_id == "network 123"
739+
740+
# THEN
741+
event["resource"] = "/accounts/{account_id}"
742+
event["path"] = "/accounts/single account"
743+
assert app.resolve(event, {})["statusCode"] == 200
744+
745+
event["resource"] = "/accounts/{account_id}/source_networks"
746+
event["path"] = "/accounts/nested account/source_networks"
747+
assert app.resolve(event, {})["statusCode"] == 200
748+
749+
event["resource"] = "/accounts/{account_id}/source_networks/{network_id}"
750+
event["path"] = "/accounts/nested account/source_networks/network 123"
751+
assert app.resolve(event, {})["statusCode"] == 200
752+
753+
718754
@pytest.mark.parametrize(
719755
"req",
720756
[

0 commit comments

Comments
 (0)