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 identifiers] Address issue with polymorphic findRecord #7325

Closed
wants to merge 4 commits into from
Closed
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
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');
krasnoukhov marked this conversation as resolved.
Show resolved Hide resolved

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
3 changes: 2 additions & 1 deletion packages/store/addon/-private/system/core-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,8 @@ abstract class CoreStore extends Service {
return promise.then(
payload => {
// ensure that regardless of id returned we assign to the correct record
if (payload.data && !Array.isArray(payload.data)) {
// for polymorphic, if type does not match, we'll need a new identifier
if (payload.data && !Array.isArray(payload.data) && payload.data.type === identifier.type) {
payload.data.lid = identifier.lid;
}

Expand Down
5 changes: 4 additions & 1 deletion packages/store/addon/-private/system/store/finders.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ export function _find(adapter, store, modelClass, id, internalModel, options) {
);

// ensure that regardless of id returned we assign to the correct record
payload.data.lid = identifier.lid;
// for polymorphic, if type does not match, we'll need a new identifier
if (payload.data.type === identifier.type) {
payload.data.lid = identifier.lid;
}

return store._push(payload);
},
Expand Down