Skip to content

Commit

Permalink
Merge pull request #3550 from tstirrat/ts-jsonapi-keep-included
Browse files Browse the repository at this point in the history
Fix JSONSerializer to pass through `payload.included`
  • Loading branch information
wecc committed Jul 16, 2015
2 parents ab1f538 + ab614cc commit 09556fc
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions packages/ember-data/lib/serializers/json-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {} }
]);
});

0 comments on commit 09556fc

Please sign in to comment.