pombreda / appengine-ndb-experiment

Automatically exported from code.google.com/p/appengine-ndb-experiment
Other
0 stars 0 forks source link

ndb.Model constructor does not handle projections correctly #227

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
class Bar(model.Model):
  a = model.StringProperty()
  b = model.StringProperty()

class Foo(model.Model):
  bar = model.StructuredProperty(Bar)

The ndb.Model initializer assigns directly to _projection rather than going 
through _set_projection, which propagates the projection into structured 
properties:

foo = Foo(bar=Bar(a='a', b='b'), projection=(('bar.a',))
foo.bar.b  # succeeds even though not projected

When using ndb.Model._set_projection, on the other hand, the method ignores the 
possibility of Nones in the list returned by _get_base_value_unwrapped_as_list 
(either for repeated properties with Nones in them, or for unset unrepeated 
properties):

foo = Foo()
foo._set_projection(('a.b',))  # raises AttributeError

Proposed fix:  replace last line of ndb.Model initializer with 
self._set_projection(projection), and add an if item is not None check inside 
the _set_projection loop.  Please let me know if this sounds reasonable and 
I'll submit a patch.  Thanks.

Original issue reported on code.google.com by pi...@knowlabs.com on 4 Dec 2012 at 2:58

GoogleCodeExporter commented 9 years ago
It does sound reasonable. The patch seems simple; can you also submit some unit 
tests to demonstrate the current failure?

Original comment by guido@google.com on 4 Dec 2012 at 11:00

GoogleCodeExporter commented 9 years ago
Can you also sign the contributor form linked from 
http://code.google.com/p/appengine-ndb-experiment/wiki/Contributing please?

Original comment by guido@google.com on 4 Dec 2012 at 11:02

GoogleCodeExporter commented 9 years ago

Original comment by arful...@google.com on 16 Mar 2013 at 6:56

GoogleCodeExporter commented 9 years ago
FYI Foo(bar=None, projection=(('bar.a')))  is not valid because it is imposible 
to get an entity back with out a value for bar.a from a query with a projection 
on bar.a

Original comment by arful...@google.com on 16 Mar 2013 at 7:10

GoogleCodeExporter commented 9 years ago
Hmm, why would anyone pass in projection to the constructor?

Original comment by arful...@google.com on 16 Mar 2013 at 7:23

GoogleCodeExporter commented 9 years ago
IIRC, it was useful to simulate projected entities in unit tests.

Original comment by pi...@knowlabs.com on 16 Mar 2013 at 7:27

GoogleCodeExporter commented 9 years ago

Original comment by arful...@google.com on 8 Apr 2013 at 11:41

GoogleCodeExporter commented 9 years ago

Original comment by arful...@google.com on 12 Jun 2013 at 11:44