Comments (3)
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.
+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.
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)
- fields.URL validation error should specify "not a valid FQDN" if trying to pass hostname without domain
- missing `f` for f-Strings HOT 2
- `fields.Url` does not accept `file` URLs
- when a new request, schema context not reset !!! HOT 2
- Questions about Copilot + Open Source Software Hierarchy HOT 3
- Allow None for a allow_none=True Date field HOT 6
- fields.Nested Type Warning Since Version 3.15.0
- Setting many=True through Meta - SchemaOpts HOT 1
- RFE: use `zoneinfo` instead of `pytz` HOT 3
- 3.21.2: pytest fails in units checking time HOT 1
- 3.21.2: sphinx warnings `reference target not found` HOT 2
- Static typing of Field subclass kwargs
- Help understanding how to set Schema meta options when using Schema.from_dict() HOT 1
- PyRight: Static TypeChecking of `Schema: ClassVar[Type[Schema]] = Schema` fails HOT 1
- Send a post request containing nested data, .List(fields.Nested(Schema)), Flask restapi - marshmallow schema HOT 4
- OSS Fuzz Integration
- Dynamically set data_key in nested field?
- Error when passing custom parameter in Nested schema HOT 2
- Documentation for _serialize/_deserialize shows parent class docstring HOT 1
- Support sqlalchemy 2+ HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from marshmallow.