Skip to content

Commit 29f5a85

Browse files
authored
fix(model): Ensure repeated props have same kind when converting from ds (#824)
Legacy NDB had a bug where, with repeated Expando properties, it could end up writing arrays of different length if some entities had missing values for certain subproperties. When Cloud NDB read this out, it might have only loaded entities corresponding to the shorter array length. See issue #129 . To fix this, with PR #176 , we made Cloud NDB check if there are length differences and pad out the array as necessary. However, depending on the order properties are returned from Datastore in, we may have accidentally padded with subentities of the wrong kind, because it was possible to skip over updating the kind if we alternated between updating repeated properties. (Eg: A.a with 2 elements, B.b with 3 elements, A.c with 3 elements -> A would end up with an element of B's kind)
1 parent 7fd3ed3 commit 29f5a85

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

google/cloud/ndb/model.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ def _entity_from_ds_entity(ds_entity, model_class=None):
543543
Args:
544544
ds_entity (google.cloud.datastore_v1.types.Entity): An entity to be
545545
deserialized.
546+
model_class (class): Optional; ndb Model class type.
546547
547548
Returns:
548549
.Model: The deserialized entity.
@@ -623,10 +624,14 @@ def new_entity(key):
623624
# different lengths for the subproperties, which was a
624625
# bug. We work around this when reading out such values
625626
# by making sure our repeated property is the same
626-
# length as the longest suproperty.
627+
# length as the longest subproperty.
628+
# Make sure to create a key of the same kind as
629+
# the other entries in the value list
627630
while len(subvalue) > len(value):
628631
# Need to make some more subentities
629-
value.append(new_entity(key._key))
632+
expando_kind = structprop._model_class._get_kind()
633+
expando_key = key_module.Key(expando_kind, None)
634+
value.append(new_entity(expando_key._key))
630635

631636
# Branch coverage bug,
632637
# See: https://github.com/nedbat/coveragepy/issues/817

tests/unit/test_model.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5606,6 +5606,40 @@ class ThisKind(model.Model):
56065606
assert entity.baz[2].bar == "iminjail"
56075607
assert entity.copacetic is True
56085608

5609+
@staticmethod
5610+
@pytest.mark.usefixtures("in_context")
5611+
def test_legacy_repeated_structured_property_uneven_expandos():
5612+
class Expando1(model.Expando):
5613+
bar = model.StringProperty()
5614+
5615+
class Expando2(model.Expando):
5616+
qux = model.StringProperty()
5617+
5618+
class ThisKind(model.Model):
5619+
foo = model.StructuredProperty(model_class=Expando1, repeated=True)
5620+
baz = model.StructuredProperty(model_class=Expando2, repeated=True)
5621+
5622+
key = datastore.Key("ThisKind", 123, project="testing")
5623+
datastore_entity = datastore.Entity(key=key)
5624+
datastore_entity.items = mock.Mock(
5625+
return_value=(
5626+
# Order matters here
5627+
("foo.bar", ["foo_bar_1"]),
5628+
("baz.qux", ["baz_qux_1", "baz_qux_2"]),
5629+
("foo.custom_1", ["foo_c1_1", "foo_c1_2"]), # longer than foo.bar
5630+
)
5631+
)
5632+
entity = model._entity_from_ds_entity(datastore_entity)
5633+
assert isinstance(entity, ThisKind)
5634+
assert len(entity.foo) == 2
5635+
assert len(entity.baz) == 2
5636+
assert entity.foo[0].bar == "foo_bar_1"
5637+
assert entity.foo[0].custom_1 == "foo_c1_1"
5638+
assert entity.foo[1].bar is None
5639+
assert entity.foo[1].custom_1 == "foo_c1_2"
5640+
assert entity.baz[0].qux == "baz_qux_1"
5641+
assert entity.baz[1].qux == "baz_qux_2"
5642+
56095643
@staticmethod
56105644
@pytest.mark.usefixtures("in_context")
56115645
def test_legacy_repeated_structured_property_with_name():

0 commit comments

Comments
 (0)