Skip to content

Commit f42f4b9

Browse files
author
Lee Richmond
committed
Handle null attributes correctly
1 parent a23b088 commit f42f4b9

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/attribute.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default class Attribute {
4444
},
4545

4646
set(value) : void {
47-
if (!value.hasOwnProperty('isAttr')) {
47+
if (!value || !value.hasOwnProperty('isAttr')) {
4848
attr.setter(this, value);
4949
}
5050
}

test/fixtures.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ let Author = Person.extend({
1818
jsonapiType: 'authors'
1919
},
2020

21+
nilly: attr(),
22+
2123
multiWords: hasMany('multi_words'),
2224
books: hasMany(),
2325
tags: hasMany(),

test/unit/model-test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ describe('Model', function() {
1414
type: 'authors',
1515
attributes: {
1616
firstName: 'Donald Budge',
17-
unknown: 'adsf'
17+
unknown: 'adsf',
18+
nilly: null
1819
},
1920
relationships: {
2021
unknownrelationship: {
@@ -107,7 +108,8 @@ describe('Model', function() {
107108
let instance = Model.fromJsonapi(doc.data, doc);
108109
expect(instance.firstName).to.eq('Donald Budge');
109110
expect(instance.attributes).to.eql({
110-
firstName: 'Donald Budge'
111+
firstName: 'Donald Budge',
112+
nilly: null
111113
})
112114
});
113115

@@ -126,6 +128,11 @@ describe('Model', function() {
126128
expect(instance).to.not.have.property('unknownrelationship');
127129
});
128130

131+
it('does not blow up on null attributes', function() {
132+
let instance = Model.fromJsonapi(doc.data, doc);
133+
expect(instance.nilly).to.eq(null);
134+
});
135+
129136
it('assigns metadata correctly', function() {
130137
let instance = Model.fromJsonapi(doc.data, doc);
131138
expect(instance.__meta__).to.eql({

0 commit comments

Comments
 (0)