New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How can I pass the context in a nested field of a structured dict? #820

Closed
RosanneZe opened this Issue May 18, 2018 · 7 comments

Comments

Projects
None yet
4 participants
@RosanneZe

RosanneZe commented May 18, 2018

I noticed that if you use a nested field for values in a structured Dict, the context is not automatically given to the nested schema. Is there a way to pass it the context?

Example:

class Inner(Schema):
    foo = fields.String()

    @validates('foo')
    def validate_foo(self, value):
        if 'foo_context' not in self.context:
            raise ValidationError('no context!')


class Outer(Schema):
    bar = fields.Dict(values=fields.Nested(Inner))

# gives no error:
Inner(context={'foo_context': 'foo'}).load({'foo': 'some foo'})
# gives 'no context!' error:
Outer(context={'foo_context': 'foo'}).load({'bar': { 'key': {'foo': 'some foo'}}})

@lafrech lafrech added the bug label May 18, 2018

@lafrech

This comment has been minimized.

Member

lafrech commented May 18, 2018

I'm not familiar with the "context" feature, but from a quick glance, this looks like a bug.

@sloria

This comment has been minimized.

Member

sloria commented May 18, 2018

Yeah, AFAIK, Dict does not pass down context to it's inner fields, like Nested does.

@sloria sloria added the help wanted label May 18, 2018

@deckar01

This comment has been minimized.

Member

deckar01 commented May 18, 2018

Does List pass the context correctly?

@lafrech

This comment has been minimized.

Member

lafrech commented May 18, 2018

I'm afraid it does not.

@sloria

This comment has been minimized.

Member

sloria commented May 19, 2018

It looks like List works as expected. The following test passes on 2.x-line:

    def test_nested_list_fields_inherit_context(self):
        class InnerSchema(Schema):
            foo = fields.Field()

            @validates('foo')
            def validate_foo(self, value):
                if 'foo_context' not in self.context:
                    raise ValidationError('Missing context')

        class OuterSchema(Schema):
            bars = fields.List(fields.Nested(InnerSchema()))

        inner = InnerSchema(strict=True)
        inner.context['foo_context'] = 'foo'
        assert inner.load({'foo': 42})

        outer = OuterSchema(strict=True)
        outer.context['foo_context'] = 'foo'
        assert outer.load({'bars': [{'foo': 42}]})

And I verified that the test fails if outer.context['foo_context'] = 'foo' is removed.

sloria added a commit that referenced this issue May 19, 2018

@deckar01

This comment has been minimized.

Member

deckar01 commented May 20, 2018

Dict needs to copy the _add_to_schema logic from List to link the "container" to the parent.

@RosanneZe

This comment has been minimized.

RosanneZe commented May 22, 2018

Thanks for the quick fix everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment