Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JSONSerializer to pass through payload.included #3550

Merged
merged 1 commit into from
Jul 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: {} }
]);
});