Skip to content

Commit fc6e905

Browse files
Stanley Stuartbmac
Stanley Stuart
authored andcommitted
[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 (cherry picked from commit c68d0ef)
1 parent 52f17f1 commit fc6e905

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

addon/serializers/rest.js

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

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

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

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ module("integration/serializer/rest - RESTSerializer", {
2222
});
2323
EvilMinion = DS.Model.extend({
2424
superVillain: DS.belongsTo('super-villain', { async: false }),
25-
name: DS.attr('string')
25+
name: DS.attr('string'),
26+
doomsdayDevice: DS.belongsTo('doomsday-device', { async: false })
2627
});
2728
YellowMinion = EvilMinion.extend({
2829
eyes: DS.attr('number')
@@ -846,3 +847,93 @@ test("don't polymorphically deserialize based on the type key in payload when a
846847
});
847848

848849
});
850+
851+
test('Serializer should respect the attrs hash in links', function(assert) {
852+
env.registry.register("serializer:super-villain", DS.RESTSerializer.extend({
853+
attrs: {
854+
evilMinions: { key: 'my_minions' }
855+
}
856+
}));
857+
858+
var jsonHash = {
859+
"super-villains": [
860+
{
861+
firstName: 'Tom',
862+
lastName: 'Dale',
863+
links: {
864+
my_minions: 'me/minions'
865+
}
866+
}
867+
]
868+
};
869+
870+
var documentHash = env.container.lookup("serializer:super-villain").normalizeSingleResponse(env.store, SuperVillain, jsonHash);
871+
872+
assert.equal(documentHash.data.relationships.evilMinions.links.related, 'me/minions');
873+
});
874+
875+
// https://github.com/emberjs/data/issues/3805
876+
test('normalizes sideloaded single record so that it sideloads correctly - belongsTo - GH-3805', function(assert) {
877+
env.registry.register("serializer:doomsday-device", DS.RESTSerializer.extend());
878+
let payload = {
879+
doomsdayDevice: {
880+
id: 1,
881+
evilMinion: 2
882+
},
883+
evilMinion: {
884+
id: 2,
885+
doomsdayDevice: 1
886+
}
887+
};
888+
889+
let document = env.store.serializerFor('doomsday-device').normalizeSingleResponse(env.store, DoomsdayDevice, payload);
890+
assert.equal(document.data.relationships.evilMinion.data.id, 2);
891+
assert.equal(document.included.length, 1);
892+
assert.deepEqual(document.included[0], {
893+
attributes: {},
894+
id: '2',
895+
type: 'evil-minion',
896+
relationships: {
897+
doomsdayDevice: {
898+
data: {
899+
id: '1',
900+
type: 'doomsday-device'
901+
}
902+
}
903+
}
904+
});
905+
});
906+
907+
// https://github.com/emberjs/data/issues/3805
908+
test('normalizes sideloaded single record so that it sideloads correctly - hasMany - GH-3805', function(assert) {
909+
env.registry.register("serializer:home-planet", DS.RESTSerializer.extend());
910+
let payload = {
911+
homePlanet: {
912+
id: 1,
913+
superVillains: [2]
914+
},
915+
superVillain: {
916+
id: 2,
917+
homePlanet: 1
918+
}
919+
};
920+
921+
let document = env.store.serializerFor('home-planet').normalizeSingleResponse(env.store, HomePlanet, payload);
922+
923+
assert.equal(document.data.relationships.superVillains.data.length, 1);
924+
assert.equal(document.data.relationships.superVillains.data[0].id, 2);
925+
assert.equal(document.included.length, 1);
926+
assert.deepEqual(document.included[0], {
927+
attributes: {},
928+
id: '2',
929+
type: 'super-villain',
930+
relationships: {
931+
homePlanet: {
932+
data: {
933+
id: '1',
934+
type: 'home-planet'
935+
}
936+
}
937+
}
938+
});
939+
});

0 commit comments

Comments
 (0)