Skip to content

Commit 8beafdb

Browse files
authored
Merge pull request #132 from mikeywaites/fix/130-collection-none
fix issue when collection is not required and not passed closes #130
2 parents 3ec3628 + 93a76e8 commit 8beafdb

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0-rc9
1+
1.0.0-rc10

kim/pipelines/collection.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .serialization import SerializePipeline
1414

1515

16-
@pipe()
16+
@pipe(run_if_none=True)
1717
def marshall_collection(session):
1818
"""iterate over each item in ``data`` and marshal the item through the
1919
wrapped field defined for this collection
@@ -26,24 +26,25 @@ def marshall_collection(session):
2626

2727
output = []
2828

29-
if not hasattr(session.data, '__iter__'):
30-
raise session.field.invalid('type_error')
31-
32-
for i, datum in enumerate(session.data):
33-
_output = {}
34-
# If the object already exists, try to match up the existing elements
35-
# with those in the input json
36-
if existing_value is not None:
37-
try:
38-
_output[wrapped_field.opts.source] = existing_value[i]
39-
except IndexError:
40-
pass
41-
42-
mapper_session = session.mapper.get_mapper_session(datum, _output)
43-
wrapped_field.marshal(mapper_session, parent_session=session)
44-
45-
result = _output[wrapped_field.opts.source]
46-
output.append(result)
29+
if session.data is not None:
30+
if not hasattr(session.data, '__iter__'):
31+
raise session.field.invalid('type_error')
32+
33+
for i, datum in enumerate(session.data):
34+
_output = {}
35+
# If the object already exists, try to match up the existing elements
36+
# with those in the input json
37+
if existing_value is not None:
38+
try:
39+
_output[wrapped_field.opts.source] = existing_value[i]
40+
except IndexError:
41+
pass
42+
43+
mapper_session = session.mapper.get_mapper_session(datum, _output)
44+
wrapped_field.marshal(mapper_session, parent_session=session)
45+
46+
result = _output[wrapped_field.opts.source]
47+
output.append(result)
4748

4849
session.data = output
4950
return session.data

tests/test_pipelines/test_collection.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,29 @@ class PostMapper(Mapper):
388388

389389
mapper = PostMapper(data=data)
390390
mapper.marshal()
391+
392+
393+
def test_marshal_collection_required_false():
394+
395+
class UserMapper(Mapper):
396+
397+
__type__ = TestType
398+
399+
id = field.String(required=True)
400+
name = field.String()
401+
402+
class PostMapper(Mapper):
403+
404+
__type__ = TestType
405+
406+
readers = field.Collection(
407+
field.Nested(UserMapper, allow_create=True),
408+
required=False)
409+
410+
data = {}
411+
412+
mapper = PostMapper(data=data)
413+
414+
output = mapper.marshal()
415+
416+
assert output.readers == []

0 commit comments

Comments
 (0)