Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoMap doesn't work with union types #600

Open
Abd3lwahab opened this issue Mar 7, 2024 · 0 comments
Open

AutoMap doesn't work with union types #600

Abd3lwahab opened this issue Mar 7, 2024 · 0 comments

Comments

@Abd3lwahab
Copy link

Abd3lwahab commented Mar 7, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant