From fdcd26a5c34b677f5c52a334cd72760c489fd42e Mon Sep 17 00:00:00 2001 From: yihaowang Date: Sun, 1 Dec 2024 11:36:23 +0800 Subject: [PATCH] fix: enable filtering on UserProperty fields Fix UserProperty query functionality by properly handling meanings at the entity's top level. Previously, UserProperty fields could not be used in queries because the _MEANING_PREDEFINED_ENTITY_USER was not being set correctly in the outer layer of the property. --- google/cloud/ndb/_datastore_query.py | 7 +++++++ google/cloud/ndb/model.py | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/google/cloud/ndb/_datastore_query.py b/google/cloud/ndb/_datastore_query.py index 7dd98a4c..c2980a20 100644 --- a/google/cloud/ndb/_datastore_query.py +++ b/google/cloud/ndb/_datastore_query.py @@ -81,6 +81,13 @@ def make_filter(name, op, value): op=FILTER_OPERATORS[op], ) helpers._set_protobuf_value(filter_pb.value._pb, value) + + # Handle UserProperty meanings which are stored at the entity's top level, + # not within individual property values. This ensures proper meaning propagation + # when filtering on UserProperty fields. + if hasattr(value, "_meanings"): + helpers._set_pb_meaning_from_entity(value, name, value, filter_pb.value._pb) + return filter_pb diff --git a/google/cloud/ndb/model.py b/google/cloud/ndb/model.py index acfd10a8..fe0afa47 100644 --- a/google/cloud/ndb/model.py +++ b/google/cloud/ndb/model.py @@ -3471,6 +3471,17 @@ def __init__( if auto_current_user_add is not None: raise exceptions.NoLongerImplementedError() + def _comparison(self, op, value): + query = super(UserProperty, self)._comparison(op, value) + # Assign _MEANING_PREDEFINED_ENTITY_USER to the property's meaning dictionary + # This special meaning designation is crucial for enabling filter queries on userProperty + # Without this meaning, the datastore would not properly recognize user-related filters + # in queries. This ensures the outer layer of userProperty is correctly marked as a + # predefined user entity type for query operations + query._value._meanings = {self._name: (_MEANING_PREDEFINED_ENTITY_USER, query._value)} + + return query + def _validate(self, value): """Validate a ``value`` before setting it.