Skip to content

Commit

Permalink
Guard for embedded unknown hasMany relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
wecc committed Jun 1, 2015
1 parent 1b1bd43 commit 98b96cd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/ember-data/lib/serializers/embedded-records-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,17 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
}
var includeIds = this.hasSerializeIdsOption(attr);
var includeRecords = this.hasSerializeRecordsOption(attr);
var key;
var key, hasMany;
if (includeIds) {
key = this.keyForRelationship(attr, relationship.kind, 'serialize');
json[key] = snapshot.hasMany(attr, { ids: true });
} else if (includeRecords) {
key = this.keyForAttribute(attr, 'serialize');
json[key] = snapshot.hasMany(attr).map(function(embeddedSnapshot) {
hasMany = snapshot.hasMany(attr);

Ember.warn("The relationship '" + key + "' is unknown for '" + snapshot.modelName + "' with id '" + snapshot.id + "'. Please include it in your original payload.", Ember.typeOf(hasMany) !== 'undefined');

json[key] = Ember.A(hasMany).map(function(embeddedSnapshot) {
var embeddedJson = embeddedSnapshot.record.serialize({ includeId: true });
this.removeEmbeddedForeignKey(snapshot, embeddedSnapshot, relationship, embeddedJson);
return embeddedJson;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,32 @@ test("serialize with embedded objects (hasMany relationship)", function() {
});
});

test("serialize with embedded objects (unknown hasMany relationship)", function() {
var league;
run(function() {
league = env.store.push(HomePlanet, { name: "Villain League", id: "123" });
});

env.registry.register('serializer:home-planet', DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
villains: { embedded: 'always' }
}
}));

var serializer, json;
warns(function() {
run(function() {
serializer = env.container.lookup("serializer:home-planet");
json = serializer.serialize(league._createSnapshot());
});
}, /The relationship 'villains' is unknown for 'home-planet' with id '123'. Please include it in your original payload./);

deepEqual(json, {
name: "Villain League",
villains: []
});
});

test("serialize with embedded objects (hasMany relationship) supports serialize:false", function() {
run(function() {
league = env.store.createRecord(HomePlanet, { name: "Villain League", id: "123" });
Expand Down

0 comments on commit 98b96cd

Please sign in to comment.