Skip to content

Commit 4d1da57

Browse files
fix: handle leading forward slash in pattern matching path function (#116)
1 parent f0d6007 commit 4d1da57

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/firebase_functions/private/path_pattern.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,10 @@ class PathPattern:
129129
segments: list[PathSegment]
130130

131131
def __init__(self, raw_path: str):
132-
self.raw = raw_path
132+
normalized_path = raw_path.strip("/")
133+
self.raw = normalized_path
133134
self.segments = []
134-
self.init_path_segments(raw_path)
135+
self.init_path_segments(normalized_path)
135136

136137
def init_path_segments(self, raw: str):
137138
parts = raw.split("/")

tests/test_path_pattern.py

+22
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ def test_trim_param(self):
4141
self.assertEqual(trim_param("{something=*}"), "something")
4242

4343
def test_extract_matches(self):
44+
# parse single-capture segments with leading slash
45+
pp = PathPattern("/messages/{a}/{b}/{c}")
46+
self.assertEqual(
47+
pp.extract_matches("messages/match_a/match_b/match_c"),
48+
{
49+
"a": "match_a",
50+
"b": "match_b",
51+
"c": "match_c",
52+
},
53+
)
54+
55+
# parse single-capture segments without leading slash
56+
pp = PathPattern("messages/{a}/{b}/{c}")
57+
self.assertEqual(
58+
pp.extract_matches("messages/match_a/match_b/match_c"),
59+
{
60+
"a": "match_a",
61+
"b": "match_b",
62+
"c": "match_c",
63+
},
64+
)
65+
4466
# parse without multi-capture segments
4567
pp = PathPattern("{a}/something/else/{b}/end/{c}")
4668
self.assertEqual(

0 commit comments

Comments
 (0)