Skip to content

Commit 19cc575

Browse files
committed
Refactored part of fields.List.output() into List.format() so that the field can be properly applied to single values
1 parent 43585b6 commit 19cc575

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

flask_restful/fields.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,25 @@ def __init__(self, cls_or_instance, **kwargs):
145145
raise MarshallingException(error_msg)
146146
self.container = cls_or_instance
147147

148+
def format(self, value):
149+
# Convert all instances in typed list to container type
150+
if isinstance(value, set):
151+
value = list(value)
152+
153+
return [
154+
self.container.output(idx,
155+
val if isinstance(val, dict) and
156+
not isinstance(self.container, Nested)
157+
and not type(self.container) is Raw
158+
else value)
159+
for idx, val in enumerate(value)
160+
]
161+
148162
def output(self, key, data):
149163
value = get_value(key if self.attribute is None else self.attribute, data)
150164
# we cannot really test for external dict behavior
151165
if is_indexable_but_not_string(value) and not isinstance(value, dict):
152-
# Convert all instances in typed list to container type
153-
if isinstance(value, set):
154-
value = list(value)
155-
156-
return [
157-
self.container.output(idx,
158-
val if isinstance(val, dict) and
159-
not isinstance(self.container, Nested)
160-
and not type(self.container) is Raw
161-
else value)
162-
for idx, val in enumerate(value)
163-
]
166+
return self.format(value)
164167

165168
if value is None:
166169
return self.default

0 commit comments

Comments
 (0)