Skip to content

Commit

Permalink
[BUGIFX] Allow createRecord responses without a type
Browse files Browse the repository at this point in the history
  • Loading branch information
igorT committed Jun 24, 2020
1 parent 3c633db commit aa860a4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,46 @@ module('integration/adapter/record_persistence - Persisting Records', function(h
assert.strictEqual(tom.isDeleted, true, 'Tom is marked as deleted');
assert.strictEqual(yehuda.isDeleted, true, 'Yehuda is marked as deleted');
});

test('Create record response does not have to include the type property', async function(assert) {
assert.expect(2);

const Person = Model.extend({
updatedAt: attr('string'),
name: attr('string'),
firstName: attr('string'),
lastName: attr('string'),
});

const ApplicationAdapter = Adapter.extend({
shouldBackgroundReloadRecord: () => false,
});

this.owner.register('model:person', Person);
this.owner.register('adapter:application', ApplicationAdapter);
this.owner.register(
'serializer:application',
JSONAPISerializer.extend({
normalizeResponse: (store, primaryModelClass, payload, id, requestType) => {
return payload;
},
})
);

let store = this.owner.lookup('service:store');
let adapter = store.adapterFor('application');

let tom;

adapter.createRecord = function(_store, type, snapshot) {
assert.strictEqual(type, Person, "The type of the record is 'Person'");
assert.strictEqual(snapshot.record, tom, 'The record in the snapshot is the correct one');

return resolve({ data: { id: '1' } });
};

tom = store.createRecord('person', { name: 'Tom Dale' });

return await tom.save();
});
});
8 changes: 4 additions & 4 deletions packages/store/addon/-private/identifiers/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ function performRecordIdentifierUpdate(
updateFn: UpdateMethod
) {
let { id, lid } = data;
let type = normalizeModelName(data.type);
let type = data.type && normalizeModelName(data.type);

if (DEBUG) {
// get the mutable instance behind our proxy wrapper
Expand Down Expand Up @@ -517,7 +517,7 @@ function performRecordIdentifierUpdate(
}

// TODO consider just ignoring here to allow flexible polymorphic support
if (type !== identifier.type) {
if (type && type !== identifier.type) {
throw new Error(
`The 'type' for a RecordIdentifier cannot be updated once it has been set. Attempted to set type for '${wrapper}' to '${type}'.`
);
Expand Down Expand Up @@ -551,14 +551,14 @@ function detectMerge(

return existingIdentifier !== undefined ? existingIdentifier : false;
} else {
let newType = normalizeModelName(data.type);
let newType = data.type && normalizeModelName(data.type);

// If the ids and type are the same but lid is not the same, we should trigger a merge of the identifiers
if (id !== null && id === newId && newType === type && data.lid && data.lid !== lid) {
let existingIdentifier = lids[data.lid];
return existingIdentifier !== undefined ? existingIdentifier : false;
// If the lids are the same, and ids are the same, but types are different we should trigger a merge of the identifiers
} else if (id !== null && id === newId && newType !== type && data.lid && data.lid === lid) {
} else if (id !== null && id === newId && newType && newType !== type && data.lid && data.lid === lid) {
let keyOptions = getTypeIndex(typesCache, newType);
let existingIdentifier = keyOptions.id[id];
return existingIdentifier !== undefined ? existingIdentifier : false;
Expand Down

0 comments on commit aa860a4

Please sign in to comment.