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

Inconsistent behavior in passing request fields to Schema.load (many=True vs many=False) #268

Closed
tuukkamustonen opened this issue Aug 8, 2018 · 5 comments

Comments

@tuukkamustonen
Copy link
Contributor

tuukkamustonen commented Aug 8, 2018

With 4.0.0 (and marshmallow 3.0.0b13):

class Sch(Schema):
    foo = fields.Str()

parser = FlaskParser()

req = Mock()
req.mimetype = 'application/json'

req.get_json = lambda *args, **kwargs: [{'extra': 1}]
with pytest.raises(UnprocessableEntity) as e:
    parser.parse(Foo(many=True), req=req, locations=('json', ))
assert e.value.exc.messages == {0: {'extra': ['Unknown field.']}}

req.get_json = lambda *args, **kwargs: {'extra': 1}
parser.parse(Foo(), req=req, locations=('json', ))

So the with many=True the extra key is actually passed to Schema.load() (which can then raise ValidationError because unknown defaults to RAISE) but with many=False it is passed onwards. I think it's confusing.

#267 would fix this.

@lafrech
Copy link
Member

lafrech commented Jan 30, 2020

@tuukkamustonen is this fixed in webargs 6 beta?

@tuukkamustonen
Copy link
Contributor Author

@lafrech I haven't been able to follow the project lately, but as the referenced ticket #267 has been fixed and merged, I would assume so.

Feel free to close, if you think this is fixed, we can re-open if it's not...

@lafrech
Copy link
Member

lafrech commented Jan 30, 2020

Fixed.

from werkzeug.exceptions import UnprocessableEntity
from marshmallow import Schema, fields
from webargs.flaskparser import FlaskParser
from unittest.mock import Mock


class Foo(Schema):
    foo = fields.Str()

parser = FlaskParser()

req = Mock()
req.mimetype = 'application/json'

req.get_data = lambda *args, **kwargs: '[{"extra": 1}]'

# Both raise UnprocessableEntity
try:
    parser.parse(Foo(many=True), req=req, location='json')
except UnprocessableEntity:
    print('OK')
try:
    parser.parse(Foo(), req=req, location='json')
except UnprocessableEntity:
    print('OK')

@lafrech lafrech closed this as completed Jan 30, 2020
@tuukkamustonen
Copy link
Contributor Author

This indeed looks like fixed now. Thanks for checking!

@tuukkamustonen
Copy link
Contributor Author

Just a minor "correction":

The latter case, with many=False actually failed with just {"extra": 1} not [{"extra": 1}] (as in your code above).

But both those now raise error properly, so yeah it's fixed.

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