Skip to content

Commit

Permalink
Do not assign lid when type does not match
Browse files Browse the repository at this point in the history
Add test for cache

Alternaive solution to 7325
  • Loading branch information
krasnoukhov authored and igorT committed Nov 30, 2020
1 parent b0b8840 commit 897bdb4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ module('Integration | Identifiers - single-table-inheritance polymorphic scenari

const foundFerrari = await store.findRecord('car', '1');
assert.strictEqual(foundFerrari.constructor.modelName, 'ferrari', 'We found the right type');

const cachedFerrari = await store.peekRecord('ferrari', '1');
assert.strictEqual(cachedFerrari.constructor.modelName, 'ferrari', 'We cached the right type');
});

test(`Identity of polymorphic relations can change type when in cache`, async function(assert) {
Expand Down
11 changes: 11 additions & 0 deletions packages/store/addon/-private/identifiers/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,17 @@ export class IdentifierCache {
let newId = coerceId(data.id);
let existingIdentifier = detectMerge(this._cache.types, identifier, data, newId, this._cache.lids);

if (!existingIdentifier) {
// If the incoming type does not match the identifier type, we need to create an identifier for the incoming
// data so we can merge the incoming data with the existing identifier, see #7325 and #7363
if (identifier.type !== data.type && data.type) {
let incomingDataResource = Object.assign({}, data);
// Need to strip the lid from the incomingData in order force a new identifier creation
delete incomingDataResource.lid;
existingIdentifier = this.getOrCreateRecordIdentifier(incomingDataResource);
}
}

if (existingIdentifier) {
let keyOptions = getTypeIndex(this._cache.types, identifier.type);
identifier = this._mergeRecordIdentifiers(keyOptions, identifier, existingIdentifier, data, newId as string);
Expand Down

0 comments on commit 897bdb4

Please sign in to comment.