Skip to content

Commit 3a408ed

Browse files
committed
Bugfix ensure that the entire part is matched
Previously the router would accept a partial match of a part, which is contrary to how the router is meant to work. For example if the rule to be matched was `d+` the router would match `2dfd` as `2` and silently forget the rest.
1 parent 76ccf1f commit 3a408ed

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Unreleased
77

88
- Fix router so that ``/path/`` will match a rule ``/path`` if strict
99
slashes mode is disabled for the rule. :issue:`2467`
10+
- Fix router so that partial part matches are not allowed
11+
i.e. ``/2df`` does not match ``/<int>``. :pr:`2470`
1012
- Restore ``ValidationError`` to be importable from
1113
``werkzeug.routing``. :issue:`2465`
1214

src/werkzeug/routing/rules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ def _parse_rule(self, rule: str) -> t.Iterable[RulePart]:
625625
-len(argument_weights),
626626
argument_weights,
627627
)
628-
if final:
628+
if not static:
629629
content += r"$\Z"
630630
yield RulePart(
631631
content=content, final=final, static=static, weight=weight

tests/test_routing.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,3 +1400,18 @@ def test_newline_match():
14001400

14011401
with pytest.raises(NotFound):
14021402
a.match("/hello\n")
1403+
1404+
1405+
def test_weighting():
1406+
m = r.Map(
1407+
[
1408+
r.Rule("/<int:value>", endpoint="int"),
1409+
r.Rule("/<uuid:value>", endpoint="uuid"),
1410+
]
1411+
)
1412+
a = m.bind("localhost")
1413+
1414+
assert a.match("/2b5b0911-fdcf-4dd2-921b-28ace88db8a0") == (
1415+
"uuid",
1416+
{"value": uuid.UUID("2b5b0911-fdcf-4dd2-921b-28ace88db8a0")},
1417+
)

0 commit comments

Comments
 (0)