From ab614ccaeea665fbb21478492689143c01b5238b Mon Sep 17 00:00:00 2001 From: Tim Stirrat Date: Wed, 15 Jul 2015 12:24:46 -0700 Subject: [PATCH] [BUGFIX release] JSONSerializer pass through `payload.included` --- CHANGELOG.md | 8 ++- .../lib/serializers/json-serializer.js | 10 ++- .../serializers/json-serializer-test.js | 69 +++++++++++++++++++ 3 files changed, 82 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7011991ecb9..c3eab956b04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Master +- [#3550](https://github.com/emberjs/data/pull/3550) JSONSerializer now works with EmbeddedRecordsMixin by respecting the `included` property in normalizeResponse [@tstirrat](https://github.com/tstirrat) + ### Release 1.13.5 (July 8, 2015) - [#3437](https://github.com/emberjs/data/pull/3437) Deprecate normalizePayload and normalizeHash [@wecc](https://github.com/wecc) @@ -122,9 +124,9 @@ ##### Store Service moved to an Instance Initializer -In order to fix deprecations warning induced by Ember 1.12, the store service -is now injected as an instanceInitializer. As a consequence, if you had initializers -depending on the store, you should move them to an instance initializer as well, +In order to fix deprecations warning induced by Ember 1.12, the store service +is now injected as an instanceInitializer. As a consequence, if you had initializers +depending on the store, you should move them to an instance initializer as well, and mark it as after: 'ember-data'. - Removed support for DS.FixtureAdapter. You can use it as an addon, or diff --git a/packages/ember-data/lib/serializers/json-serializer.js b/packages/ember-data/lib/serializers/json-serializer.js index f4800b003dc..f79afe8a527 100644 --- a/packages/ember-data/lib/serializers/json-serializer.js +++ b/packages/ember-data/lib/serializers/json-serializer.js @@ -449,11 +449,17 @@ var JSONSerializer = Serializer.extend({ } if (isSingle) { - let { data } = this.normalize(primaryModelClass, payload); + let { data, included } = this.normalize(primaryModelClass, payload); documentHash.data = data; + if (included) { + documentHash.included = included; + } } else { documentHash.data = payload.map((item) => { - let { data } = this.normalize(primaryModelClass, item); + let { data, included } = this.normalize(primaryModelClass, item); + if (included) { + documentHash.included.push(...included); + } return data; }); } diff --git a/packages/ember-data/tests/integration/serializers/json-serializer-test.js b/packages/ember-data/tests/integration/serializers/json-serializer-test.js index a92292d3e08..213a8327c93 100644 --- a/packages/ember-data/tests/integration/serializers/json-serializer-test.js +++ b/packages/ember-data/tests/integration/serializers/json-serializer-test.js @@ -635,3 +635,72 @@ test('normalizeResponse should extract meta using extractMeta', function() { deepEqual(post.meta.authors, ['Tomster', 'Tomhuda']); }); + +test('normalizeResponse returns empty `included` payload by default', function() { + env.registry.register("serializer:post", DS.JSONSerializer.extend()); + + var jsonHash = { + id: "1", + title: "Rails is omakase" + }; + + var post = env.store.serializerFor("post").normalizeResponse(env.store, Post, jsonHash, '1', 'findRecord'); + + deepEqual(post.included, []); +}); + +test('normalizeResponse respects `included` items (single response)', function() { + env.registry.register("serializer:post", DS.JSONSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + comments: { embedded: 'always' } + } + })); + + var jsonHash = { + id: "1", + title: "Rails is omakase", + comments: [ + { id: "1", body: "comment 1" }, + { id: "2", body: "comment 2" } + ] + }; + + var post = env.store.serializerFor("post").normalizeResponse(env.store, Post, jsonHash, '1', 'findRecord'); + + deepEqual(post.included, [ + { id: "1", type: "comment", attributes: { body: "comment 1" }, relationships: {} }, + { id: "2", type: "comment", attributes: { body: "comment 2" }, relationships: {} } + ]); +}); + +test('normalizeResponse respects `included` items (array response)', function() { + env.registry.register("serializer:post", DS.JSONSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + comments: { embedded: 'always' } + } + })); + + var payload = [{ + id: "1", + title: "Rails is omakase", + comments: [ + { id: "1", body: "comment 1" } + ] + }, + { + id: "2", + title: "Post 2", + comments: [ + { id: "2", body: "comment 2" }, + { id: "3", body: "comment 3" } + ] + }]; + + var post = env.store.serializerFor("post").normalizeResponse(env.store, Post, payload, '1', 'findAll'); + + deepEqual(post.included, [ + { id: "1", type: "comment", attributes: { body: "comment 1" }, relationships: {} }, + { id: "2", type: "comment", attributes: { body: "comment 2" }, relationships: {} }, + { id: "3", type: "comment", attributes: { body: "comment 3" }, relationships: {} } + ]); +});