Skip to content

Commit

Permalink
Merge pull request #132 from mikeywaites/fix/130-collection-none
Browse files Browse the repository at this point in the history
fix issue when collection is not required and not passed closes #130
  • Loading branch information
mikeywaites authored Feb 21, 2017
2 parents 3ec3628 + 93a76e8 commit 8beafdb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0-rc9
1.0.0-rc10
39 changes: 20 additions & 19 deletions kim/pipelines/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .serialization import SerializePipeline


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

output = []

if not hasattr(session.data, '__iter__'):
raise session.field.invalid('type_error')

for i, datum in enumerate(session.data):
_output = {}
# If the object already exists, try to match up the existing elements
# with those in the input json
if existing_value is not None:
try:
_output[wrapped_field.opts.source] = existing_value[i]
except IndexError:
pass

mapper_session = session.mapper.get_mapper_session(datum, _output)
wrapped_field.marshal(mapper_session, parent_session=session)

result = _output[wrapped_field.opts.source]
output.append(result)
if session.data is not None:
if not hasattr(session.data, '__iter__'):
raise session.field.invalid('type_error')

for i, datum in enumerate(session.data):
_output = {}
# If the object already exists, try to match up the existing elements
# with those in the input json
if existing_value is not None:
try:
_output[wrapped_field.opts.source] = existing_value[i]
except IndexError:
pass

mapper_session = session.mapper.get_mapper_session(datum, _output)
wrapped_field.marshal(mapper_session, parent_session=session)

result = _output[wrapped_field.opts.source]
output.append(result)

session.data = output
return session.data
Expand Down
26 changes: 26 additions & 0 deletions tests/test_pipelines/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,29 @@ class PostMapper(Mapper):

mapper = PostMapper(data=data)
mapper.marshal()


def test_marshal_collection_required_false():

class UserMapper(Mapper):

__type__ = TestType

id = field.String(required=True)
name = field.String()

class PostMapper(Mapper):

__type__ = TestType

readers = field.Collection(
field.Nested(UserMapper, allow_create=True),
required=False)

data = {}

mapper = PostMapper(data=data)

output = mapper.marshal()

assert output.readers == []

0 comments on commit 8beafdb

Please sign in to comment.