Skip to content

Commit 0ae6070

Browse files
fix: normalize firestore document path (#106)
1 parent 73f3b6f commit 0ae6070

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/firebase_functions/firestore_fn.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ def example(event: Event[Change[DocumentSnapshot]]) -> None:
201201
options = FirestoreOptions(**kwargs)
202202

203203
def on_document_written_inner_decorator(func: _C1):
204-
document_pattern = _path_pattern.PathPattern(options.document)
204+
document_pattern = _path_pattern.PathPattern(
205+
_util.normalize_path(options.document))
205206

206207
@_functools.wraps(func)
207208
def on_document_written_wrapped(raw: _ce.CloudEvent):
@@ -248,7 +249,8 @@ def example(event: Event[Change[DocumentSnapshot]]) -> None:
248249
options = FirestoreOptions(**kwargs)
249250

250251
def on_document_updated_inner_decorator(func: _C1):
251-
document_pattern = _path_pattern.PathPattern(options.document)
252+
document_pattern = _path_pattern.PathPattern(
253+
_util.normalize_path(options.document))
252254

253255
@_functools.wraps(func)
254256
def on_document_updated_wrapped(raw: _ce.CloudEvent):
@@ -295,7 +297,8 @@ def example(event: Event[DocumentSnapshot]):
295297
options = FirestoreOptions(**kwargs)
296298

297299
def on_document_created_inner_decorator(func: _C2):
298-
document_pattern = _path_pattern.PathPattern(options.document)
300+
document_pattern = _path_pattern.PathPattern(
301+
_util.normalize_path(options.document))
299302

300303
@_functools.wraps(func)
301304
def on_document_created_wrapped(raw: _ce.CloudEvent):
@@ -342,7 +345,8 @@ def example(event: Event[DocumentSnapshot]) -> None:
342345
options = FirestoreOptions(**kwargs)
343346

344347
def on_document_deleted_inner_decorator(func: _C2):
345-
document_pattern = _path_pattern.PathPattern(options.document)
348+
document_pattern = _path_pattern.PathPattern(
349+
_util.normalize_path(options.document))
346350

347351
@_functools.wraps(func)
348352
def on_document_deleted_wrapped(raw: _ce.CloudEvent):

src/firebase_functions/private/util.py

+7
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,10 @@ def firebase_config() -> None | FirebaseConfig:
308308
f'FIREBASE_CONFIG JSON string "{json_str}" is not valid json. {err}'
309309
) from err
310310
return FirebaseConfig(storage_bucket=json_data.get("storageBucket"))
311+
312+
313+
def normalize_path(path: str) -> str:
314+
"""
315+
Normalize a path string to a consistent format.
316+
"""
317+
return path.strip("/")

tests/test_util.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Internal utils tests.
1616
"""
1717
from os import environ, path
18-
from firebase_functions.private.util import firebase_config
18+
from firebase_functions.private.util import firebase_config, normalize_path
1919

2020
test_bucket = "python-functions-testing.appspot.com"
2121
test_config_file = path.join(path.dirname(path.realpath(__file__)),
@@ -40,3 +40,21 @@ def test_firebase_config_loads_from_env_file():
4040
environ["FIREBASE_CONFIG"] = test_config_file
4141
assert firebase_config().storage_bucket == test_bucket, (
4242
"Failure, firebase_config did not load from env variable.")
43+
44+
45+
def test_normalize_document_path():
46+
"""
47+
Testing "document" path passed to Firestore event listener
48+
is normalized.
49+
"""
50+
test_path = "/test/document/"
51+
assert normalize_path(test_path) == "test/document", (
52+
"Failure, path was not normalized.")
53+
54+
test_path1 = "//////test/document//////////"
55+
assert normalize_path(test_path1) == "test/document", (
56+
"Failure, path was not normalized.")
57+
58+
test_path2 = "test/document"
59+
assert normalize_path(test_path2) == "test/document", (
60+
"Failure, path should not be changed if it is already normalized.")

0 commit comments

Comments
 (0)