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

Deserialization of Hyperlinks and URL fields fails #9

Closed
sloria opened this issue Dec 22, 2014 · 2 comments

Comments

@sloria
Copy link
Member

commented Dec 22, 2014

From @raj-kesavan in #7 :

Simple set up with Flask-RESTful and Flask-Marshmallow. When I navigate to /api/v1/person, I get a 500 Internal Server Error. Server logs at bottom.

class PersonSchema(ma.Schema):
    name = ma.String(required = True)
    friends = ma.Hyperlinks({
        'href': ma.AbsoluteURL('person_ep')
    })

class PersonAPI(Resource):
    def get(self):
        person, errors = PersonSchema().load({
            'name': 'Rob'
        })

api.add_resource(PersonAPI, '/api/v1/person/', endpoint = 'person_ep')

Server log

[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]   File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 521, in dispatch_request
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]     resp = meth(*args, **kwargs)
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]   File "/var/www/html/devabacus/abacus/views.py", line 95, in get
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]     'name': 'Rob'
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]   File "/usr/local/lib/python2.7/dist-packages/marshmallow/schema.py", line 498, in load
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]     dict_class=self.dict_class
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]   File "/usr/local/lib/python2.7/dist-packages/marshmallow/fields.py", line 260, in deserialize
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]     key = fields_dict[attr_name].attribute or attr_name
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69] AttributeError: 'Hyperlinks' object has no attribute 'attribute'

@sloria sloria added the bug label Dec 22, 2014

@sloria sloria closed this in d7f6256 Dec 22, 2014

@ghost

This comment has been minimized.

Copy link

commented Dec 22, 2014

Thank you, this works great.

Maybe this will be helpful for someone else:
I was having a problem because my URLs included the id attribute, but this wasn't supplied by the user when submitting a PUT request. Since PUT is idempotent, I needed to deserialize and then serialize the data (providing empty strings for optional attributes); it broke upon serializing because the generated user had no id attribute (necessary for the link). I made a new class, LinkedUserSchema which inherited from UserSchema and moved the links to the LinkedUserSchema instead - then my deserializing/serializing was done with UserSchema, which had all the data it needed to validate, then my GET request would use the LinkedUserSchema.

Thanks again.

@sloria

This comment has been minimized.

Copy link
Member Author

commented Dec 23, 2014

Thanks @raj-kesavan for the suggestion.

If marshmallow-code/marshmallow#61 gets implemented, you would be able to add the read_only flag to the link-related fields, so that they are only active for serialization (but not validation/deserialization). In the meantime, I think your solution is fine.

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