Skip to content

Commit c68d0ef

Browse files
author
Stanley Stuart
committed
[BUGFIX beta] convert single record sideloaded records to plural
Before, `_normalizeArray` would call `forEach` even if the object wasn't an array. We guard against it by using `Ember.makeArray`. fixes #3805 #3805
1 parent 1ced8b5 commit c68d0ef

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

addon/serializers/rest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ var RESTSerializer = JSONSerializer.extend({
187187
let serializer = store.serializerFor(modelName);
188188

189189
/*jshint loopfunc:true*/
190-
arrayHash.forEach((hash) => {
190+
Ember.makeArray(arrayHash).forEach((hash) => {
191191
let { data, included } = this._normalizePolymorphicRecord(store, hash, prop, modelClass, serializer);
192192
documentHash.data.push(data);
193193
if (included) {

tests/integration/serializers/rest-serializer-test.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module("integration/serializer/rest - RESTSerializer", {
2323
});
2424
EvilMinion = DS.Model.extend({
2525
superVillain: DS.belongsTo('super-villain', { async: false }),
26-
name: DS.attr('string')
26+
name: DS.attr('string'),
27+
doomsdayDevice: DS.belongsTo('doomsday-device', { async: false })
2728
});
2829
YellowMinion = EvilMinion.extend({
2930
eyes: DS.attr('number')
@@ -894,3 +895,69 @@ test('Serializer should respect the attrs hash in links', function(assert) {
894895

895896
assert.equal(documentHash.data.relationships.evilMinions.links.related, 'me/minions');
896897
});
898+
899+
// https://github.com/emberjs/data/issues/3805
900+
test('normalizes sideloaded single record so that it sideloads correctly - belongsTo - GH-3805', function(assert) {
901+
env.registry.register("serializer:doomsday-device", DS.RESTSerializer.extend());
902+
let payload = {
903+
doomsdayDevice: {
904+
id: 1,
905+
evilMinion: 2
906+
},
907+
evilMinion: {
908+
id: 2,
909+
doomsdayDevice: 1
910+
}
911+
};
912+
913+
let document = env.store.serializerFor('doomsday-device').normalizeSingleResponse(env.store, DoomsdayDevice, payload);
914+
assert.equal(document.data.relationships.evilMinion.data.id, 2);
915+
assert.equal(document.included.length, 1);
916+
assert.deepEqual(document.included[0], {
917+
attributes: {},
918+
id: '2',
919+
type: 'evil-minion',
920+
relationships: {
921+
doomsdayDevice: {
922+
data: {
923+
id: '1',
924+
type: 'doomsday-device'
925+
}
926+
}
927+
}
928+
});
929+
});
930+
931+
// https://github.com/emberjs/data/issues/3805
932+
test('normalizes sideloaded single record so that it sideloads correctly - hasMany - GH-3805', function(assert) {
933+
env.registry.register("serializer:home-planet", DS.RESTSerializer.extend());
934+
let payload = {
935+
homePlanet: {
936+
id: 1,
937+
superVillains: [2]
938+
},
939+
superVillain: {
940+
id: 2,
941+
homePlanet: 1
942+
}
943+
};
944+
945+
let document = env.store.serializerFor('home-planet').normalizeSingleResponse(env.store, HomePlanet, payload);
946+
947+
assert.equal(document.data.relationships.superVillains.data.length, 1);
948+
assert.equal(document.data.relationships.superVillains.data[0].id, 2);
949+
assert.equal(document.included.length, 1);
950+
assert.deepEqual(document.included[0], {
951+
attributes: {},
952+
id: '2',
953+
type: 'super-villain',
954+
relationships: {
955+
homePlanet: {
956+
data: {
957+
id: '1',
958+
type: 'home-planet'
959+
}
960+
}
961+
}
962+
});
963+
});

0 commit comments

Comments
 (0)