Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Schema instances leak included data #269

Open
coffeyk opened this issue Nov 13, 2019 · 1 comment
Open

Schema instances leak included data #269

coffeyk opened this issue Nov 13, 2019 · 1 comment

Comments

@coffeyk
Copy link

coffeyk commented Nov 13, 2019

I found an issue where reusing a Schema instance causes old data to be included in future dumps. This example shows it happening for the included section, but I expect it is also an issue for the meta section too.

from marshmallow_jsonapi import Schema, fields


class AddressSchema(Schema):
    id = fields.Str()
    name = fields.Str()

    class Meta:
        type_ = "addresses"


class UserSchema(Schema):
    id = fields.Str()
    name = fields.Str()
    address = fields.Relationship(type_="addresses", schema=AddressSchema())

    class Meta:
        type_ = "addresses"



address = dict(id="addr-uuid", name="My Address Name")
user = dict(id="user-uuid", name="User Name", address=address)

address_other = dict(id="another-addr-uuid", name="Not your address")
user_other = dict(id="another-user-uuid", name="Stranger Danger", address=address_other)

user_schema = UserSchema(include_data=('address',))
user_dump = user_schema.dumps(user).data

# This will include the address from the first dump in error
user_other_dump = user_schema.dumps(user_other).data

# This fails because `address` is still in the user_schema.included_data member dictionary
assert address["id"] not in user_other_dump
@olinger
Copy link

olinger commented Apr 7, 2020

ran into this issue as well.. here is a quick workaround - put a pre_dump in your schema to clear out any existing included data before you dump.

@pre_dump(pass_many=True)
def clear_includes(self, in_data, **kwargs):
    if self.included_data:
        self.included_data = {}
    return in_data

I use a base schema class for all my schemas that includes this pre_dump (and some other stuff) so its not so tedious, but it would be nice to see a real fix for this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants