Skip to content

Commit 0cd39ed

Browse files
fix(Transform): transform object with null prototype
1 parent ac92126 commit 0cd39ed

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/TransformOperationExecutor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class TransformOperationExecutor {
7474

7575
return new Date(value);
7676

77-
} else if (value instanceof Object) {
77+
} else if (typeof value === "object" && value !== null) {
7878

7979
// try to guess the type
8080
if (!targetType && value.constructor !== Object/* && TransformationType === TransformationType.CLASS_TO_PLAIN*/) targetType = value.constructor;

test/functional/basic-functionality.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ describe("basic functionality", () => {
15551555

15561556
});
15571557

1558-
it("should be able to transform array too", () => {
1558+
it("should transform array", () => {
15591559
defaultMetadataStorage.clear();
15601560

15611561

@@ -1681,4 +1681,17 @@ describe("basic functionality", () => {
16811681

16821682
});
16831683

1684+
it("should transform objects with null prototype", () => {
1685+
class TestClass {
1686+
prop: string;
1687+
}
1688+
1689+
const obj = Object.create(null);
1690+
obj.a = "JS FTW";
1691+
1692+
const transformedClass = plainToClass(TestClass, obj);
1693+
1694+
transformedClass.should.be.instanceOf(TestClass);
1695+
});
1696+
16841697
});

0 commit comments

Comments
 (0)