at master 5.1 kB view raw
1Fixes compatibility with Werkzeug 2.1.0 ported over from flask-restx#423. 2 3https://github.com/python-restx/flask-restx/pull/423 4 5diff --git a/flask_restful/reqparse.py b/flask_restful/reqparse.py 6index 9bb3099..5c59594 100644 7--- a/flask_restful/reqparse.py 8+++ b/flask_restful/reqparse.py 9@@ -114,7 +114,10 @@ class Argument(object): 10 :param request: The flask request object to parse arguments from 11 """ 12 if isinstance(self.location, six.string_types): 13- value = getattr(request, self.location, MultiDict()) 14+ if self.location in {"json", "get_json"}: 15+ value = request.get_json(silent=True) 16+ else: 17+ value = getattr(request, self.location, MultiDict()) 18 if callable(value): 19 value = value() 20 if value is not None: 21@@ -122,7 +125,10 @@ class Argument(object): 22 else: 23 values = MultiDict() 24 for l in self.location: 25- value = getattr(request, l, None) 26+ if l in {"json", "get_json"}: 27+ value = request.get_json(silent=True) 28+ else: 29+ value = getattr(request, l, None) 30 if callable(value): 31 value = value() 32 if value is not None: 33diff --git a/tests/test_api.py b/tests/test_api.py 34index 15f12eb..9a9cceb 100644 35--- a/tests/test_api.py 36+++ b/tests/test_api.py 37@@ -936,7 +936,7 @@ class APITestCase(unittest.TestCase): 38 app = app.test_client() 39 resp = app.get('/api') 40 self.assertEqual(resp.status_code, 302) 41- self.assertEqual(resp.headers['Location'], 'http://localhost/') 42+ self.assertEqual(resp.headers['Location'], '/') 43 44 def test_json_float_marshalled(self): 45 app = Flask(__name__) 46diff --git a/tests/test_reqparse.py b/tests/test_reqparse.py 47index 1d75e40..e5c586b 100644 48--- a/tests/test_reqparse.py 49+++ b/tests/test_reqparse.py 50@@ -23,8 +23,9 @@ class ReqParseTestCase(unittest.TestCase): 51 with app.app_context(): 52 parser = RequestParser() 53 parser.add_argument('foo', choices=('one', 'two'), help='Bad choice: {error_msg}') 54- req = Mock(['values']) 55+ req = Mock(["values", "get_json"]) 56 req.values = MultiDict([('foo', 'three')]) 57+ req.get_json.return_value = None 58 parser.parse_args(req) 59 expected = {'foo': 'Bad choice: three is not a valid choice'} 60 abort.assert_called_with(400, message=expected) 61@@ -35,8 +36,9 @@ class ReqParseTestCase(unittest.TestCase): 62 with app.app_context(): 63 parser = RequestParser() 64 parser.add_argument('foo', choices=('one', 'two'), help=u'Bad choice: {error_msg}') 65- req = Mock(['values']) 66+ req = Mock(["values", "get_json"]) 67 req.values = MultiDict([('foo', u'\xf0\x9f\x8d\x95')]) 68+ req.get_json.return_value = None 69 parser.parse_args(req) 70 expected = {'foo': u'Bad choice: \xf0\x9f\x8d\x95 is not a valid choice'} 71 abort.assert_called_with(400, message=expected) 72@@ -47,8 +49,9 @@ class ReqParseTestCase(unittest.TestCase): 73 with app.app_context(): 74 parser = RequestParser() 75 parser.add_argument('foo', choices=['one', 'two'], help='Please select a valid choice') 76- req = Mock(['values']) 77+ req = Mock(["values", "get_json"]) 78 req.values = MultiDict([('foo', 'three')]) 79+ req.get_json.return_value = None 80 parser.parse_args(req) 81 expected = {'foo': 'Please select a valid choice'} 82 abort.assert_called_with(400, message=expected) 83@@ -58,8 +61,9 @@ class ReqParseTestCase(unittest.TestCase): 84 def bad_choice(): 85 parser = RequestParser() 86 parser.add_argument('foo', choices=['one', 'two']) 87- req = Mock(['values']) 88+ req = Mock(["values", "get_json"]) 89 req.values = MultiDict([('foo', 'three')]) 90+ req.get_json.return_value = None 91 parser.parse_args(req) 92 abort.assert_called_with(400, message='three is not a valid choice') 93 app = Flask(__name__) 94@@ -190,7 +194,8 @@ class ReqParseTestCase(unittest.TestCase): 95 self.assertTrue(len(arg.source(req)) == 0) # yes, basically you don't find it 96 97 def test_source_default_location(self): 98- req = Mock(['values']) 99+ req = Mock(['values', 'get_json']) 100+ req.get_json.return_value = None 101 req._get_child_mock = lambda **kwargs: MultiDict() 102 arg = Argument('foo') 103 self.assertEqual(arg.source(req), req.values) 104@@ -215,8 +220,9 @@ class ReqParseTestCase(unittest.TestCase): 105 args = parser.parse_args(req) 106 self.assertEqual(args['foo'], "bar") 107 108- req = Mock() 109+ req = Mock(['get_json']) 110 req.values = () 111+ req.get_json.return_value = None 112 req.json = None 113 req.view_args = {"foo": "bar"} 114 parser = RequestParser()