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 beta] Attribute/relationship named "type" of primary record i… #3846

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

const primaryHasTypeAttribute = modelHasAttributeOrRelationshipNamedType(modelClass);
/*jshint loopfunc:true*/
arrayHash.forEach((hash) => {
let { data, included } = this._normalizePolymorphicRecord(store, hash, prop, modelClass, serializer, primaryHasTypeAttribute);
let { data, included } = this._normalizePolymorphicRecord(store, hash, prop, modelClass, serializer);
documentHash.data.push(data);
if (included) {
documentHash.included.push(...included);
Expand All @@ -162,8 +161,9 @@ var RESTSerializer = JSONSerializer.extend({
return documentHash;
},

_normalizePolymorphicRecord: function(store, hash, prop, primaryModelClass, primarySerializer, primaryHasTypeAttribute) {
_normalizePolymorphicRecord: function(store, hash, prop, primaryModelClass, primarySerializer) {
let serializer, modelClass;
const primaryHasTypeAttribute = modelHasAttributeOrRelationshipNamedType(primaryModelClass);
// Support polymorphic records in async relationships
if (!primaryHasTypeAttribute && hash.type && store._hasModelFor(this.modelNameFromPayloadKey(hash.type))) {
serializer = store.serializerFor(hash.type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,12 +607,12 @@ test("don't polymorphically deserialize base on the type key in payload when a t
}));

run(function() {
env.restSerializer.normalizeArrayResponse(env.store, Basket, {
env.store.push(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' })
{ type: 'bamboo', size: 10, id: '1' },
{ type: 'yellowMinion', size: 10, id: '65536' }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for not using env.store.createRecord anymore here!

]
});
}));
});

const normalRecord = env.store.peekRecord('basket', '1');
Expand All @@ -626,6 +626,24 @@ test("don't polymorphically deserialize base on the type key in payload when a t
strictEqual(clashingRecord.get('size'), 10);
});

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

run(function() {
env.store.push(env.restSerializer.normalizeSingleResponse(env.store, Basket, {
basket: { type: 'yellowMinion', size: 10, id: '65536' }
}, '65536'));
});

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);
});


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
Expand Down