Skip to content

Commit af160d6

Browse files
committed
fix(sessions): return no sessions for an empty user id without querying
The Firestore backend rejects a document path with an empty segment (400 InvalidArgument, checked against the emulator), so list_sessions with user_id="" raised instead of returning an empty list, and create_session cannot store a session for that user either.
1 parent e44692c commit af160d6

2 files changed

Lines changed: 6 additions & 12 deletions

File tree

‎src/google/adk/integrations/firestore/firestore_session_service.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ async def list_sessions(
368368
self, *, app_name: str, user_id: Optional[str] = None
369369
) -> ListSessionsResponse:
370370
"""Lists sessions from Firestore."""
371+
if user_id == "":
372+
# Firestore rejects a document path with an empty segment, so no session
373+
# can be stored for this user, and querying for one would fail.
374+
return ListSessionsResponse()
371375
if user_id is not None:
372376
query = self._get_sessions_ref(app_name, user_id).where(
373377
filter=FieldFilter("appName", "==", app_name)

‎tests/unittests/integrations/firestore/test_firestore_session_service.py‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -962,20 +962,11 @@ async def mock_get_all(refs):
962962

963963

964964
@pytest.mark.asyncio
965-
async def test_list_sessions_with_empty_user_id_lists_only_that_user(
965+
async def test_list_sessions_with_empty_user_id_returns_no_sessions(
966966
mock_firestore_client,
967967
):
968+
"""user_id="" must not fall through to the all-users query."""
968969
service = FirestoreSessionService(client=mock_firestore_client)
969-
root_coll = mock_firestore_client.collection.return_value
970-
users_coll = root_coll.document.return_value.collection.return_value
971-
user_doc_ref = users_coll.document.return_value
972-
user_doc_ref.collection.return_value.where.return_value.get = mock.AsyncMock(
973-
return_value=[]
974-
)
975-
user_doc_ref.get = mock.AsyncMock(return_value=_stored_snapshot(None))
976-
root_coll.document.return_value.get = mock.AsyncMock(
977-
return_value=_stored_snapshot(None)
978-
)
979970
mock_firestore_client.collection_group.return_value.where.return_value.get = (
980971
mock.AsyncMock(return_value=[])
981972
)
@@ -984,7 +975,6 @@ async def test_list_sessions_with_empty_user_id_lists_only_that_user(
984975

985976
assert response.sessions == []
986977
mock_firestore_client.collection_group.assert_not_called()
987-
users_coll.document.assert_any_call("")
988978

989979

990980
@pytest.mark.asyncio

0 commit comments

Comments
 (0)