question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lafrechcommented, 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')
0reactions
tuukkamustonencommented, Jan 30, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

marshmallow - Read the Docs
Note: If you pass in a string field name to only, only a single value (or flat list of values if many=True) will...
Read more >
Source code for marshmallow_jsonapi.fields
Schema ` to determine which fields should be formatted as relationship objects. See: http://jsonapi.org/format/#document-resource-object-relationships """ ...
Read more >
marshmallow - Bountysource
In order to iterate over a schema's fields in the order in which it was defined, you have to add a class Meta:...
Read more >
Inconsistent Behavior for IMPORT with RENAME SCHEMA ...
Hello all, I'm looking into methods to help automate initialization of schemas with a base catalog objects in HANA Cloud and the strategy ......
Read more >
OASIS Specification Template
These applications know which methods to call or fields to access on the Data Objects they ... The end user sends a request...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found