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..1046680d94a 100644 --- a/packages/ember-data/lib/serializers/json-serializer.js +++ b/packages/ember-data/lib/serializers/json-serializer.js @@ -449,11 +449,13 @@ var JSONSerializer = Serializer.extend({ } if (isSingle) { - let { data } = this.normalize(primaryModelClass, payload); + let { data, included } = this.normalize(primaryModelClass, payload); documentHash.data = data; + documentHash.included = included; } else { documentHash.data = payload.map((item) => { - let { data } = this.normalize(primaryModelClass, item); + let { data, included } = this.normalize(primaryModelClass, item); + 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..972ae1c4c6a 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,59 @@ test('normalizeResponse should extract meta using extractMeta', function() { deepEqual(post.meta.authors, ['Tomster', 'Tomhuda']); }); + +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: {} } + ]); +});