Skip to content

Commit d88f81d

Browse files
Stacy W. Smithstacywsmith
Stacy W. Smith
authored andcommitted
Handle Werkzeug 2.1.0 change to Request.get_json().
pallets/werkzeug#2339 changed the behavior of `Request.get_json()` and the `Request.json` property to raise a `BadRequest` if `Request.get_json()` is called without `silent=True`, or the `Request.json` property is accessed, and the content type is not `"application/json"`. Argument parsing allows parsing from multiple locations, and defaults to `["json", "values"]`, but if the locations include `"json"` and the content type is not `"application/json"`, a `BadRequest` is now raised with Werkzeug >= 2.1.0. Invoking `Request.get_json()` with the `silent=True` parameter now handles the situation where `"json"` is included in the locations, but the content type is not `"application/json"`.
1 parent 88497ce commit d88f81d

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

flask_restx/reqparse.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,21 @@ def source(self, request):
138138
:param request: The flask request object to parse arguments from
139139
"""
140140
if isinstance(self.location, six.string_types):
141-
value = getattr(request, self.location, MultiDict())
141+
if self.location in {"json", "get_json"}:
142+
value = request.get_json(silent=True)
143+
else:
144+
value = getattr(request, self.location, MultiDict())
142145
if callable(value):
143146
value = value()
144147
if value is not None:
145148
return value
146149
else:
147150
values = MultiDict()
148151
for l in self.location:
149-
value = getattr(request, l, None)
152+
if l in {"json", "get_json"}:
153+
value = request.get_json(silent=True)
154+
else:
155+
value = getattr(request, l, None)
150156
if callable(value):
151157
value = value()
152158
if value is not None:

tests/test_reqparse.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ def test_help(self, app, mocker):
4141
)
4242
parser = RequestParser()
4343
parser.add_argument("foo", choices=("one", "two"), help="Bad choice.")
44-
req = mocker.Mock(["values"])
44+
req = mocker.Mock(["values", "get_json"])
4545
req.values = MultiDict([("foo", "three")])
46+
req.get_json.return_value = None
4647
with pytest.raises(BadRequest):
4748
parser.parse_args(req)
4849
expected = {
@@ -58,7 +59,8 @@ def test_no_help(self, app, mocker):
5859
)
5960
parser = RequestParser()
6061
parser.add_argument("foo", choices=["one", "two"])
61-
req = mocker.Mock(["values"])
62+
req = mocker.Mock(["values", "get_json"])
63+
req.get_json.return_value = None
6264
req.values = MultiDict([("foo", "three")])
6365
with pytest.raises(BadRequest):
6466
parser.parse_args(req)
@@ -76,9 +78,9 @@ def test_viewargs(self, mocker):
7678
args = parser.parse_args(req)
7779
assert args["foo"] == "bar"
7880

79-
req = mocker.Mock()
81+
req = mocker.Mock(["get_json"])
8082
req.values = ()
81-
req.json = None
83+
req.get_json.return_value = None
8284
req.view_args = {"foo": "bar"}
8385
parser = RequestParser()
8486
parser.add_argument("foo", store_missing=True)
@@ -101,11 +103,10 @@ def test_parse_unicode_app(self, app):
101103
args = parser.parse_args()
102104
assert args["foo"] == "barß"
103105

104-
@pytest.mark.request_context("/bubble", method="post")
106+
@pytest.mark.request_context("/bubble", method="post", content_type='application/json')
105107
def test_json_location(self):
106108
parser = RequestParser()
107109
parser.add_argument("foo", location="json", store_missing=True)
108-
109110
args = parser.parse_args()
110111
assert args["foo"] is None
111112

@@ -856,7 +857,8 @@ def test_source_bad_location(self, mocker):
856857
assert len(arg.source(req)) == 0 # yes, basically you don't find it
857858

858859
def test_source_default_location(self, mocker):
859-
req = mocker.Mock(["values"])
860+
req = mocker.Mock(["values", "get_json"])
861+
req.get_json.return_value = None
860862
req._get_child_mock = lambda **kwargs: MultiDict()
861863
arg = Argument("foo")
862864
assert arg.source(req) == req.values

0 commit comments

Comments
 (0)