Skip to content
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

Docs: requesting custom field examples using marshmallow Field/Method #94

Closed
brettdh opened this issue Mar 7, 2016 · 2 comments

Comments

@brettdh
Copy link
Contributor

commented Mar 7, 2016

Added this test case to test_flaskparser.py:

import marshmallow

class Schema(marshmallow.Schema):
    foo = fields.Int()
    cube = fields.Method('get_cube')

    def get_cube(self, obj):
        return obj.foo ** 3

def test_method_field(testapp):
    with testapp.test_request_context('/myendpoint?foo=3', method='get'):
        args = parser.parse(Schema(), locations=('query', ))
        assert args == {'foo': 3, 'cube': 27}

Result (cleaned up a bit):

======= FAILURES =======
___ test_method_field ___

testapp = <Flask 'tests.test_flaskparser'>

    def test_method_field(testapp):
        with testapp.test_request_context('/myendpoint?foo=3', method='get'):
            args = parser.parse(Schema(), locations=('query', ))
>           assert args == {'foo': 3, 'cube': 27}
E           assert {'foo': 3} == {'cube': 27, 'foo': 3}
E             Omitting 1 identical items, use -v to show
E             Right contains more items:
E             {u'cube': 27}
E             Use -v to get the full diff

tests/test_flaskparser.py:120: AssertionError
=== 1 failed, 224 passed, 23 skipped, 3 pytest-warnings in 1.43 seconds ===

Same failure on Python 2.6, 2.7 (didn't test Python 3).

@brettdh

This comment has been minimized.

Copy link
Contributor Author

commented Mar 7, 2016

Whoops. See marshmallow-code/marshmallow#409

I would like to request that examples be added to the webargs docs using Function and Method fields, as the context shift from the deserialization-focused world of webargs and the serialization-focused custom field examples led me off in the wrong direction. I will updated the issue title to reflect the change, but I think the original example is useful to illustrate my misunderstanding.

@brettdh brettdh changed the title Method field not present in parsed args Docs: requesting custom field examples using marshmallow Field/Method Mar 7, 2016

@sloria sloria closed this in f48a81b Apr 4, 2016

@sloria

This comment has been minimized.

Copy link
Member

commented Apr 4, 2016

I've added a section in the docs that exemplifies the deserialize argument passed to Method and Function.

Feel free to send a docs PR if you think the docs can be clarified further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.