Coder Social home page Coder Social logo

Comments (3)

sloria avatar sloria commented on September 28, 2024 1

What are the concerns that led to the self.exclude += (self.name, ) safeguard in fields.py?

The safeguard was added to prevent infinite recursion when nesting 'self'.

from marshmallow import Schema, fields, pprint

class User:
    def __init__(self, name, employer=None):
        self.name = name
        self.employer = employer

user = User('Foo')
employer = User('Bar')
user.employer = user2

class UserSchema(Schema):
    name = fields.String()
    employer = fields.Nested('self')

schema = UserSchema(strict=True)
# Without the safeguard, this would result in infinite recursion
result = schema.dump(user2)
result.errors

Now that you've brought this up, though, this behavior is inconsistent with the way two-way nesting works. In the case of two-way nesting, the user is responsible for preventing infinite recursion by explicitly excluding the nested field.

I'm thinking that "self-nesting" should behave the same way.

class UserSchema(Schema):
    name = fields.String()
    # Explicitly exclude nested field
    employer = fields.Nested('self', exclude=('employer', ))

Also, as you point out, the safeguard is unnecessary when dealing with adjacency lists.

I think we have a good case for removing it. Thanks for your feedback on this.

from marshmallow.

stephenemslie avatar stephenemslie commented on September 28, 2024

+1

This would be helpful when serializing models that are using the adjacency list pattern. What are the concerns that led to the self.exclude += (self.name, ) safeguard in fields.py?

from marshmallow.

stephenemslie avatar stephenemslie commented on September 28, 2024

It seems to be possible to work around this issue by referencing the serializer using it's full dotted name. e.g.

class UserSerializer(Serializer):
    name = fields.String()
    email = fields.Email()
    friends = fields.Nested('foo.bar.UserSerializer', many=True)

from marshmallow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.