From 237924bad1531b45d93d138191757af46336ae6a Mon Sep 17 00:00:00 2001 From: russellwheatley Date: Mon, 19 Jun 2023 13:45:35 +0100 Subject: [PATCH 1/3] fix: normalize firestore document path --- src/firebase_functions/firestore_fn.py | 11 ++++++----- src/firebase_functions/private/util.py | 6 ++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/firebase_functions/firestore_fn.py b/src/firebase_functions/firestore_fn.py index b8ae498..c442932 100644 --- a/src/firebase_functions/firestore_fn.py +++ b/src/firebase_functions/firestore_fn.py @@ -201,8 +201,9 @@ def example(event: Event[Change[DocumentSnapshot]]) -> None: options = FirestoreOptions(**kwargs) def on_document_written_inner_decorator(func: _C1): - document_pattern = _path_pattern.PathPattern(options.document) - + print("WWWWWWWWWW: ", options.document) + document_pattern = _path_pattern.PathPattern(_util.normalize_path(options.document)) + print("WWWWWWWWWW 1111111: ", _util.normalize_path(options.document)) @_functools.wraps(func) def on_document_written_wrapped(raw: _ce.CloudEvent): return _firestore_endpoint_handler( @@ -248,7 +249,7 @@ def example(event: Event[Change[DocumentSnapshot]]) -> None: options = FirestoreOptions(**kwargs) def on_document_updated_inner_decorator(func: _C1): - document_pattern = _path_pattern.PathPattern(options.document) + document_pattern = _path_pattern.PathPattern(_util.normalize_path(options.document)) @_functools.wraps(func) def on_document_updated_wrapped(raw: _ce.CloudEvent): @@ -295,7 +296,7 @@ def example(event: Event[DocumentSnapshot]): options = FirestoreOptions(**kwargs) def on_document_created_inner_decorator(func: _C2): - document_pattern = _path_pattern.PathPattern(options.document) + document_pattern = _path_pattern.PathPattern(_util.normalize_path(options.document)) @_functools.wraps(func) def on_document_created_wrapped(raw: _ce.CloudEvent): @@ -342,7 +343,7 @@ def example(event: Event[DocumentSnapshot]) -> None: options = FirestoreOptions(**kwargs) def on_document_deleted_inner_decorator(func: _C2): - document_pattern = _path_pattern.PathPattern(options.document) + document_pattern = _path_pattern.PathPattern(_util.normalize_path(options.document)) @_functools.wraps(func) def on_document_deleted_wrapped(raw: _ce.CloudEvent): diff --git a/src/firebase_functions/private/util.py b/src/firebase_functions/private/util.py index 49bf264..ccbd6f8 100644 --- a/src/firebase_functions/private/util.py +++ b/src/firebase_functions/private/util.py @@ -308,3 +308,9 @@ def firebase_config() -> None | FirebaseConfig: f'FIREBASE_CONFIG JSON string "{json_str}" is not valid json. {err}' ) from err return FirebaseConfig(storage_bucket=json_data.get("storageBucket")) + +def normalize_path(path: str) -> str: + """ + Normalize a path string to a consistent format. + """ + return path.strip("/") \ No newline at end of file From ea2f7203f7aaf6bd8e9a39ed35e96899ec884550 Mon Sep 17 00:00:00 2001 From: russellwheatley Date: Mon, 19 Jun 2023 13:51:19 +0100 Subject: [PATCH 2/3] chore: remove print logs --- src/firebase_functions/firestore_fn.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/firebase_functions/firestore_fn.py b/src/firebase_functions/firestore_fn.py index c442932..b0f68e5 100644 --- a/src/firebase_functions/firestore_fn.py +++ b/src/firebase_functions/firestore_fn.py @@ -201,9 +201,8 @@ def example(event: Event[Change[DocumentSnapshot]]) -> None: options = FirestoreOptions(**kwargs) def on_document_written_inner_decorator(func: _C1): - print("WWWWWWWWWW: ", options.document) document_pattern = _path_pattern.PathPattern(_util.normalize_path(options.document)) - print("WWWWWWWWWW 1111111: ", _util.normalize_path(options.document)) + @_functools.wraps(func) def on_document_written_wrapped(raw: _ce.CloudEvent): return _firestore_endpoint_handler( From a88e68e04ddfe59aadc01e53806509dba04e13ae Mon Sep 17 00:00:00 2001 From: russellwheatley Date: Mon, 19 Jun 2023 14:02:03 +0100 Subject: [PATCH 3/3] format --- src/firebase_functions/firestore_fn.py | 12 ++++++++---- src/firebase_functions/private/util.py | 3 ++- tests/test_util.py | 20 +++++++++++++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/firebase_functions/firestore_fn.py b/src/firebase_functions/firestore_fn.py index b0f68e5..122a82c 100644 --- a/src/firebase_functions/firestore_fn.py +++ b/src/firebase_functions/firestore_fn.py @@ -201,7 +201,8 @@ def example(event: Event[Change[DocumentSnapshot]]) -> None: options = FirestoreOptions(**kwargs) def on_document_written_inner_decorator(func: _C1): - document_pattern = _path_pattern.PathPattern(_util.normalize_path(options.document)) + document_pattern = _path_pattern.PathPattern( + _util.normalize_path(options.document)) @_functools.wraps(func) def on_document_written_wrapped(raw: _ce.CloudEvent): @@ -248,7 +249,8 @@ def example(event: Event[Change[DocumentSnapshot]]) -> None: options = FirestoreOptions(**kwargs) def on_document_updated_inner_decorator(func: _C1): - document_pattern = _path_pattern.PathPattern(_util.normalize_path(options.document)) + document_pattern = _path_pattern.PathPattern( + _util.normalize_path(options.document)) @_functools.wraps(func) def on_document_updated_wrapped(raw: _ce.CloudEvent): @@ -295,7 +297,8 @@ def example(event: Event[DocumentSnapshot]): options = FirestoreOptions(**kwargs) def on_document_created_inner_decorator(func: _C2): - document_pattern = _path_pattern.PathPattern(_util.normalize_path(options.document)) + document_pattern = _path_pattern.PathPattern( + _util.normalize_path(options.document)) @_functools.wraps(func) def on_document_created_wrapped(raw: _ce.CloudEvent): @@ -342,7 +345,8 @@ def example(event: Event[DocumentSnapshot]) -> None: options = FirestoreOptions(**kwargs) def on_document_deleted_inner_decorator(func: _C2): - document_pattern = _path_pattern.PathPattern(_util.normalize_path(options.document)) + document_pattern = _path_pattern.PathPattern( + _util.normalize_path(options.document)) @_functools.wraps(func) def on_document_deleted_wrapped(raw: _ce.CloudEvent): diff --git a/src/firebase_functions/private/util.py b/src/firebase_functions/private/util.py index ccbd6f8..5838fcf 100644 --- a/src/firebase_functions/private/util.py +++ b/src/firebase_functions/private/util.py @@ -309,8 +309,9 @@ def firebase_config() -> None | FirebaseConfig: ) from err return FirebaseConfig(storage_bucket=json_data.get("storageBucket")) + def normalize_path(path: str) -> str: """ Normalize a path string to a consistent format. """ - return path.strip("/") \ No newline at end of file + return path.strip("/") diff --git a/tests/test_util.py b/tests/test_util.py index e13fe1c..8a40f99 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -15,7 +15,7 @@ Internal utils tests. """ from os import environ, path -from firebase_functions.private.util import firebase_config +from firebase_functions.private.util import firebase_config, normalize_path test_bucket = "python-functions-testing.appspot.com" test_config_file = path.join(path.dirname(path.realpath(__file__)), @@ -40,3 +40,21 @@ def test_firebase_config_loads_from_env_file(): environ["FIREBASE_CONFIG"] = test_config_file assert firebase_config().storage_bucket == test_bucket, ( "Failure, firebase_config did not load from env variable.") + + +def test_normalize_document_path(): + """ + Testing "document" path passed to Firestore event listener + is normalized. + """ + test_path = "/test/document/" + assert normalize_path(test_path) == "test/document", ( + "Failure, path was not normalized.") + + test_path1 = "//////test/document//////////" + assert normalize_path(test_path1) == "test/document", ( + "Failure, path was not normalized.") + + test_path2 = "test/document" + assert normalize_path(test_path2) == "test/document", ( + "Failure, path should not be changed if it is already normalized.")