Skip to content

Commit f482e9c

Browse files
author
Chris Rossi
authored
fix: include ancestors in Key.to_legacy_urlsafe (#494)
Fixes #478.
1 parent 6f69471 commit f482e9c

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

packages/google-cloud-ndb/google/cloud/ndb/key.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,10 +759,8 @@ def to_legacy_urlsafe(self, location_prefix):
759759
b'aglzfmV4YW1wbGVyCwsSBEtpbmQYuQoM'
760760
"""
761761
return google.cloud.datastore.Key(
762-
self._key.kind,
763-
self._key.id or self._key.name,
764-
namespace=self._key.namespace,
765-
project=self._key.project,
762+
*self.flat(),
763+
**{"namespace": self._key.namespace, "project": self._key.project}
766764
).to_legacy_urlsafe(location_prefix=location_prefix)
767765

768766
@_options.ReadOptions.options

packages/google-cloud-ndb/tests/unit/test_key.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,18 @@ def test_to_legacy_urlsafe_name():
586586
== b"agNzfmZyCAsSAWQiAXgM"
587587
)
588588

589+
@staticmethod
590+
@pytest.mark.usefixtures("in_context")
591+
def test_to_legacy_urlsafe_w_ancestor():
592+
"""Regression test for #478.
593+
594+
https://github.com/googleapis/python-ndb/issues/478
595+
"""
596+
key = key_module.Key("d", 123, "e", 234, app="f")
597+
urlsafe = key.to_legacy_urlsafe(location_prefix="s~")
598+
key2 = key_module.Key(urlsafe=urlsafe)
599+
assert key == key2
600+
589601
@staticmethod
590602
@pytest.mark.usefixtures("in_context")
591603
@mock.patch("google.cloud.ndb._datastore_api")

0 commit comments

Comments
 (0)