Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix list field accept dict parameter bug #597

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion flask_restplus/fields.py
Expand Up @@ -273,9 +273,12 @@ def __init__(self, cls_or_instance, **kwargs):

def format(self, value):
# Convert all instances in typed list to container type
if isinstance(value, set):
if isinstance(value, (set, tuple)):
value = list(value)

if not isinstance(value, list):
raise MarshallingError('should be list')

is_nested = isinstance(self.container, Nested) or type(self.container) is Raw

def is_attr(val):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_fields.py
Expand Up @@ -836,6 +836,13 @@ def test_with_nested_field(self, api):
expected = [OrderedDict([('name', 'John Doe')]), OrderedDict([('name', 'Jane Doe')])]
self.assert_field(field, data, expected)

def test_format_error(self, api):
nested_fields = api.model('NestedModel', {'name': fields.String})
field = fields.List(fields.Nested(nested_fields))
value = {'name': 'John Doe', 'age': 42}
with pytest.raises(fields.MarshallingError):
field.format(value)

def test_min_items(self):
field = fields.List(fields.String, min_items=5)
assert 'minItems' in field.__schema__
Expand Down