Skip to content
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

Error getting field when field name contains Unicode character #1000

Open
CorieW opened this issue Dec 31, 2024 · 1 comment
Open

Error getting field when field name contains Unicode character #1000

CorieW opened this issue Dec 31, 2024 · 1 comment
Assignees
Labels
api: firestore Issues related to the googleapis/python-firestore API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@CorieW
Copy link

CorieW commented Dec 31, 2024

Details

An issue was created for this in firebase-functions-python #187, but I believe this is an issue with this project, so I'm creating an issue here.

I recently reported a similar issue, but with document id #999

Environment details

  • OS type and version: Windows 10
  • google-cloud-firestore version: 2.19.0

Steps to reproduce

  1. Create a new firebase project using firebase init
    a. Use Firestore and Functions when prompted
    b. When prompted for language, select Python
  2. Add the code below to main.py in /functions
  3. Use firebase emulators with firebase emulators:start

Code example

from firebase_functions import firestore_fn, https_fn, logger
from firebase_admin import initialize_app, firestore
import google.cloud.firestore

app = initialize_app()

@firestore_fn.on_document_created(document="test/1")
def sentenceToImageFunc(event: firestore_fn.Event[firestore_fn.DocumentSnapshot | None]) -> None:
    logger.info("Received a new document.")
    firestore_client: google.cloud.firestore.Client = firestore.client()
    # Get the value of "Oración" if it exists.
    if event.data is None:
        return
    try:
        sentence = event.data.get("Oración")
        logger.info(f"Processing sentence: {sentence}")
    except KeyError:
        logger.error("Document is missing 'Oración' field.")
        # No "Oración" field, so do nothing.
        return

Stack trace

>  Traceback (most recent call last):
>    File "app\functions\venv\Lib\site-packages\flask\app.py", line 1511, in wsgi_app
>      response = self.full_dispatch_request()
>                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "app\functions\venv\Lib\site-packages\flask\app.py", line 919, in full_dispatch_request
>      rv = self.handle_user_exception(e)
>           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "app\functions\venv\Lib\site-packages\flask\app.py", line 917, in full_dispatch_request
>      rv = self.dispatch_request()
>           ^^^^^^^^^^^^^^^^^^^^^^^
>    File "app\functions\venv\Lib\site-packages\flask\app.py", line 902, in dispatch_request
>      return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
>             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "app\functions\venv\Lib\site-packages\functions_framework\execution_id.py", line 106, in wrapper
>      return view_function(*args, **kwargs)
>             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "app\functions\venv\Lib\site-packages\functions_framework\__init__.py", line 188, in view_func
>      function(event)
>    File "app\functions\venv\Lib\site-packages\firebase_functions\firestore_fn.py", line 451, in on_document_created_wrapped
>      return _firestore_endpoint_handler(
>             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "app\functions\venv\Lib\site-packages\firebase_functions\firestore_fn.py", line 222, in _firestore_endpoint_handler
>      _typing.cast(_C1 | _C2, func)(database_event)
>    File "app\functions\venv\Lib\site-packages\firebase_functions\core.py", line 125, in wrapper
>      return fn(*args, **kwargs)
>             ^^^^^^^^^^^^^^^^^^^
>    File "app\functions\main.py", line 16, in sentenceToImageFunc
>      sentence = event.data.get("Oración")
>                 ^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "app\functions\venv\Lib\site-packages\google\cloud\firestore_v1\base_document.py", line 496, in get
>      nested_data = field_path_module.get_nested_value(field_path, self._data)
>                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "app\functions\venv\Lib\site-packages\google\cloud\firestore_v1\field_path.py", line 227, in get_nested_value
>      field_names = parse_field_path(field_path)
>                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "app\functions\venv\Lib\site-packages\google\cloud\firestore_v1\field_path.py", line 119, in parse_field_path
>      for field_name in split_field_path(api_repr):
>                        ^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "app\functions\venv\Lib\site-packages\google\cloud\firestore_v1\field_path.py", line 82, in split_field_path
>      for element in _tokenize_field_path(path):
>    File "app\functions\venv\Lib\site-packages\google\cloud\firestore_v1\field_path.py", line 62, in _tokenize_field_path
>      raise ValueError("Path {} not consumed, residue: {}".format(path, path[pos:]))
>  ValueError: Path Oración not consumed, residue: ón
@product-auto-label product-auto-label bot added the api: firestore Issues related to the googleapis/python-firestore API. label Dec 31, 2024
@daniel-sanche daniel-sanche added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p3 Desirable enhancement or fix. May not be included in next release. labels Jan 6, 2025
@daniel-sanche
Copy link
Contributor

daniel-sanche commented Jan 6, 2025

I was able to reproduce this one. It looks like the field_path module was written to only support alphanumeric ascii characters. We should re-write it to support unicode, but we'll have to be careful not to break exisitng parsing functionality

I'm marking this as P3 though, because there is a workaround. You should be able to use event.data.to_dict["Oración"] to access and traverse the dict manually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the googleapis/python-firestore API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

3 participants