Skip to content

Commit 6507169

Browse files
committed
fix(mapping): Fixed an issue that would cause @Mapper() to break on Date properties
1 parent 22b0655 commit 6507169

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

packages/nest-utils/src/mapping/services/mapper.test.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ describe('Mapper', () => {
2424
@Map()
2525
bool: boolean = false;
2626

27+
@Map()
28+
date: Date = new Date('2024-10-29T00:00:00');
29+
2730
@Map()
2831
opt: string | undefined = undefined;
2932
}
@@ -35,12 +38,15 @@ describe('Mapper', () => {
3538
@Map()
3639
num: number;
3740

41+
@Map()
42+
date: Date;
43+
3844
@Map()
3945
bool: boolean;
4046
}
4147

4248
const target = mapper.map(new Source(), Source, Target);
43-
expect(target).toEqual({ str: '', num: 0, bool: false });
49+
expect(target).toEqual({ str: '', num: 0, bool: false, date: new Date('2024-10-29T00:00:00') });
4450
});
4551

4652
it("should not map if the source property isn't decorated", () => {

packages/nest-utils/src/mapping/services/mapper.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ export class Mapper {
8383

8484
let value: any;
8585
const sourceType = this.getType(source, propertyKey);
86-
const isNestedProp = sourceType.prototype.toString().includes('object');
86+
let isNestedProp = false;
87+
try {
88+
isNestedProp = sourceType.prototype.toString().includes('object');
89+
} catch (error) {
90+
// Do nothing, isNestedProp will remain false
91+
}
8792
if (isNestedProp && this.getValue(source, propertyKey)) {
8893
// In case the property is a nested property, map it recursively
8994
const targetType = this.getType(target, sourceMetadata.targetKey);

0 commit comments

Comments
 (0)