Nested single string 'only' behavior edge case
See original GitHub issueWhen using Nested fields, passing a single string field to only
does not work as expected when applying a transform with on_bind_field
from marshmallow import fields, Schema
def test_nested_only_on_bind_field():
def to_camel_case(snake_str):
components = snake_str.split('_')
return components[0] + ''.join(x.title() for x in components[1:])
class CamelCaseSchema(Schema):
def on_bind_field(self, field_name, field_obj):
field_obj.data_key = to_camel_case(field_name)
class UserRoleSchema(CamelCaseSchema):
user_id = fields.Number()
role_name = fields.String()
class UserSchema(CamelCaseSchema):
id = fields.Number()
roles = fields.Nested(UserRoleSchema, many=True, only='role_name')
user = {
'id': 1,
'roles': [
{'user_id': 1, 'role_name': 'admin'},
{'user_id': 1, 'role_name': 'test'},
]
}
serialized_user = UserSchema().dumps(user)
assert serialized_user == {"id": 1, "roles": ["admin", "test"]}
This results in the following error:
/env/lib/python3.6/site-packages/marshmallow/schema.py:476: in dumps
serialized = self.dump(obj, many=many, update_fields=update_fields)
/env/lib/python3.6/site-packages/marshmallow/schema.py:428: in dump
**kwargs
/env/lib/python3.6/site-packages/marshmallow/marshalling.py:147: in serialize
index=(index if index_errors else None)
/env/lib/python3.6/site-packages/marshmallow/marshalling.py:68: in call_and_store
value = getter_func(data)
/env/lib/python3.6/site-packages/marshmallow/marshalling.py:141: in <lambda>
getter = lambda d: field_obj.serialize(attr_name, d, accessor=accessor)
/env/lib/python3.6/site-packages/marshmallow/fields.py:250: in serialize
return self._serialize(value, attr, obj)
/env/lib/python3.6/site-packages/marshmallow/fields.py:455: in _serialize
return utils.pluck(ret, key=self.only)
/env/lib/python3.6/site-packages/marshmallow/utils.py:318: in pluck
return [d[key] for d in dictlist]
KeyError: 'role_name'
This seems to be only affecting the single string case, as passing only=['role_name']
works as expected.
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Select...Case Statement - Visual Basic - Microsoft Learn
Select Case constructions can be nested. Each nested Select Case construction must have a matching End Select statement and must be completely ...
Read more >Create a type with the same nested keys as another type but ...
This works as intended for your example use case: type Transformed = PrimitivesToString<SomeType>; /* type Transformed = { prop1: string; ...
Read more >Manipulating Nested Data Just Got Easier in Apache Spark 3.1.1
Generating a DataFrame with a StructType column. To keep things simple, we've created our DataFrame with only one row containing actual data for ......
Read more >KIP-821: Connect Transforms support for nested structures
This KIP aim to include support for nested structures on the existing SMTs. Proposed Changes. Nested notation.
Read more >Nested Links - CSS-Tricks
Have both link elements as direct children of the grid, with one positioned from edge to edge, and the other only occupying a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think modulating the
Nested
functionality this much based on the type of a parameter is confusing. The feature is useful though.What about a dedicated field type to encapsulate this behavior?
I think that interface would communicate the behavior without needing to dig into the docs.
As a side note, if we go ahead with https://github.com/marshmallow-code/marshmallow/pull/810, then
__filter_fields
is gone anyway. Though I agree thatfields.Pluck
makes more sense regardless.