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

Commit 0ce3618

Browse files
committed
fix serialize relationship ids as scalars
1 parent 4344eb7 commit 0ce3618

3 files changed

Lines changed: 21 additions & 3 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.1.0",
3+
"version": "2.1.1",
44
"description": "GOintegro json-api extended serializer/deserializer",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
get,
99
isEmpty,
1010
isNull,
11+
isNumber,
1112
isObject,
13+
isString,
1214
isUndefined,
1315
kebabCase,
1416
keys,
@@ -344,7 +346,7 @@ export class JSONAPISerializer {
344346
}
345347

346348
private serializeEntity(
347-
data: JSONApiResource,
349+
data: any,
348350
config: SerializerConfig,
349351
includedEntities: JSONApiResource[],
350352
serializedEntities: JSONApiResource[],
@@ -532,6 +534,8 @@ export class JSONAPISerializer {
532534

533535
if (v?.id) {
534536
output.id = v.id.toString();
537+
} else if (isString(v) || isNumber(v)) {
538+
output.id = v.toString();
535539
}
536540
return output;
537541
});

tests/post.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import { JSONAPIDeserializer, JSONAPISerializer } from "../src";
22

3-
test("JSONAPISerializer: compound nested", async () => {
3+
test("JSONAPISerializer:compound_nested", async () => {
4+
5+
class UserSerializer extends JSONAPISerializer {
6+
public serializerConfig = () => {
7+
return {
8+
type: "users",
9+
attributes: [],
10+
};
11+
};
12+
}
13+
414
class PostSerializer extends JSONAPISerializer {
515
public serializerConfig = () => {
616
return {
717
type: "posts",
818
attributes: ["content"],
919
relationships: {
1020
poll: { config: new PollSerializer().serializerConfig },
21+
mentions: {config: new UserSerializer().serializerConfig}
1122
},
1223
};
1324
};
@@ -35,6 +46,7 @@ test("JSONAPISerializer: compound nested", async () => {
3546

3647
const postData = {
3748
content: "Test",
49+
mentions: ["1234"],
3850
poll: {
3951
ttl: "3",
4052
options: [
@@ -54,6 +66,8 @@ test("JSONAPISerializer: compound nested", async () => {
5466
compound: true,
5567
});
5668

69+
console.log(JSON.stringify(output));
70+
5771
expect(
5872
output.data.relationships.poll.data.relationships.options.data[0].attributes
5973
.title

0 commit comments

Comments
 (0)