nartc / mapper

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

Consistent way to ignore properties #519

Open RaviVadera opened 1 year ago

RaviVadera commented 1 year ago

Is there an existing issue for this?

Describe the issue

I am using transformer-plugin as instructed from the documentation with,

I found out that the automapper still requires to explicitly define mapping between classes which is kind of expected thing probably. I am using the mappings with mapping profiles with ignore mapping configuration to ignore some properties.

These ignore configurations do not work as I have to use JSDoc comment to ignore the properties. Is there a chance to have a consistent way to ignore the properties (preferably with profiles as those are already needed)?

Maybe another bug - the reason to use ignore configuration in first place

The transformer plugin fails for type ObjectID (for TypeORM - Mongo) which somehow uses date type internally with error,

return Object.getPrototypeOf(value) === date || value === Date;

....@automapper/core/index.cjs:118
TypeError: Cannot convert undefined or null to object
    at Function.getPrototypeOf(<anonymous>)
....

Models/DTOs/VMs

@Entity()
export default class Person {

  @ObjectIdColumn()
  id: ObjectId;

  ..
}

Mapping configuration

No response

Steps to reproduce

In case of ObjectID,

  1. npm i
  2. npm start:dev
  3. See error

Expected behavior

Screenshots

No response

Minimum reproduction code

No response

Package

Other package and its version

No response

AutoMapper version

8.7.7

Additional context

Linux Node 16

GwendalBroudin-Emoko commented 1 year ago

I agree, there are some issue this the generated JS when using some imported types and generics. Here is a quick example typegoose:

Source typescript:

import { prop, Ref } from "@typegoose/typegoose";
import { AutoMap } from "@automapper/classes";
import mongoose from "mongoose";

export class Car {}

export class Person {
  @prop({ ref: () => Car })
  @AutoMap(() => Car)
  public car: Ref<Car, mongoose.Types.ObjectId>;
}

Resulting Javascript :

const typegoose_1 = require("@typegoose/typegoose");
const classes_1 = require("@automapper/classes");
class Car {
}
exports.Car = Car;
class Person {
    static __AUTOMAPPER_METADATA_FACTORY__() {
        return [
            //Produce the error "Identifier expected.ts(1003)"
           ["car", { type: () => require("./node_modules/.pnpm/@typegoose+typegoose@9.13.2_mongoose@6.7.5/node_modules/@typegoose/typegoose/lib/types").Ref<import("/sources/emoko-back-next/testing/typegoose/models.entity").Car, import("mongoose").Types.ObjectId>, depth: 1 }]
     ];
    }
}
__decorate([
    (0, typegoose_1.prop)({ ref: () => Car }),
    (0, classes_1.AutoMap)(() => Car),
    __metadata("design:type", Object)
], Person.prototype, "car", void 0);

For now, I use this solution to avoid those errors:

/**
   * @autoMapIgnore
   */
  @AutoMap(() => Car)
  public car: Ref<Car, mongoose.Types.ObjectId>; 

@nartc I guess a quick solution would be to have the transfomer plugin ignoring entirely the property when there is an AutoMap decorator.