Skip to content

Commit

Permalink
Merge pull request #713 from orbitjs/backport-fixes
Browse files Browse the repository at this point in the history
Backport fixes to v0.16
  • Loading branch information
dgeb authored Dec 10, 2019
2 parents 18b5d0b + 863e9c8 commit 160c5fd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/@orbit/data/src/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,12 @@ function updateRecordReplaceAttribute(
function updateRecordReplaceHasOne(
record: Record,
relationship: string,
relatedRecord: RecordIdentity
relatedRecord: RecordIdentity | null
) {
deepSet(
record,
['relationships', relationship, 'data'],
cloneRecordIdentity(relatedRecord)
relatedRecord ? cloneRecordIdentity(relatedRecord) : null
);
}

Expand Down
36 changes: 36 additions & 0 deletions packages/@orbit/data/test/operation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,42 @@ module('Operation', function() {
);
});

test('can coalesce replaceAttribute + replaceRelatedRecord with null', function(assert) {
assert.deepEqual(
coalesceRecordOperations([
{
op: 'replaceRelatedRecord',
record: { type: 'contact', id: '1234' },
relationship: 'address',
relatedRecord: null
},
{
op: 'replaceAttribute',
record: { type: 'contact', id: '1234' },
attribute: 'name',
value: 'James'
}
]),
[
{
op: 'updateRecord',
record: {
type: 'contact',
id: '1234',
attributes: {
name: 'James'
},
relationships: {
address: {
data: null
}
}
}
}
]
);
});

test('can coalesce addRecord + addToRelatedRecords for the same record', function(assert) {
assert.deepEqual(
coalesceRecordOperations([
Expand Down
5 changes: 4 additions & 1 deletion packages/@orbit/jsonapi/src/jsonapi-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ export class JSONAPISerializer
}
const serializer = this._serializers[attrOptions.type];
if (serializer) {
value = serializer.serialize(value, attrOptions.serializationOptions);
value =
value === null
? null
: serializer.serialize(value, attrOptions.serializationOptions);
}
deepSet(
resource,
Expand Down
32 changes: 32 additions & 0 deletions packages/@orbit/jsonapi/test/jsonapi-serializer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,38 @@ module('JSONAPISerializer', function(hooks) {
);
});

test('#serialize will pass null values without throwing', function(assert) {
assert.deepEqual(
serializer.serialize({
data: {
type: 'person',
id: '123',
attributes: {
name: null,
birthday: null,
birthtime: null,
height: null, // meters
isAdult: null
}
}
}),
{
data: {
type: 'persons',
id: '123',
attributes: {
name: null,
birthday: null,
birthtime: null,
height: null, // cm (with 2 digits)
'is-adult': null
}
}
},
'serialized document matches'
);
});

test('#deserialize will use available serializers for attribute values', function(assert) {
let result = serializer.deserialize({
data: {
Expand Down

0 comments on commit 160c5fd

Please sign in to comment.