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

Dotted `only` and `exclude` not working for nested schema instances #1384

Closed
sloria opened this issue Sep 8, 2019 · 1 comment · Fixed by #1385 · May be fixed by #1382

Comments

@sloria
Copy link
Member

commented Sep 8, 2019

#1229 fixed propagation of only and exclude for nested schema classes but no instances.
Dotted strings passed to only and exclude are not respected when a schema instance is nested.

from marshmallow import Schema, fields


class Child(Schema):
    name = fields.String()
    age = fields.Integer()


class Parent(Schema):
    favorite = fields.Nested(Child, only=("name", "age"))
    children = fields.List(fields.Nested(Child, only=("name", "age")))


class Parent2(Schema):
    favorite = fields.Nested(Child(only=("name", "age")))
    children = fields.List(fields.Nested(Child(only=("name", "age"))))


family = {
    "favorite": {"name": "Lily", "age": 15},
    "children": [{"name": "Tommy", "age": 12}, {"name": "Lily", "age": 15}],
}
# should be the same
print(Parent(only=("favorite.age", "children.name")).dump(family))
print(Parent2(only=("favorite.age", "children.name")).dump(family))

Expected output:

{'children': [{'name': 'Tommy'}, {'name': 'Lily'}], 'favorite': {'age': 15}}
{'children': [{'name': 'Tommy'}, {'name': 'Lily'}], 'favorite': {'age': 15}}

Actual output:

{'favorite': {'age': 15}, 'children': [{'name': 'Tommy'}, {'name': 'Lily'}]}
{'favorite': {'age': 15, 'name': 'Lily'}, 'children': [{'age': 12, 'name': 'Tommy'}, {'age': 15, 'name': 'Lily'}]}

@sloria sloria added the bug label Sep 8, 2019

@sloria sloria self-assigned this Sep 8, 2019

@sloria

This comment has been minimized.

Copy link
Member Author

commented Sep 8, 2019

Noticed this while working on #1382 . This needs to be fixed before that gets merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.