Skip to content

Commit

Permalink
Merge pull request #200 from Scille/fix_reference_round-trip
Browse files Browse the repository at this point in the history
Let Reference and GenericReference round-trip
  • Loading branch information
lafrech committed Jun 13, 2019
2 parents d45c934 + 68c42da commit f3492e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/test_marshmallow.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,15 @@ class Doc(Document):
# schema to OO world
ma_schema_cls = Doc.schema.as_marshmallow_schema()
ma_schema = ma_schema_cls()
# Dump uMongo object
ret = ma_schema.dump(doc)
assert not ret.errors
assert ret.data == serialized
# Dump OO data (not uMongo object) to ensure bonus fields round-trip
ret = ma_schema.dump(oo_data)
assert not ret.errors
assert ret.data == serialized
# Load serialized data
ret = ma_schema.load(serialized)
assert not ret.errors
assert ret.data == oo_data
Expand Down
6 changes: 6 additions & 0 deletions umongo/marshmallow_bonus.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def _serialize(self, value, attr, obj):
return str(value)
else:
# In OO world, value is a :class:`umongo.data_object.Reference`
# or an ObjectId before being loaded into a Document
if isinstance(value, bson.ObjectId):
return str(value)
return str(value.pk)


Expand All @@ -169,6 +172,9 @@ def _serialize(self, value, attr, obj):
return {'id': str(value['_id']), 'cls': value['_cls']}
else:
# In OO world, value is a :class:`umongo.data_object.Reference`
# or a dict before being loaded into a Document
if isinstance(value, dict):
return {'id': str(value['id']), 'cls': value['cls']}
return {'id': str(value.pk), 'cls': value.document_cls.__name__}

def _deserialize(self, value, attr, data):
Expand Down

0 comments on commit f3492e3

Please sign in to comment.