63 lines
2.3 KiB
Diff
63 lines
2.3 KiB
Diff
From f71b4f61d96c43748ca1cb9002f874a8d8276312 Mon Sep 17 00:00:00 2001
|
|
From: Stephen Pascoe <stephen.pascoe@lirico.co.uk>
|
|
Date: Wed, 4 Oct 2017 01:01:47 +0100
|
|
Subject: [PATCH] Fix arguments with type=list (#705)
|
|
|
|
Closes #681
|
|
---
|
|
flask_restful/reqparse.py | 2 +-
|
|
tests/test_reqparse.py | 25 +++++++++++++++++++++++++
|
|
2 files changed, 26 insertions(+), 1 deletion(-)
|
|
|
|
diff --git a/flask_restful/reqparse.py b/flask_restful/reqparse.py
|
|
index 8fe142e..527bc94 100644
|
|
--- flask_restful/reqparse.py
|
|
+++ flask_restful/reqparse.py
|
|
@@ -176,7 +176,7 @@ class Argument(object):
|
|
values = source.getlist(name)
|
|
else:
|
|
values = source.get(name)
|
|
- if not isinstance(values, collections.MutableSequence):
|
|
+ if not (isinstance(values, collections.MutableSequence) and self.action == 'append'):
|
|
values = [values]
|
|
|
|
for value in values:
|
|
diff --git a/tests/test_reqparse.py b/tests/test_reqparse.py
|
|
index ce9ce30..df18ead 100644
|
|
--- tests/test_reqparse.py
|
|
+++ tests/test_reqparse.py
|
|
@@ -891,5 +891,30 @@ class ReqParseTestCase(unittest.TestCase):
|
|
self.assertEquals(args['int1'], 1)
|
|
self.assertEquals(args['int2'], 2)
|
|
|
|
+ def test_list_argument(self):
|
|
+ app = Flask(__name__)
|
|
+
|
|
+ parser = RequestParser()
|
|
+ parser.add_argument('arg1', location='json', type=list)
|
|
+
|
|
+ with app.test_request_context('/bubble', method="post",
|
|
+ data=json.dumps({'arg1': ['foo', 'bar']}),
|
|
+ content_type='application/json'):
|
|
+ args = parser.parse_args()
|
|
+ self.assertEquals(args['arg1'], ['foo', 'bar'])
|
|
+
|
|
+ def test_list_argument_dict(self):
|
|
+ app = Flask(__name__)
|
|
+
|
|
+ parser = RequestParser()
|
|
+ parser.add_argument('arg1', location='json', type=list)
|
|
+
|
|
+ with app.test_request_context('/bubble', method="post",
|
|
+ data=json.dumps({'arg1': [{'foo': 1, 'bar': 2}]}),
|
|
+ content_type='application/json'):
|
|
+ args = parser.parse_args()
|
|
+ self.assertEquals(args['arg1'], [{'foo': 1, 'bar': 2}])
|
|
+
|
|
+
|
|
if __name__ == '__main__':
|
|
unittest.main()
|
|
--
|
|
2.17.0.rc1
|
|
|