miLibris / flask-rest-jsonapi

Flask extension to build REST APIs around JSONAPI 1.0 specification.
http://flask-rest-jsonapi.readthedocs.io
MIT License
598 stars 153 forks source link

Attempting to build schema for non-existing resource #132

Closed aramwram closed 5 years ago

aramwram commented 5 years ago

Hi, guys.

First of all, congrats on the fantastic library you've made. It's super cool.

I'm submitting a report on erroneous (from my POV) behavior. Here's STR:

  1. Create a schema where at least one field has default value like this:

    state = fields.Integer(default=0)
  2. Create resource for that schema and route leading to it.

  3. Try to fetch single resource by non-existing ID:

    GET /bots/122
  4. AR:

    • 500 Internal server error
    • A snippet from log:

      AttributeError: 'id' is not a valid attribute of {'state': 0}

  5. ER:

    {
    data: null
    }

So, the problem is in that method ResourceDetail.get() after figuring out that there's no resource with provided ID still tries to built a result:

        schema = compute_schema(self.schema,
                                getattr(self, 'get_schema_kwargs', dict()),
                                qs,
                                qs.include)

        result = schema.dump(obj).data

But if there's some field with default value in schema we get a semi-valid resource object { state: 0 } without ID. Then, on trying to create a self link system tries to get 'id' field on { state: 0 } and eventually produces AttributeError.

I think it should be fixed by skipping compute_schema and schema.dump in the ResourceDetail.get() method. As soon as it's clear that resource does not exist we may simply jump to after_get.

What do you think?

akira-dev commented 5 years ago

Yes you're I will fix this in the next release. Thanks

etiennecaldo commented 5 years ago

Hi akira, Did you fix this issue? I still have it in 0.26.0 version. Thanks again for your framework we use with success in production