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

Commit eae589b

Browse files
committed
fix compound nested serialize
1 parent ce87d52 commit eae589b

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-jsonapi-serializer",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "GOintegro json-api extended serializer/deserializer",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,11 @@ export class JSONAPISerializer {
355355
const entityId = get(data, "id");
356356
const entityType = get(data, "type") || config.type;
357357

358+
// detect optional whitelist processing
359+
// if compound is true and includeWhitelistKeys is not defined should compound everything
360+
const shouldProcessIncludedOrCompound =
361+
includeWhitelistKeys || (!includeWhitelistKeys && compound);
362+
358363
if (entityId) {
359364
if (this.isEntitySerialized(serializedEntities, entityId, entityType)) {
360365
return this.getSerializedEntity(
@@ -413,7 +418,7 @@ export class JSONAPISerializer {
413418
forIn(relationships, (value, dashCaseKey) => {
414419
const key = camelCase(dashCaseKey);
415420

416-
if (!includeWhitelistKeys) {
421+
if (!shouldProcessIncludedOrCompound) {
417422
return;
418423
}
419424

tests/post.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { JSONAPIDeserializer, JSONAPISerializer } from "../src";
2+
3+
test("JSONAPISerializer: compound nested", async () => {
4+
class PostSerializer extends JSONAPISerializer {
5+
public serializerConfig = () => {
6+
return {
7+
type: "posts",
8+
attributes: ["content"],
9+
relationships: {
10+
poll: { config: new PollSerializer().serializerConfig },
11+
},
12+
};
13+
};
14+
}
15+
class PollSerializer extends JSONAPISerializer {
16+
public serializerConfig = () => {
17+
return {
18+
type: "polls",
19+
attributes: ["ttl"],
20+
relationships: {
21+
options: { config: new PollOptionSerializer().serializerConfig },
22+
},
23+
};
24+
};
25+
}
26+
27+
class PollOptionSerializer extends JSONAPISerializer {
28+
public serializerConfig = () => {
29+
return {
30+
type: "poll-options",
31+
attributes: ["title"],
32+
};
33+
};
34+
}
35+
36+
const postData = {
37+
content: "Test",
38+
poll: {
39+
ttl: "3",
40+
options: [
41+
{
42+
title: "Test 1",
43+
},
44+
{
45+
title: "Test 2",
46+
},
47+
],
48+
},
49+
};
50+
51+
const postSerializer = new PostSerializer();
52+
const output = postSerializer.serialize({
53+
data: postData,
54+
compound: true,
55+
});
56+
57+
expect(
58+
output.data.relationships.poll.data.relationships.options.data[0].attributes
59+
.title
60+
).toEqual("Test 1");
61+
});

0 commit comments

Comments
 (0)