Skip to content

Commit

Permalink
[BUGFIX beta] convert single record sideloaded records to plural
Browse files Browse the repository at this point in the history
Before, `_normalizeArray` would call `forEach` even if the object wasn't
an array. We guard against it by using `Ember.makeArray`.

fixes #3805

#3805
  • Loading branch information
Stanley Stuart committed Apr 8, 2016
1 parent 1ced8b5 commit 18b7ca3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion addon/serializers/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ var RESTSerializer = JSONSerializer.extend({
let serializer = store.serializerFor(modelName);

/*jshint loopfunc:true*/
arrayHash.forEach((hash) => {
Ember.makeArray(arrayHash).forEach((hash) => {
let { data, included } = this._normalizePolymorphicRecord(store, hash, prop, modelClass, serializer);
documentHash.data.push(data);
if (included) {
Expand Down
69 changes: 68 additions & 1 deletion tests/integration/serializers/rest-serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module("integration/serializer/rest - RESTSerializer", {
});
EvilMinion = DS.Model.extend({
superVillain: DS.belongsTo('super-villain', { async: false }),
name: DS.attr('string')
name: DS.attr('string'),
doomsdayDevice: DS.belongsTo('doomsday-device', {async: false})
});
YellowMinion = EvilMinion.extend({
eyes: DS.attr('number')
Expand Down Expand Up @@ -894,3 +895,69 @@ test('Serializer should respect the attrs hash in links', function(assert) {

assert.equal(documentHash.data.relationships.evilMinions.links.related, 'me/minions');
});

// https://github.com/emberjs/data/issues/3805
test('normalizes sideloaded single record so that it sideloads correctly - belongsTo - GH-3805', function(assert) {
env.registry.register("serializer:doomsday-device", DS.RESTSerializer.extend());
let payload = {
doomsdayDevice: {
id: 1,
evilMinion: 2
},
evilMinion: {
id: 2,
doomsdayDevice: 1
}
};

let document = env.store.serializerFor('doomsday-device').normalizeSingleResponse(env.store, DoomsdayDevice, payload);
assert.equal(document.data.relationships.evilMinion.data.id, 2);
assert.equal(document.included.length, 1);
assert.deepEqual(document.included[0], {
attributes: {},
id: '2',
type: 'evil-minion',
relationships: {
doomsdayDevice: {
data: {
id: '1',
type: 'doomsday-device'
}
}
}
});
});

// https://github.com/emberjs/data/issues/3805
test('normalizes sideloaded single record so that it sideloads correctly - hasMany - GH-3805', function(assert) {
env.registry.register("serializer:home-planet", DS.RESTSerializer.extend());
let payload = {
homePlanet: {
id: 1,
superVillains: [2],
},
superVillain: {
id: 2,
homePlanet: 1
}
};

let document = env.store.serializerFor('home-planet').normalizeSingleResponse(env.store, HomePlanet, payload);

assert.equal(document.data.relationships.superVillains.data.length, 1);
assert.equal(document.data.relationships.superVillains.data[0].id, 2);
assert.equal(document.included.length, 1);
assert.deepEqual(document.included[0], {
attributes: {},
id: '2',
type: 'super-villain',
relationships: {
homePlanet: {
data: {
id: '1',
type: 'home-planet'
}
}
}
});
});

0 comments on commit 18b7ca3

Please sign in to comment.