From ef94af8dd2e31894d84c7d78b9b31be2e9868593 Mon Sep 17 00:00:00 2001 From: Dan Schultz Date: Thu, 8 Sep 2011 03:57:42 -0700 Subject: [PATCH] Fix issue with serialization of has one association --- src/mesh/model/serialization/Serializer.as | 15 ++++++++----- .../model/serialization/SerializerTests.as | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/mesh/model/serialization/Serializer.as b/src/mesh/model/serialization/Serializer.as index 530b6e8..fc58ef8 100644 --- a/src/mesh/model/serialization/Serializer.as +++ b/src/mesh/model/serialization/Serializer.as @@ -111,20 +111,25 @@ package mesh.model.serialization if (value is Array) { value = value.map(function(object:Object, ...args):Object { - if (object != null && object.hasOwnProperty("serialize")) { - return object.serialize(options); - } - return new Serializer(object, options).serialize(); + return serializeAssociation(object, options); }); } // The object isn't iterable, so serialize it. else { - value = value != null ? new Serializer(value, options).serialize() : value; + value = serializeAssociation(value, options); } return value; } + private function serializeAssociation(object:Object, options:Object):Object + { + if (object != null && object.hasOwnProperty("serialize")) { + return object.serialize(options); + } + return object != null ? new Serializer(object, options).serialize() : object; + } + /** * Aggregates the properties to serialize on the object. */ diff --git a/tests/mesh/model/serialization/SerializerTests.as b/tests/mesh/model/serialization/SerializerTests.as index 663cab5..10fa914 100644 --- a/tests/mesh/model/serialization/SerializerTests.as +++ b/tests/mesh/model/serialization/SerializerTests.as @@ -5,13 +5,19 @@ package mesh.model.serialization import mesh.Customer; import mesh.Name; import mesh.Order; + import mesh.TestSource; + import mesh.model.store.Store; import mx.collections.ArrayList; import org.flexunit.assertThat; import org.hamcrest.collection.array; + import org.hamcrest.core.allOf; + import org.hamcrest.core.not; import org.hamcrest.object.equalTo; import org.hamcrest.object.hasProperties; + import org.hamcrest.object.hasProperty; + import org.hamcrest.object.hasPropertyWithValue; import org.hamcrest.object.nullValue; public class SerializerTests @@ -38,6 +44,10 @@ package mesh.model.serialization }) ]) }); + + var store:Store = new Store(new TestSource()); + store.add(_customer); + store.commit(); } [Test] @@ -94,5 +104,16 @@ package mesh.model.serialization assertThat(serialized.account, nullValue()); assertThat(serialized.orders, array(hasProperties({total:5}), hasProperties({total:10}))); } + + [Test] + public function testSerializeNestedAssociations():void + { + var serialized:Object = _customer.serialize({ + includes:{account:true, orders:true} + }); + + assertThat(serialized.account, allOf(not(hasProperty("storeKey")), hasPropertyWithValue("number", _customer.account.number))); + assertThat(serialized.orders, array(allOf(not(hasProperty("storeKey")), hasProperties({total:5})), allOf(not(hasProperty("storeKey")), hasProperties({total:10})))); + } } } \ No newline at end of file