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] Do not deserialize when a relationship named type exists #3814

Merged
merged 1 commit into from
Oct 3, 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
4 changes: 2 additions & 2 deletions packages/ember-data/lib/serializers/rest-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ var RESTSerializer = JSONSerializer.extend({
let modelClass = store.modelFor(modelName);
let serializer = store.serializerFor(modelName);

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

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

module("integration/serializer/rest - RESTSerializer", {
Expand Down Expand Up @@ -33,14 +33,19 @@ module("integration/serializer/rest - RESTSerializer", {
type: DS.attr('string'),
size: DS.attr('number')
});
Container = DS.Model.extend({
type: DS.belongsTo('basket', { async: true }),
volume: DS.attr('string')
});
env = setupStore({
superVillain: SuperVillain,
homePlanet: HomePlanet,
evilMinion: EvilMinion,
yellowMinion: YellowMinion,
doomsdayDevice: DoomsdayDevice,
comment: Comment,
basket: Basket
basket: Basket,
container: Container
});
env.store.modelFor('super-villain');
env.store.modelFor('home-planet');
Expand All @@ -49,6 +54,7 @@ module("integration/serializer/rest - RESTSerializer", {
env.store.modelFor('doomsday-device');
env.store.modelFor('comment');
env.store.modelFor('basket');
env.store.modelFor('container');
},

teardown: function() {
Expand Down Expand Up @@ -619,3 +625,27 @@ test("don't polymorphically deserialize base on the type key in payload when a t
strictEqual(clashingRecord.get('type'), 'yellowMinion');
strictEqual(clashingRecord.get('size'), 10);
});

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

env.adapter.findRecord = () => {
return {
containers: [{ id: 42, volume: '10 liters', type: 1 }],
baskets: [{ id: 1, size: 4 }]
};
};

run(function() {
env.store.findRecord('container', 42).then((container) => {
strictEqual(container.get('volume'), '10 liters');
return container.get('type');
}).then((basket) => {
ok(basket instanceof Basket);
equal(basket.get('size'), 4);
});
});

});