Skip to content

fix(firestore): store non-native app: and user: state as JSON-safe values - #8232

Open
AliMuhammadAslam wants to merge 1 commit into
google:mainfrom
AliMuhammadAslam:fix/firestore-non-native-app-user-state
Open

AliMuhammadAslam wants to merge 1 commit into
google:mainfrom
AliMuhammadAslam:fix/firestore-non-native-app-user-state

Conversation

@AliMuhammadAslam

Copy link
Copy Markdown
Contributor

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:
FirestoreSessionService writes app: and user: state to Firestore natively. Values the Firestore client can't encode reached it unchanged: datetime.date, uuid.UUID, decimal.Decimal, a plain Enum, a Pydantic model. The client raised TypeError: Cannot convert to a Firestore Value inside the write transaction, so append_event failed and lost the event, and create_session failed. The same values work under session-level keys and in the other session services, which store them in JSON-safe form.

Solution:
_encode_firestore_value is the single function that both create_session and append_event use for app and user state. It now leaves values the client stores natively unchanged: None, bool, int, float, str, bytes, datetime, GeoPoint and document references. Any other value is converted with _session_util.make_json_safe_state, the same function used for the session bucket and the other backends. The result is then encoded again, so nested arrays inside a converted value, such as a list of lists in a Pydantic model, are still wrapped.

Native types keep the current behaviour, including the existing tests that check a datetime stays a datetime. Values that used to abort the transaction are now stored, and read back, as they are in the session bucket and in the database and SQLite backends: "2024-06-15", the UUID string, "1.50".

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

New tests in tests/unittests/integrations/firestore/test_firestore_session_service.py:

  • test_append_event_stores_non_native_app_and_user_state and test_create_session_stores_non_native_app_and_user_state, each parametrized over date, UUID, Decimal, Enum and a Pydantic model holding a date and a list of lists. Each test passes what the service writes for app and user state through the Firestore client's own encode_dict / decode_dict, so a value the client can't encode raises as it would in the real transaction. It then reads the result back through get_user_state, and asserts that app, user and session state all hold the same JSON-safe value.
  • test_create_session_keeps_firestore_native_app_and_user_state checks that GeoPoint, bytes and a list of datetime are written unchanged.

Before the fix, the 10 parametrized cases fail with TypeError: ('Cannot convert to a Firestore Value', ...). After it:

tests/unittests/integrations/firestore: 105 passed
tests/unittests/sessions + tests/unittests/integrations/firestore: 1246 passed, 26 xfailed

isort, pyink and mypy pass on the changed files.

Manual End-to-End (E2E) Tests:

I don't have a live Firestore database. Instead I ran the reproduction script from the issue, which mocks the client but encodes each transaction.set() with the client's real pbs_for_set_no_merge:

# main
session state with a date: stored
user: state with a date: append_event failed: TypeError('Cannot convert to a Firestore Value', datetime.date(2026, 11, 18), 'Invalid type', <class 'datetime.date'>)
events: 0

# this branch
session state with a date: stored
user: state with a date: stored, events: 1

I also checked that SqliteSessionService and DatabaseSessionService store the same app: and user: values and read them back as '2024-06-15', '00000000-0000-0000-0000-000000000001' and '1.50'. Those are the values the new tests expect from Firestore.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end. (Against Firestore's real encoding, not a live database; see above.)
  • Any dependent changes have been merged and published in downstream modules.

Additional context

A value that was stored natively before this change, such as a datetime, bytes or a nested list, is written exactly as it was. Nothing already in Firestore needs migrating. Vector isn't treated as native because its module isn't in every supported google-cloud-firestore release (>=2.11). Firestore's vector fields are for vector search, not for session state.

…lues

FirestoreSessionService writes app: and user: state to Firestore natively,
and passed values the Firestore client cannot encode, such as a date, UUID,
Decimal, plain Enum or Pydantic model, through unchanged. The client raised
TypeError inside the write transaction, so append_event lost the event and
create_session failed, although the same values are stored under
session-level keys and by the other session services.

_encode_firestore_value now keeps the values Firestore stores natively and
coerces any other value with _session_util.make_json_safe_state, the same
form the session bucket and the other backends store.

Fixes google#8231
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FirestoreSessionService loses the event when app: or user: state holds a date, UUID, Decimal or other value Firestore can't store

2 participants