Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

Commit 1e429fb

Browse files
committed
fix nested relationships w/o ids
1 parent d4496e2 commit 1e429fb

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ export const JSONAPIDeserializer = {
7272
resource: JSONApiResource,
7373
resolvedMap: T
7474
): T[K] {
75+
if (!resource.id || !resource.type) {
76+
return;
77+
}
7578
const key = `${resource.id}-${resource.type}`;
7679

7780
return get(resolvedMap, key);
@@ -87,7 +90,6 @@ export const JSONAPIDeserializer = {
8790
const type = data.type;
8891

8992
const resolvedItem = this.getResolvedItem(data, resolvedMap);
90-
9193
if (resolvedItem) {
9294
return resolvedItem;
9395
}

tests/index.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
import { JSONAPIDeserializer, JSONAPISerializer } from "../src";
22

3+
test("JSONAPIDeserializer:compound relationships w/o ids", async () => {
4+
var jsonapiResponse = {
5+
data: {
6+
type: "email-lists",
7+
relationships: {
8+
emails: {
9+
data: [
10+
{
11+
type: "emails",
12+
attributes: {
13+
email: "email-1@mail.com",
14+
},
15+
},
16+
{
17+
type: "emails",
18+
attributes: {
19+
email: "email-2@mail.com",
20+
},
21+
},
22+
],
23+
},
24+
},
25+
},
26+
};
27+
28+
// @ts-ignore @todo check this type
29+
const output = JSONAPIDeserializer.deserialize(jsonapiResponse);
30+
expect(output.data.emails[0].email).toEqual("email-1@mail.com");
31+
expect(output.data.emails[1].email).toEqual("email-2@mail.com");
32+
});
33+
334
test("JSONAPIDeserializer:basic attributes", async () => {
435
const jsonapiResponse = {
536
data: {

0 commit comments

Comments
 (0)