Skip to content

Commit

Permalink
serialize:true takes priority over the OneToMany check for relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
sly7-7 committed Jun 10, 2015
1 parent 3d7dba5 commit db82aeb
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 8 deletions.
46 changes: 38 additions & 8 deletions packages/ember-data/lib/serializers/json-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,41 @@ export default Serializer.extend({
return !attrs || !attrs[key] || attrs[key].serialize !== false;
},

/**
When attrs.key.serialize is set to true then
it takes priority over the other checks and the related
attribute/relationship will be serialized
@method _mustSerialize
@private
@param {String} key
@return {boolean} true if the key must be serialized
*/
_mustSerialize: function(key) {
var attrs = get(this, 'attrs');

return attrs && attrs[key] && attrs[key].serialize === true;
},

/**
Check if the given hasMany relationship should be serialized
@method _shouldSerializeHasMany
@private
@param {DS.Snapshot} snapshot
@param {String} key
@param {String} relationshipType
@return {boolean} true if the hasMany relationship should be serialized
*/
_shouldSerializeHasMany: function (snapshot, key, relationship) {
var relationshipType = snapshot.type.determineRelationshipType(relationship, this.store);
if (this._mustSerialize(key)) {
return true;
}
return this._canSerialize(key) && (relationshipType === 'manyToNone' || relationshipType === 'manyToMany');
},


// SERIALIZE
/**
Called when a record is saved in order to convert the
Expand Down Expand Up @@ -1095,7 +1130,7 @@ export default Serializer.extend({
serializeHasMany: function(snapshot, json, relationship) {
var key = relationship.key;

if (this._canSerialize(key)) {
if (this._shouldSerializeHasMany(snapshot, key, relationship)) {
var payloadKey;

// if provided, use the mapping provided by `attrs` in
Expand All @@ -1104,13 +1139,8 @@ export default Serializer.extend({
if (payloadKey === key && this.keyForRelationship) {
payloadKey = this.keyForRelationship(key, "hasMany", "serialize");
}

var relationshipType = snapshot.type.determineRelationshipType(relationship, this.store);

if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany') {
json[payloadKey] = snapshot.hasMany(key, { ids: true });
// TODO support for polymorphic manyToNone and manyToMany relationships
}
json[payloadKey] = snapshot.hasMany(key, { ids: true });
// TODO support for polymorphic manyToNone and manyToMany relationships
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,86 @@ test('Serializer respects `serialize: false` on the attrs hash for a `belongsTo`
ok(!payload.hasOwnProperty(serializedProperty), "Does not add the key to instance");
});

test('Serializer respects `serialize: false` on the attrs hash for a `hasMany` property', function() {
expect(1);
env.registry.register("serializer:post", DS.JSONSerializer.extend({
attrs: {
comments: { serialize: false }
}
}));

run(function() {
post = env.store.createRecord('post', { title: "Rails is omakase" });
comment = env.store.createRecord('comment', { body: "Omakase is delicious", post: post });
});

var serializer = env.container.lookup("serializer:post");
var serializedProperty = serializer.keyForRelationship('comments', 'hasMany');

var payload = serializer.serialize(post._createSnapshot());
ok(!payload.hasOwnProperty(serializedProperty), "Does not add the key to instance");
});

test('Serializer respects `serialize: false` on the attrs hash for a `belongsTo` property', function() {
expect(1);
env.registry.register("serializer:comment", DS.JSONSerializer.extend({
attrs: {
post: { serialize: false }
}
}));

run(function() {
post = env.store.createRecord('post', { title: "Rails is omakase" });
comment = env.store.createRecord('comment', { body: "Omakase is delicious", post: post });
});

var serializer = env.container.lookup("serializer:comment");
var serializedProperty = serializer.keyForRelationship('post', 'belongsTo');

var payload = serializer.serialize(comment._createSnapshot());
ok(!payload.hasOwnProperty(serializedProperty), "Does not add the key to instance");
});

test('Serializer respects `serialize: true` on the attrs hash for a `hasMany` property', function() {
expect(1);
env.registry.register("serializer:post", DS.JSONSerializer.extend({
attrs: {
comments: { serialize: true }
}
}));

run(function() {
post = env.store.createRecord('post', { title: "Rails is omakase" });
comment = env.store.createRecord('comment', { body: "Omakase is delicious", post: post });
});

var serializer = env.container.lookup("serializer:post");
var serializedProperty = serializer.keyForRelationship('comments', 'hasMany');

var payload = serializer.serialize(post._createSnapshot());
ok(payload.hasOwnProperty(serializedProperty), "Add the key to instance");
});

test('Serializer respects `serialize: true` on the attrs hash for a `belongsTo` property', function() {
expect(1);
env.registry.register("serializer:comment", DS.JSONSerializer.extend({
attrs: {
post: { serialize: true }
}
}));

run(function() {
post = env.store.createRecord('post', { title: "Rails is omakase" });
comment = env.store.createRecord('comment', { body: "Omakase is delicious", post: post });
});

var serializer = env.container.lookup("serializer:comment");
var serializedProperty = serializer.keyForRelationship('post', 'belongsTo');

var payload = serializer.serialize(comment._createSnapshot());
ok(payload.hasOwnProperty(serializedProperty), "Add the key to instance");
});

test("Serializer should merge attrs from superclasses", function() {
expect(4);
Post.reopen({
Expand Down

2 comments on commit db82aeb

@broerse
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rsutphin Have you checked this with Pouch?

@rsutphin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not. The principle seems fine, though.

Please sign in to comment.