Skip to content

Issue with Firestore Trigger on_document_created_with_auth_context #207

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

Closed
antoniobocale opened this issue Jun 24, 2024 · 2 comments · Fixed by #212
Closed

Issue with Firestore Trigger on_document_created_with_auth_context #207

antoniobocale opened this issue Jun 24, 2024 · 2 comments · Fixed by #212
Assignees
Labels
bug Something isn't working

Comments

@antoniobocale
Copy link

antoniobocale commented Jun 24, 2024

I am trying to use the Firestore trigger on_document_created_with_auth_context to get user info along with the document. However, the event data I receive is of type DocumentEventData instead of DocumentSnapshot. As a result, I cannot access the document fields using the get method. Instead, I receive the following error:

AttributeError: Unknown field for DocumentEventData: get

Steps to Reproduce

  1. Set up a Firestore trigger using on_document_created_with_auth_context.
  2. Attempt to access document fields using the get method on the event data.
  3. Observe the error mentioned above.
from firebase_functions.firestore_fn import AuthEvent, DocumentSnapshot, on_document_created_with_auth_context

@on_document_created_with_auth_context(document="*")
def document_created(event: AuthEvent[DocumentSnapshot]):
    event.data.get("field")

Expected Behavior

The event data should be of type DocumentSnapshot, allowing access to document fields using the get method, similar to the behavior of the on_document_created trigger.

Actual Behavior

The event data is of type DocumentEventData, which does not support the get method, resulting in an AttributeError.

Additional Information

When using the on_document_created trigger, the event data is of type DocumentSnapshot, and I can access document fields without any issues. However, with on_document_created_with_auth_context, the event data type is DocumentEventData, leading to the error.

Is this the intended behavior, or should the event data be of type DocumentSnapshot similar to on_document_created? If not, how should this be resolved?

@andreas-thomik
Copy link

andreas-thomik commented Jul 1, 2024

I ran into a similar issue trying to access .before and .after on change triggers.

I think the root cause is in the _firestore_endpoint_handler function, specifically the following statements:

    if event_type == _event_type_deleted:
        firestore_event_data = _typing.cast(_firestore.DocumentEventData,
                                            old_value_snapshot)
    if event_type == _event_type_created:
        firestore_event_data = _typing.cast(_firestore.DocumentEventData,
                                            value_snapshot)
    if event_type in (_event_type_written, _event_type_updated):
        firestore_event_data = _typing.cast(
            _firestore.DocumentEventData,
            Change(
                before=old_value_snapshot,
                after=value_snapshot,
            ))

They ignore the fact that _event_type can also be any of the above with the _with_auth_context suffix.

As a workaround for local development, adding the missing event types to the if event_type ... statements works but obviously does not work when actually trying to deploy a function. That would require a new release.

@exaby73 exaby73 added the bug Something isn't working label Jul 1, 2024
@exaby73
Copy link
Contributor

exaby73 commented Jul 1, 2024

Thanks for bringing this to my attention. Investigating it now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants