Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

3.5.1
-----
- Fix payload loading issue when content type header contains but not exactly "application/json"


3.5.0
-----
- [feature] add `NUMBER` that accepts float and integer
Expand Down
2 changes: 1 addition & 1 deletion data_spec_validator/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '3.5.0'
__version__ = '3.5.1'
3 changes: 1 addition & 2 deletions data_spec_validator/decorator/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ def _collect_data(method, req_qp, req_data) -> Dict:
return req_data

def _get_dj_payload(request):
content_type = request.headers.get('Content-Type')
if content_type == 'application/json':
if request.content_type and 'application/json' in request.content_type:
try:
return request.body and json.loads(request.body) or {}
except Exception:
Expand Down
31 changes: 25 additions & 6 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,36 @@ def make_request(cls, path='/', method='GET', user=None, headers=None, data=None
kwargs = {'REQUEST_METHOD': method, 'PATH_INFO': path, 'wsgi.input': StringIO()}
if qs:
kwargs.update({'QUERY_STRING': qs})

# Set content type in initial environ if data is provided
if data and method in ['POST', 'PUT', 'PATCH', 'DELETE']:
kwargs.update(
{
'CONTENT_TYPE': 'application/json' if is_json else 'application/x-www-form-urlencoded',
'CONTENT_LENGTH': len(str(data)),
}
)

req = WSGIRequest(kwargs)
else:
kwargs = {'path': path, 'method': method}
if qs:
kwargs.update({'query_string': qs})

# Set content type in initial scope if data is provided
if data and method in ['POST', 'PUT', 'PATCH', 'DELETE']:
headers = kwargs.get('headers', [])
headers.extend(
[
[
b'content-type',
('application/json' if is_json else 'application/x-www-form-urlencoded').encode(),
],
[b'content-length', str(len(str(data))).encode()],
]
)
kwargs['headers'] = headers

req = ASGIRequest(kwargs, StringIO())

req.user = user
Expand All @@ -60,12 +85,6 @@ def make_request(cls, path='/', method='GET', user=None, headers=None, data=None
setattr(req, 'GET', data)
elif method in ['POST', 'PUT', 'PATCH', 'DELETE']:
req.read() # trigger RawPostDataException and force DRF to load data from req.POST
req.META.update(
{
'CONTENT_TYPE': 'application/json' if is_json else 'application/x-www-form-urlencoded',
'CONTENT_LENGTH': len(str(data)),
}
)
if is_json:
req._body = data
req.POST = {}
Expand Down
Loading