From 98b96cd44536c2b475806b9d2ef234051d5cbceb Mon Sep 17 00:00:00 2001 From: Christoffer Persson Date: Sun, 31 May 2015 14:06:12 +0300 Subject: [PATCH] Guard for embedded unknown hasMany relationship --- .../lib/serializers/embedded-records-mixin.js | 8 ++++-- .../embedded-records-mixin-test.js | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/ember-data/lib/serializers/embedded-records-mixin.js b/packages/ember-data/lib/serializers/embedded-records-mixin.js index d0c2be79e37..7a1a94fda6c 100644 --- a/packages/ember-data/lib/serializers/embedded-records-mixin.js +++ b/packages/ember-data/lib/serializers/embedded-records-mixin.js @@ -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; diff --git a/packages/ember-data/tests/integration/serializers/embedded-records-mixin-test.js b/packages/ember-data/tests/integration/serializers/embedded-records-mixin-test.js index 8606df6b8a8..88a5ef1a876 100644 --- a/packages/ember-data/tests/integration/serializers/embedded-records-mixin-test.js +++ b/packages/ember-data/tests/integration/serializers/embedded-records-mixin-test.js @@ -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" });