Skip to content

Commit 67eb7ac

Browse files
mattwang44claude
andauthored
fix(deco): load body as json whenever content type has 'application/json' (#45)
Co-authored-by: Claude <[email protected]>
1 parent 5bc16a8 commit 67eb7ac

File tree

6 files changed

+34
-11
lines changed

6 files changed

+34
-11
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on: [pull_request]
44

55
jobs:
66
pre-commit:
7-
runs-on: ubuntu-20.04
7+
runs-on: ubuntu-latest
88
steps:
99
- uses: actions/checkout@v3
1010
with:

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ repos:
2222
rev: 24.4.2
2323
hooks:
2424
- id: black
25-
language_version: python3.8
25+
language_version: python3.11
2626
args:
2727
- --target-version=py38
2828
- --line-length=120

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
3.5.1
5+
-----
6+
- Fix payload loading issue when content type header contains but not exactly "application/json"
7+
8+
49
3.5.0
510
-----
611
- [feature] add `NUMBER` that accepts float and integer

data_spec_validator/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '3.5.0'
1+
__version__ = '3.5.1'

data_spec_validator/decorator/decorators.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ def _collect_data(method, req_qp, req_data) -> Dict:
104104
return req_data
105105

106106
def _get_dj_payload(request):
107-
content_type = request.headers.get('Content-Type')
108-
if content_type == 'application/json':
107+
if request.content_type and 'application/json' in request.content_type:
109108
try:
110109
return request.body and json.loads(request.body) or {}
111110
except Exception:

test/utils.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,36 @@ def make_request(cls, path='/', method='GET', user=None, headers=None, data=None
4343
kwargs = {'REQUEST_METHOD': method, 'PATH_INFO': path, 'wsgi.input': StringIO()}
4444
if qs:
4545
kwargs.update({'QUERY_STRING': qs})
46+
47+
# Set content type in initial environ if data is provided
48+
if data and method in ['POST', 'PUT', 'PATCH', 'DELETE']:
49+
kwargs.update(
50+
{
51+
'CONTENT_TYPE': 'application/json' if is_json else 'application/x-www-form-urlencoded',
52+
'CONTENT_LENGTH': len(str(data)),
53+
}
54+
)
55+
4656
req = WSGIRequest(kwargs)
4757
else:
4858
kwargs = {'path': path, 'method': method}
4959
if qs:
5060
kwargs.update({'query_string': qs})
61+
62+
# Set content type in initial scope if data is provided
63+
if data and method in ['POST', 'PUT', 'PATCH', 'DELETE']:
64+
headers = kwargs.get('headers', [])
65+
headers.extend(
66+
[
67+
[
68+
b'content-type',
69+
('application/json' if is_json else 'application/x-www-form-urlencoded').encode(),
70+
],
71+
[b'content-length', str(len(str(data))).encode()],
72+
]
73+
)
74+
kwargs['headers'] = headers
75+
5176
req = ASGIRequest(kwargs, StringIO())
5277

5378
req.user = user
@@ -60,12 +85,6 @@ def make_request(cls, path='/', method='GET', user=None, headers=None, data=None
6085
setattr(req, 'GET', data)
6186
elif method in ['POST', 'PUT', 'PATCH', 'DELETE']:
6287
req.read() # trigger RawPostDataException and force DRF to load data from req.POST
63-
req.META.update(
64-
{
65-
'CONTENT_TYPE': 'application/json' if is_json else 'application/x-www-form-urlencoded',
66-
'CONTENT_LENGTH': len(str(data)),
67-
}
68-
)
6988
if is_json:
7089
req._body = data
7190
req.POST = {}

0 commit comments

Comments
 (0)