question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested single string 'only' behavior edge case

See original GitHub issue

When 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:closed
  • Created 5 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
deckar01commented, May 13, 2018

Would it make sense to remove this support entirely for v3?

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?

class UserSchema(Schema):
    id = fields.Number()
    roles = fields.Pluck(UserRoleSchema, 'role_name', many=True)

I think that interface would communicate the behavior without needing to dig into the docs.

0reactions
taioncommented, May 15, 2018

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 that fields.Pluck makes more sense regardless.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found