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

Commit ace9d9f

Browse files
authored
Merge pull request #3 from GoIntegro/fix_undefined_relationships
fix undefined relationship
2 parents ef0c26a + db70be5 commit ace9d9f

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,8 @@ export class JSONAPISerializer {
484484
if (value?.id) {
485485
output.id = value.id;
486486
}
487-
serializedRelationships[kebabCase(property)] = isNull(value)
488-
? null
489-
: output;
487+
serializedRelationships[kebabCase(property)] =
488+
isNull(value) || isUndefined(value) ? null : output;
490489
}
491490
}
492491
}

tests/index.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,33 @@ test("JSONAPIDeserializer: polymorphic megapost", async () => {
634634

635635
expect(data.items[0].author.profile.avatar.url).toEqual("avatar-1.jpg");
636636
});
637+
638+
test("JSONAPISerializer: null relationship with data null", async () => {
639+
const inputData = {
640+
id: "1",
641+
name: "El discurso del rey",
642+
duration: 120,
643+
director: null,
644+
};
645+
646+
const movieSerializer = new MovieSerializer();
647+
const output = movieSerializer.serialize({ data: inputData });
648+
const { data } = output;
649+
650+
expect(data.relationships.director.data).toBeNull();
651+
});
652+
653+
test("JSONAPISerializer: undefined relationship with data null", async () => {
654+
const inputData = {
655+
id: "1",
656+
name: "El discurso del rey",
657+
duration: 120,
658+
director: undefined,
659+
};
660+
661+
const movieSerializer = new MovieSerializer();
662+
const output = movieSerializer.serialize({ data: inputData });
663+
const { data } = output;
664+
665+
expect(data.relationships.director.data).toBeNull();
666+
});

0 commit comments

Comments
 (0)