Skip to content

AutoMap doesn't work with union types #600

Open
@Abd3lwahab

Description

This from the source code of @automapper/classes

        if (!options.type) {
            const designTypeMeta = Reflect.getMetadata(
                'design:type',
                target,
                propertyKey
            );
            // only store design:type metadata if it's not Array or Object
            if (
                designTypeMeta &&
                designTypeMeta !== Array &&
                designTypeMeta !== Object
            ) {
                options.type = () => designTypeMeta;
            }
        }

packages/classes/src/lib/automap.ts

Why this ignore the type of Object and Array?

This causing the types with union types to be ignored because the design:type metadata of them are Object

if I have this property in entity:

  @AutoMap()
  @Column('character varying', { name: 'name', nullable: true })
  name: string | null;

this will be the metadata of it "from JS build code of my app"

tslib_1.__decorate([
    (0, classes_1.AutoMap)(),
    (0, typeorm_1.Column)('character varying', { name: 'name', nullable: true }),
    tslib_1.__metadata("design:type", Object)
], Product.prototype, "name", void 0);

I tried to remove that check from @automapper/classes and that property mapped correctly with @AutoMap

I think I am missing the reason why this check added in the first place so Idk if this safe to do.

It will work fine also if I added any constructor to the @AutoMap() no matter what was the type

  @AutoMap(() => String)
  @Column('character varying', { name: 'name', nullable: true })
  name: string | null;

OR

  @AutoMap(() => Number)
  @Column('character varying', { name: 'name', nullable: true })
  name: string | null;

OR

  @AutoMap(() => Object)
  @Column('character varying', { name: 'name', nullable: true })
  name: string | null;

All this solution I tried mapped the property correctly.
So idk what this constructor means to do?

I am enabling strict and strictNullChecks in tsconfig

I am willing to create any PR fix in this issue as the discussion will lead to.

Thanks.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions