mapper icon indicating copy to clipboard operation
mapper copied to clipboard

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

Open zhanglp520 opened this issue 2 years ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

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

  • [ ] I don't know.
  • [X] @automapper/core
  • [X] @automapper/classes
  • [X] @automapper/nestjs
  • [ ] @automapper/pojos
  • [ ] @automapper/mikro
  • [ ] @automapper/sequelize
  • [ ] Other (see below)

Other package and its version

No response

AutoMapper version

8.7.7

Additional context

No response

zhanglp520 avatar Sep 14 '23 01:09 zhanglp520

The root cause is the mapping problem where attributes are arrays

zhanglp520 avatar Sep 14 '23 01:09 zhanglp520