Skip to content

fix: normalize firestore document path #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/firebase_functions/firestore_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(options.document)
document_pattern = _path_pattern.PathPattern(
_util.normalize_path(options.document))

@_functools.wraps(func)
def on_document_written_wrapped(raw: _ce.CloudEvent):
Expand Down Expand Up @@ -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(options.document)
document_pattern = _path_pattern.PathPattern(
_util.normalize_path(options.document))

@_functools.wraps(func)
def on_document_updated_wrapped(raw: _ce.CloudEvent):
Expand Down Expand Up @@ -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(options.document)
document_pattern = _path_pattern.PathPattern(
_util.normalize_path(options.document))

@_functools.wraps(func)
def on_document_created_wrapped(raw: _ce.CloudEvent):
Expand Down Expand Up @@ -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(options.document)
document_pattern = _path_pattern.PathPattern(
_util.normalize_path(options.document))

@_functools.wraps(func)
def on_document_deleted_wrapped(raw: _ce.CloudEvent):
Expand Down
7 changes: 7 additions & 0 deletions src/firebase_functions/private/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,10 @@ 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("/")
20 changes: 19 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)),
Expand All @@ -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.")