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

[BUGFIX release-1-13] Disable polymorphic deserialization when a mode… #3725

Merged
merged 1 commit into from
Sep 10, 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
7 changes: 4 additions & 3 deletions packages/ember-data/lib/serializers/rest-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,10 @@ var RESTSerializer = JSONSerializer.extend({

Ember.assert(`${this.toString()} has opted into the new serializer API and expects the ${serializer.toString()} it collaborates with to also support the new serializer API by setting its \`isNewSerializerAPI\` property to true.`, get(serializer, 'isNewSerializerAPI'));

const primaryHasTypeAttribute = get(modelClass, 'attributes').get('type');
/*jshint loopfunc:true*/
forEach.call(arrayHash, (hash) => {
let { data, included } = this._normalizePolymorphicRecord(store, hash, prop, modelClass, serializer);
let { data, included } = this._normalizePolymorphicRecord(store, hash, prop, modelClass, serializer, primaryHasTypeAttribute);
documentHash.data.push(data);
if (included) {
documentHash.included.push(...included);
Expand All @@ -240,10 +241,10 @@ var RESTSerializer = JSONSerializer.extend({
return documentHash;
},

_normalizePolymorphicRecord(store, hash, prop, primaryModelClass, primarySerializer) {
_normalizePolymorphicRecord(store, hash, prop, primaryModelClass, primarySerializer, primaryHasTypeAttribute) {
let serializer, modelClass;
// Support polymorphic records in async relationships
if (hash.type && store._hasModelFor(this.modelNameFromPayloadKey(hash.type))) {
if (!primaryHasTypeAttribute && hash.type && store._hasModelFor(this.modelNameFromPayloadKey(hash.type))) {
serializer = store.serializerFor(hash.type);
modelClass = store.modelFor(hash.type);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var get = Ember.get;
var HomePlanet, league, SuperVillain, EvilMinion, YellowMinion, DoomsdayDevice, Comment, env;
var HomePlanet, league, SuperVillain, EvilMinion, YellowMinion, DoomsdayDevice, Comment, Basket, env;
var run = Ember.run;

module("integration/serializer/rest - RESTSerializer", {
Expand Down Expand Up @@ -30,20 +30,26 @@ module("integration/serializer/rest - RESTSerializer", {
root: DS.attr('boolean'),
children: DS.hasMany('comment', { inverse: null, async: false })
});
Basket = DS.Model.extend({
type: DS.attr('string'),
size: DS.attr('number')
});
env = setupStore({
superVillain: SuperVillain,
homePlanet: HomePlanet,
evilMinion: EvilMinion,
yellowMinion: YellowMinion,
doomsdayDevice: DoomsdayDevice,
comment: Comment
comment: Comment,
basket: Basket
});
env.store.modelFor('super-villain');
env.store.modelFor('home-planet');
env.store.modelFor('evil-minion');
env.store.modelFor('yellow-minion');
env.store.modelFor('doomsday-device');
env.store.modelFor('comment');
env.store.modelFor('basket');
},

teardown: function() {
Expand Down Expand Up @@ -513,3 +519,28 @@ test("normalizeResponse can load secondary records of the same type without affe
}]
});
});

test("don't polymorphically deserialize base on the type key in payload when a type attribute exist", function() {
env.registry.register('serializer:application', DS.RESTSerializer.extend({
isNewSerializerAPI: true
}));

run(function() {
env.restSerializer.normalizeArrayResponse(env.store, Basket, {
basket: [
env.store.createRecord('Basket', { type: 'bamboo', size: 10, id: '1' }),
env.store.createRecord('Basket', { type: 'yellowMinion', size: 10, id: '65536' })
]
});
});

const normalRecord = env.store.peekRecord('basket', '1');
ok(normalRecord, "payload with type that doesn't exist");
strictEqual(normalRecord.get('type'), 'bamboo');
strictEqual(normalRecord.get('size'), 10);

const clashingRecord = env.store.peekRecord('basket', '65536');
ok(clashingRecord, 'payload with type that matches another model name');
strictEqual(clashingRecord.get('type'), 'yellowMinion');
strictEqual(clashingRecord.get('size'), 10);
});