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

[BUGFIX beta] Fix JSONSerializer.serializeHasMany() issue #3760 #3765

Merged
merged 1 commit into from
Sep 17, 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
18 changes: 10 additions & 8 deletions packages/ember-data/lib/serializers/json-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1116,16 +1116,18 @@ var JSONSerializer = Serializer.extend({
var key = relationship.key;

if (this._shouldSerializeHasMany(snapshot, key, relationship)) {
var payloadKey;
var hasMany = snapshot.hasMany(key, { ids: true });
if (hasMany !== undefined) {
// if provided, use the mapping provided by `attrs` in
// the serializer
var payloadKey = this._getMappedKey(key);
if (payloadKey === key && this.keyForRelationship) {
payloadKey = this.keyForRelationship(key, "hasMany", "serialize");
}

// if provided, use the mapping provided by `attrs` in
// the serializer
payloadKey = this._getMappedKey(key);
if (payloadKey === key && this.keyForRelationship) {
payloadKey = this.keyForRelationship(key, "hasMany", "serialize");
json[payloadKey] = hasMany;
// 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 @@ -171,6 +171,25 @@ test("serializeHasMany respects keyForRelationship", function() {
});
});

test("serializeHasMany omits unknown relationships on pushed record", function() {

run(function() {
post = env.store.push({
id: "1",
type: "post",
attributes: {
title: "Rails is omakase"
}
});
});

var json = {};

env.store.serializerFor("post").serializeHasMany(post._createSnapshot(), json, { key: "comments", options: {} });

ok(!json.hasOwnProperty("comments"), "Does not add the relationship key to json");
});

test("serializeIntoHash", function() {
run(function() {
post = env.store.createRecord('post', { title: "Rails is omakase" });
Expand Down