Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Commit 9d533a2

Browse files
committed
bug fixes
1 parent 37343e0 commit 9d533a2

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

flask_rest_jsonapi/decorators.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,28 @@ def check_headers(func):
1818
@wraps(func)
1919
def wrapper(*args, **kwargs):
2020
if request.method in ('POST', 'PATCH'):
21-
if 'Content-Type' not in request.headers or request.headers['Content-Type'] != 'application/vnd.api+json':
21+
if 'Content-Type' in request.headers and\
22+
'application/vnd.api+json' in request.headers['Content-Type'] and\
23+
request.headers['Content-Type'] != 'application/vnd.api+json':
2224
error = jsonify(jsonapi_errors([{'source': '',
2325
'detail': "Content-Type header must be application/vnd.api+json",
2426
'title': 'InvalidRequestHeader',
2527
'status': '415'}]))
2628
return make_response(error, 415, {'Content-Type': 'application/vnd.api+json'})
27-
if request.headers.get('Accept') and 'application/vnd.api+json' not in request.accept_mimetypes:
28-
error = jsonify(jsonapi_errors([{'source': '',
29-
'detail': "Accept header must be application/vnd.api+json",
30-
'title': 'InvalidRequestHeader',
31-
'status': '406'}]))
32-
return make_response(error, 406, {'Content-Type': 'application/vnd.api+json'})
29+
if 'Accept' in request.headers:
30+
flag = False
31+
for accept in request.headers['Accept'].split(','):
32+
if accept == 'application/vnd.api+json':
33+
flag = False
34+
break
35+
if 'application/vnd.api+json' in accept and accept != 'application/vnd.api+json':
36+
flag = True
37+
if flag is True:
38+
error = jsonify(jsonapi_errors([{'source': '',
39+
'detail': "Accept header must be application/vnd.api+json",
40+
'title': 'InvalidRequestHeader',
41+
'status': '406'}]))
42+
return make_response(error, 406, {'Content-Type': 'application/vnd.api+json'})
3343
return func(*args, **kwargs)
3444
return wrapper
3545

flask_rest_jsonapi/exceptions.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class JsonApiException(Exception):
88

99
title = 'Unknown error'
1010
status = '500'
11-
source = ''
11+
source = None
1212

1313
def __init__(self, detail, source=None, title=None, status=None, code=None, id_=None, links=None, meta=None):
1414
"""Initialize a jsonapi exception
@@ -17,8 +17,6 @@ def __init__(self, detail, source=None, title=None, status=None, code=None, id_=
1717
:param str detail: the detail of the error
1818
"""
1919
self.detail = detail
20-
if source is not None:
21-
self.source = source
2220
self.source = source
2321
self.code = code
2422
self.id = id_

tests/test_sqlalchemy_data_layer.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ def test_add_pagination_links(app):
338338
assert len(last_page_dict['page[number]']) == 1
339339
assert last_page_dict['page[number]'][0] == '5'
340340

341+
341342
def test_Node(person_model, person_schema, monkeypatch):
342343
from copy import deepcopy
343344
filt = {
@@ -376,8 +377,7 @@ def test_check_method_requirements(monkeypatch):
376377
request = type('request', (object,), dict(method='GET'))
377378
monkeypatch.setattr(flask_rest_jsonapi.decorators, 'request', request)
378379
with pytest.raises(Exception):
379-
flask_rest_jsonapi.\
380-
decorators.check_method_requirements(lambda: 1)(self())
380+
flask_rest_jsonapi.decorators.check_method_requirements(lambda: 1)(self())
381381

382382

383383
def test_json_api_exception():
@@ -738,14 +738,16 @@ def test_single_accept_header(client, register_routes):
738738
response = client.get('/persons', content_type='application/vnd.api+json', headers={'Accept': 'application/vnd.api+json'})
739739
assert response.status_code == 200
740740

741+
741742
def test_multiple_accept_header(client, register_routes):
742743
with client:
743-
response = client.get('/persons', content_type='application/vnd.api+json', headers={'Accept': '*/*, application/vnd.api+json'})
744+
response = client.get('/persons', content_type='application/vnd.api+json', headers={'Accept': '*/*, application/vnd.api+json, application/vnd.api+json;q=0.9'})
744745
assert response.status_code == 200
745746

747+
746748
def test_wrong_accept_header(client, register_routes):
747749
with client:
748-
response = client.get('/persons', content_type='application/vnd.api+json', headers={'Accept': 'error'})
750+
response = client.get('/persons', content_type='application/vnd.api+json', headers={'Accept': 'application/vnd.api+json;q=0.7, application/vnd.api+json;q=0.9'})
749751
assert response.status_code == 406
750752

751753

0 commit comments

Comments
 (0)