Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions datastore/google/cloud/datastore/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ def to_legacy_urlsafe(self):
This is intended to work with the "legacy" representation of a
datastore "Key" used within Google App Engine (a so-called
"Reference"). The returned string can be used as the ``urlsafe``
argument to ``ndb.Key(urlsafe=...)``.
argument to ``ndb.Key(urlsafe=...)``. The base64 encoded values
will have padding removed.

:rtype: bytes
:returns: A bytestring containing the key encoded as URL-safe base64.
Expand All @@ -315,7 +316,7 @@ def to_legacy_urlsafe(self):
name_space=self.namespace,
)
raw_bytes = reference.SerializeToString()
return base64.urlsafe_b64encode(raw_bytes)
return base64.urlsafe_b64encode(raw_bytes).strip(b'=')

@classmethod
def from_legacy_urlsafe(cls, urlsafe):
Expand All @@ -324,7 +325,8 @@ def from_legacy_urlsafe(cls, urlsafe):
This is intended to work with the "legacy" representation of a
datastore "Key" used within Google App Engine (a so-called
"Reference"). This assumes that ``urlsafe`` was created within an App
Engine app via something like ``ndb.Key(...).urlsafe()``.
Engine app via something like ``ndb.Key(...).urlsafe()``. The base64
encoded values will have padding removed.

This comment was marked as spam.


:type urlsafe: bytes or unicode
:param urlsafe: The base64 encoded (ASCII) string corresponding to a
Expand All @@ -333,6 +335,9 @@ def from_legacy_urlsafe(cls, urlsafe):
:rtype: :class:`~google.cloud.datastore.key.Key`.
:returns: The key corresponding to ``urlsafe``.
"""
padding = b'=' * (-len(urlsafe) % 4)
urlsafe += padding

This comment was marked as spam.


urlsafe = _to_bytes(urlsafe, encoding='ascii')
raw_bytes = base64.urlsafe_b64decode(urlsafe)

Expand Down