Skip to content

Commit

Permalink
[BUGFIX identifiers] Address issue with polymorphic findRecord (#7363)
Browse files Browse the repository at this point in the history
* [BUGFIX identifiers] Address issue with polymorphic findRecord

Fixes issue when encountering polymorphic records with a subtype on initial load. If the returned record did not share the asked for type, identifiers cache would get into an incoherent statet.

Co-authored-by: Dmitry Krasnoukhov <[email protected]>
  • Loading branch information
igorT and krasnoukhov committed Dec 1, 2020
1 parent da4fb93 commit 4902d67
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,34 @@ module('Integration | Identifiers - single-table-inheritance polymorphic scenari
store = owner.lookup('service:store');
});

test(`Identity of polymorphic relations can change type`, async function(assert) {
test(`Identity of polymorphic relations can change type on first load`, async function(assert) {
const { owner } = this;
class TestAdapter extends Adapter {
shouldBackgroundReloadRecord() {
return false;
}
findRecord(_, __, id) {
return resolve({
data: {
id,
type: 'ferrari',
attributes: {
color: 'red',
},
},
});
}
}
owner.register('adapter:application', TestAdapter);

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) {
const { owner } = this;
const requests: RID[] = [];
const expectedRequests = [
Expand Down
12 changes: 12 additions & 0 deletions packages/store/addon/-private/identifiers/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { warn } from '@ember/debug';
import { assign } from '@ember/polyfills';
import { DEBUG } from '@glimmer/env';

import coerceId from '../system/coerce-id';
Expand Down Expand Up @@ -343,6 +344,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 (data.type && identifier.type !== normalizeModelName(data.type)) {
let incomingDataResource = 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 4902d67

Please sign in to comment.