I have been utilizing the automapper-ts with typescript plugin for automatic mapping. Check it out here
While it works smoothly for simple objects, I encountered issues when dealing with complex ones like: Record<string, any>
or Map<string, AnotherObject>
.
- Here is an example involving
Map<string, SampleItem>
:
export class SampleItem {
type: string;
name: string;
example?: unknown;
constructor(data?: SampleItem) {
Object.assign(this, data);
}
}
class Entity {
@AutoMap(() => SampleItem)
sample: Map<string, SampleItem>;
@AutoMap(() => Object)
details: Record<string, any>;
}
This presents a test scenario comparing both situations:
The issue arises from the following payload:
{
sample: {
sampleOne: {
name: 'sample name',
type: SampleTypes.STRING,
example: 'sample example',
} as SampleItem,
},
description: 'description'
}
- Another complication occurred when integrating a complex object with the
details
field:
{
description: { des: 'description' } as Record<string, any>,
}
Original error: Error: Mapping is not found for function String() { [native code] } and function String() { [native code] }
at setMember (/home/fabiofilho/Projects/project/node_modules/@automapper/core/index.cjs:379:15)
at map (/home/fabiofilho/Projects/project/node_modules/@automapper/core/index.cjs:439:9)
at mapReturn (/home/fabiofilho/Projects/project/node_modules/@automapper/core/index.cjs:291:10)
at Proxy.<anonymous> (/home/fabiofilho/Projects/project/node_modules/@automapper/core/index.cjs:691:31)
Additional information - using the ttypescript:
"dependencies": {
"@automapper/classes": "^8.7.5",
"@automapper/core": "^8.7.5",
"@automapper/mikro": "^8.7.5",
"@automapper/nestjs": "^8.7.5",
"@automapper/pojos": "^8.7.5",
"@automapper/sequelize": "^8.7.5",
}
module.exports = {
[...]
globals: {
'ts-jest': {
compiler: 'ttypescript',
},
},
};