miLibris / flask-rest-jsonapi

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

Invalid conditions in check_headers decorator #160

Open artezio-andreym opened 5 years ago

artezio-andreym commented 5 years ago

Hello,

According to the documentation every POST and PATCH methods should contain headers:

Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

otherwise the exception should be risen.

The point of the issue is that there is a check_headers decorator implemented in flask_rest_jsonapi.decorators:check_headers which never returns errors even if the request doesn't have correct header because these conditions will never be in True.

def check_headers(func):
    ...
        if request.method in ('POST', 'PATCH'):
            if 'Content-Type' in request.headers and\
                    'application/vnd.api+json' in request.headers['Content-Type'] and\
                    request.headers['Content-Type'] != 'application/vnd.api+json':
                error = json.dumps(jsonapi_errors([{'source': '',
                                                    'detail': "Content-Type header must be application/vnd.api+json",
                                                    'title': 'Invalid request header',
                                                    'status': '415'}]), cls=JSONEncoder)
                return make_response(error, 415, {'Content-Type': 'application/vnd.api+json'})
    ...
        return func(*args, **kwargs)
    return wrapper

Instead of the correct error message jsonapi returns:

{
  "errors": [
    {
      "detail": "Object must include `data` key.",
      "source": {
        "pointer": "/"
      },
      "status": "422",
      "title": "Validation error"
    }
  ],
  "jsonapi": {
    "version": "1.0"
  }
}

@akira-dev could you please review the condition and fix the issue?

I've created Pull Request with possible solution.