nartc / mapper

🔥 An Object-Object AutoMapper for TypeScript 🔥
https://automapperts.netlify.app/
MIT License
961 stars 84 forks source link

Error: Mapping is not found for class BaseEntity and class BaseVo #574

Open zhanglp520 opened 9 months ago

zhanglp520 commented 9 months ago

Is there an existing issue for this?

Describe the issue

Error: Mapping is not found for class BaseEntity { static _OPENAPI_METADATA_FACTORY() { return { id: { required: true, type: () => Number } }; } } and class BaseVo { static _OPENAPI_METADATA_FACTORY() { return { id: { required: true, type: () => Number } }; } }

Models/DTOs/VMs

=====================DO @Entity() export class BaseEntity { @PrimaryGeneratedColumn() @AutoMap() id: number; }

@Entity("dev_products") export class ProductEntity extends BaseEntity { @AutoMap() @Column({ type: "varchar", name: "product_id" }) productId: string; } ======================VO

export class BaseVo { @ApiPropertyOptional({ description: "主键" }) @AutoMap() id: number; } export class ProductVo extends BaseVo { @ApiProperty({ description: "产品编号" }) @AutoMap() productId: string; }

Mapping configuration

import { AutomapperProfile, InjectMapper } from "@automapper/nestjs"; import { Mapper, MappingConfiguration, MappingProfile, createMap, extend, forMember, mapFrom, } from "@automapper/core"; import { Injectable } from "@nestjs/common"; import { ProductEntity } from "../entities/product.entity"; import { ProductVo } from "../vo/product.vo"; import { DeviceEntity } from "../../device/entities/device.entity"; import { DeviceVo } from "../../device/vo/device.vo"; import { BaseVo } from "@/vos/base.dto"; import { BaseEntity } from "@/entities/base.entity";

@Injectable() export class ProductMapperProfile extends AutomapperProfile { constructor(@InjectMapper() mapper: Mapper) { console.log("ProductMapperProfile-mapper", mapper); super(mapper); }

get profile(): MappingProfile { return (mapper: Mapper) => { createMap( mapper, ProductEntity, ProductVo, forMember( (d) => { console.log("zlp-d.devices", d.devices); return d.devices; }, mapFrom((s) => { console.log("zlp-devices", s.devices); const devices = mapper.mapArray(s.devices, DeviceEntity, DeviceVo); return devices; }) ) ); }; }

protected get mappingConfigurations(): MappingConfiguration[] { return [extend(BaseEntity, BaseVo)]; } }

Steps to reproduce

No response

Expected behavior

Mapping successful

Screenshots

No response

Minimum reproduction code

No response

Package

Other package and its version

No response

AutoMapper version

8.7.7

Additional context

No response

zhanglp520 commented 9 months ago

The root cause is the mapping problem where attributes are arrays