Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serialize:true takes priority over the OneToMany check for relationships #3214

Merged
merged 1 commit into from
Jun 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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