flask-rest-jsonapi icon indicating copy to clipboard operation
flask-rest-jsonapi copied to clipboard

get_schema_kwargs updates mutably schema_kwargs in get method

Open anuragagarwal561994 opened this issue 7 years ago • 0 comments
trafficstars

https://github.com/miLibris/flask-rest-jsonapi/blob/ac83e68dbd83a48158f611ce6bc7205043bf1800/flask_rest_jsonapi/resource.py#L128

If a user is using a dictionary and passes get_schema_kwargs, the update is mutably resulting in unexpected behaviour because the user might want to user the same dictionary somewhere else.

Example: My resource classes looks like the following:

class _BaseResource(object):
    _schema_kwargs = {'exclude': ('password',)}
    def _check_authorized():
        ...
    def get_schema_kwargs(self):
        return self._schema_kwargs if not self._check_authorized() else None

class SomeResourceDetail(ResourceDetail, _BaseResource):
    ...
class SomeResourceList(ResourceList, _BaseResource):
    ...

In this case, when the list is first fetched the _schema_kwargs will get updated to {'exclude': ('password',), 'many': True} and when the detail resource is being fetched it will report an error saying that <model of SomeResource> is not iterable.

anuragagarwal561994 avatar Nov 29 '17 09:11 anuragagarwal561994