Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dict data validated as fields.List: passes and silently drops data #189

Closed
andrewbaxter opened this issue Apr 8, 2015 · 3 comments
Closed

Comments

@andrewbaxter
Copy link

Here's the code I tested with:

import collections

from marshmallow import Schema, fields, pprint

class ListInnerSchema(Schema):
    y = fields.Int()

class TestSchema(Schema):
    x = fields.List(fields.Nested(ListInnerSchema()))

schema = TestSchema()
begin = {'x': {'a': 'b'}}
end = schema.dump(begin)
print('begin: {}'.format(begin))
print('end: {}'.format(end))

Here's the output:

begin: {'x': {'a': 'b'}}
end: MarshalResult(data={u'x': [{u'y': 0}]}, errors={})

I didn't intend to dump a dict, but I had some bad data and it passed with no errors.

@andrewbaxter
Copy link
Author

I think this is related to #45 - will comment there.

@andrewbaxter
Copy link
Author

As per discussion in #45 I tried it with an explicit validate on dump.

With a list of nested:
from marshmallow import Schema, fields, pprint

class ListInnerSchema(Schema):
    y = fields.Int()

class TestSchema(Schema):
    x = fields.List(fields.Nested(ListInnerSchema), required=True)

schema = TestSchema()
begin = {'x': {'y1': 14}}
print('errors: {}'.format(schema.validate(begin)))
end = schema.dump(begin)
print('begin: {}'.format(begin))
print('end: {}'.format(end))

Output:

errors: {}
begin: {'x': {'y1': 14}}
end: MarshalResult(data={u'x': [{u'y': 0}]}, errors={})
With nested many=True:
from marshmallow import Schema, fields, pprint

class ListInnerSchema(Schema):
    y = fields.Int()

class TestSchema(Schema):
    x = fields.Nested(ListInnerSchema, many=True)

schema = TestSchema()
begin = {'x': {'y1': 14}}
print('errors: {}'.format(schema.validate(begin)))
end = schema.dump(begin)
print('begin: {}'.format(begin))
print('end: {}'.format(end))

Output:

Traceback (most recent call last):
  File "a.py", line 11, in <module>
    print('errors: {}'.format(schema.validate(begin)))
  File "/.../marshmallow/schema.py", line 529, in validate
    _, errors = self._do_load(data, many, postprocess=False)
  File "/.../marshmallow/schema.py", line 606, in _do_load
    dict_class=self.dict_class
  File "/.../marshmallow/fields.py", line 295, in deserialize
    strict=strict
  File "/.../marshmallow/fields.py", line 107, in _call_and_store
    value = getter_func(data)
  File "/.../marshmallow/fields.py", line 468, in deserialize
    return self._call_and_reraise(do_deserialization, UnmarshallingError)
  File "/.../marshmallow/fields.py", line 418, in _call_and_reraise
    return func()
  File "/.../marshmallow/fields.py", line 465, in do_deserialization
    output = self._deserialize(value)
  File "/.../marshmallow/fields.py", line 604, in _deserialize
    data, errors = self.schema.load(value)
  File "/.../marshmallow/schema.py", line 500, in load
    result, errors = self._do_load(data, many, postprocess=True)
  File "/.../marshmallow/schema.py", line 606, in _do_load
    dict_class=self.dict_class
  File "/.../marshmallow/fields.py", line 276, in deserialize
    for d in data]
  File "/.../marshmallow/fields.py", line 285, in deserialize
    raw_value = data.get(attr_name, missing)
AttributeError: 'str' object has no attribute 'get'

I would expect an error to be returned from validate (and no exception thrown in any case).

@sloria
Copy link
Member

sloria commented Apr 17, 2015

I believe this is the same as #188 . A nested list is expected to be passed; non-list values are not being handled gracefully. Closing this for now; further discussion should continue on #188

@sloria sloria closed this as completed Apr 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants