Skip to content

Commit 0b37860

Browse files
committed
Merge branch 'master' into release/version-8
2 parents 3bed99d + 5c2ede1 commit 0b37860

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# [8.1.2-beta]
2+
3+
* Merge fixes from version 7.1.2
4+
15
# [8.1.1-beta]
26

37
* Merge fixes from version 7.1.1
@@ -10,6 +14,10 @@
1014

1115
* Allow upgrade to Angular 8
1216

17+
# [7.1.2]
18+
19+
* Fix parsing has many relationships when there is no models provided
20+
1321
# [7.1.1]
1422

1523
* Allow removal of HasMany relations by setting empty array ([#224](https://github.com/ghidoz/angular2-jsonapi/pull/224))

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular2-jsonapi",
3-
"version": "8.1.1-beta",
3+
"version": "8.1.2-beta",
44
"description": "A lightweight Angular 2 adapter for JSON API",
55
"scripts": {
66
"build": "rimraf dist src/compiled && tslint src/**/*.ts && ngc",

src/models/json-api.model.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ describe('JsonApiModel', () => {
129129
});
130130
});
131131

132+
it('should return an empty array for hasMany relationship when one is included without any elements', () => {
133+
const BOOK_NUMBER = 0;
134+
const DATA = getAuthorData('books', BOOK_NUMBER);
135+
author = new Author(datastore, DATA);
136+
author.syncRelationships(DATA, getIncludedBooks(BOOK_NUMBER));
137+
138+
expect(author).toBeDefined();
139+
expect(author.id).toBe(AUTHOR_ID);
140+
expect(author.books).toBeDefined();
141+
expect(author.books.length).toBe(BOOK_NUMBER);
142+
});
143+
132144
it('should parse infinite levels of relationships by reference', () => {
133145
const BOOK_NUMBER = 4;
134146
const DATA = getAuthorData('books', BOOK_NUMBER);

src/models/json-api.model.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class JsonApiModel {
120120
for (const metadata of hasMany) {
121121
const relationship: any = data.relationships ? data.relationships[metadata.relationship] : null;
122122

123-
if (relationship && relationship.data && relationship.data.length > 0) {
123+
if (relationship && relationship.data && Array.isArray(relationship.data)) {
124124
let allModels: JsonApiModel[] = [];
125125
const modelTypesFetched: any = [];
126126

@@ -148,11 +148,9 @@ export class JsonApiModel {
148148
throw { message: `parseHasMany - Model type for relationship ${typeName} not found.` };
149149
}
150150
}
151-
152-
if (allModels.length > 0) {
153-
this[metadata.propertyName] = allModels;
154-
}
155151
}
152+
153+
this[metadata.propertyName] = allModels;
156154
}
157155
}
158156
}

0 commit comments

Comments
 (0)