Skip to content

Commit

Permalink
Fix issue with serialization of has one association
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultz committed Sep 8, 2011
1 parent f9087c1 commit ef94af8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/mesh/model/serialization/Serializer.as
Expand Up @@ -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.
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/mesh/model/serialization/SerializerTests.as
Expand Up @@ -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
Expand All @@ -38,6 +44,10 @@ package mesh.model.serialization
})
])
});

var store:Store = new Store(new TestSource());
store.add(_customer);
store.commit();
}

[Test]
Expand Down Expand Up @@ -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}))));
}
}
}

0 comments on commit ef94af8

Please sign in to comment.