Skip to content

Commit 874cee6

Browse files
authored
Merge pull request #9 from wadetandy/master
Fix bug with deeply nested associations
2 parents 096ee25 + 06fcad9 commit 874cee6

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/util/deserialize.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ class Deserializer {
147147
let hydratedDatum = this.findResource(relationData);
148148
let existing = instance[relationName];
149149
let associated = existing || this.instanceFor(hydratedDatum.type);
150-
this.deserializeInstance(associated, hydratedDatum, nestedIncludeDirective);
150+
151+
associated = this.deserializeInstance(associated, hydratedDatum, nestedIncludeDirective);
151152

152153
if (!existing) {
153154
instance[relationName] = associated;

test/fixtures.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Book extends ApplicationRecord {
4040
title: string = attr();
4141

4242
genre = belongsTo('genres');
43+
author = hasOne('authors')
4344
}
4445

4546
class Genre extends ApplicationRecord {

test/unit/model-test.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ describe('Model', function() {
198198
data: [{
199199
id: '1',
200200
type: 'books'
201+
}, {
202+
id: '2',
203+
type: 'books'
201204
}]
202205
},
203206
bio: {
@@ -217,6 +220,29 @@ describe('Model', function() {
217220
id: '1',
218221
attributes: {
219222
title: "Where's My Butt?"
223+
},
224+
relationships: {
225+
author: {
226+
data: {
227+
id: '2',
228+
type: 'authors'
229+
},
230+
}
231+
}
232+
},
233+
{
234+
type: 'books',
235+
id: '2',
236+
attributes: {
237+
title: "Catcher in the Rye"
238+
},
239+
relationships: {
240+
author: {
241+
data: {
242+
id: '2',
243+
type: 'authors'
244+
},
245+
}
220246
}
221247
},
222248
{
@@ -300,7 +326,7 @@ describe('Model', function() {
300326

301327
it('assigns hasMany relationships correctly', function() {
302328
let instance = Model.fromJsonapi(doc.data, doc);
303-
expect(instance.books.length).to.eq(1);
329+
expect(instance.books.length).to.eq(2);
304330
let book = instance.books[0];
305331
expect(book).to.be.instanceof(Book);
306332
expect(book.title).to.eq("Where's My Butt?");
@@ -330,6 +356,15 @@ describe('Model', function() {
330356
expect(authors[1].firstName).to.eq('Maurice Sendak');
331357
});
332358

359+
it('assigns duplicated nested relationships correctly', function() {
360+
let instance = Model.fromJsonapi(doc.data, doc);
361+
let book1 = instance.books[0];
362+
let book2 = instance.books[1];
363+
364+
expect(book1.author.firstName).to.eq("Maurice Sendak");
365+
expect(book2.author.firstName).to.eq("Maurice Sendak");
366+
});
367+
333368
it('skips relationships without data', function() {
334369
let instance = Model.fromJsonapi(doc.data, doc);
335370
expect(instance.tags).to.eql([]);

0 commit comments

Comments
 (0)