marshmallow icon indicating copy to clipboard operation
marshmallow copied to clipboard

Fix load parameter *unknow* propagation

Open lmignon opened this issue 6 years ago • 3 comments

When deserializing a data structure with the load method, the unknown was not propagated to the loading of nested data structures. As result, if a unknown field was present into a nested data structure a ValidationError was raised even if the load methd was called with unknown=EXCLUDE. This commit ensures that this parameter is now propagated also to the loading of nested data structures. fixes #1428

lmignon avatar Oct 18 '19 07:10 lmignon

@lafrech @sloria What do you thing about this proposed fix? I tried to provide the required documentation to make this behavior explicit.

lmignon avatar Nov 02 '19 12:11 lmignon

Nested.load() calls Schema.load() internally, so when an unknown argument is supplied to the Nested constructor, this PR causes all of the nested unknown options to be recursively overridden.

from marshmallow import fields, Schema, RAISE, INCLUDE, EXCLUDE


class C(Schema):
    foo = fields.Str()
    class Meta:
        unknown = EXCLUDE

class B(Schema):
    c = fields.Nested(C)

class A(Schema):
    b = fields.Nested(B, unknown=INCLUDE)
    class Meta:
        unknown = RAISE

schema = A()
print(schema.load({
    'b': {
        'extra': 'good',
        'c': {
            'foo': 'bar',
            'unexpected': 'bad'
        }
    }
}))

Current behavior (3.2.2):

{'b': {'c': {'foo': 'bar'}, 'extra': 'good'}}

Proposed behavior:

{'b': {'c': {'foo': 'bar', 'unexpected': 'bad'}, 'extra': 'good'}}

deckar01 avatar Nov 26 '19 21:11 deckar01

Any chance of this merging?

bhperry avatar Aug 18 '25 18:08 bhperry