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

class-transformer decorators are ignored #87

Open
qmphan opened this issue Jan 7, 2020 · 6 comments
Open

class-transformer decorators are ignored #87

qmphan opened this issue Jan 7, 2020 · 6 comments
Labels

Comments

@qmphan
Copy link

qmphan commented Jan 7, 2020

Hello,

Thank you for this nice and handy library!

I have entity with decorators (I use DateTime type from luxon) and had problem because attribute of type DateTime cannot be created with new DateTime(). In luxon, you have to use DateTime.local() or DateTime.utc() to create new instances.

So I have write some decorators to get around this. However, my decorators are ignored when fixtures are transformed due to the parameter ignoreDecorators set to true in the call to plainToClassFromExist in Builder.js

if (typeof processorInstance.preProcess === 'function') {
                    data = yield processorInstance.preProcess(fixture.name, data);
                }
                entity = class_transformer_1.plainToClassFromExist(entity, data, { ignoreDecorators: false });
                yield callExecutors();
                /* istanbul ignore else */
                if (typeof processorInstance.postProcess === 'function') {
                    yield processorInstance.postProcess(fixture.name, entity);
                }

What are the reasons for setting ignoreDecorators to true? Could you make it optional?

@RobinCK RobinCK added the 2.0 label Mar 11, 2020
@RobinCK RobinCK mentioned this issue Mar 11, 2020
12 tasks
@RobinCK
Copy link
Owner

RobinCK commented Mar 11, 2020

thanks. added to Roadmap

@RobinCK
Copy link
Owner

RobinCK commented Dec 25, 2020

fixed in 1.9.0 version

@RobinCK RobinCK closed this as completed Dec 25, 2020
@aleksandryackovlev
Copy link

This fix breaks the behavior that I use in my code.

I've got user.entity with the Exclude decorator for passwords:

@Entity()
export class User {
  @Column({
    type: 'varchar',
    length: 150,
    update: false,
    unique: true,
  })
  username: string;

  @Exclude()
  @Column()
  password: string;

  @BeforeInsert()
  async hashPassword() {
    this.password = await bcrypt.hash(this.password, 10);
  }
}

And my fixture looks like this:

entity: User
parameters: {}
items:
  user1:
    username: '{{internet.userName}}'
    password: 'test'
    __call:
      hashPassword:
  user2:
    username: '{{internet.userName}}'
    password: 'test'
    __call:
      hashPassword:

Before the fix everything worked just fine. But now this.passwordis undefinded inside the hashPassword, and it causes an error during the creation of fixtures.

Considering the fact that decorators are not ignored anymore, I removed the __call params from my fixtures, but it doesn't work either.

@RobinCK Isn't it going to be a better solution to allow users to ignore decorators by some command-line option?

@RobinCK RobinCK reopened this Jan 9, 2021
@RobinCK
Copy link
Owner

RobinCK commented Jan 9, 2021

@aleksandryackovlev
Yes, you are right, I need to move this into the entity configuration

@phillamarmotte
Copy link

phillamarmotte commented Jan 20, 2021

Hi, first of all, thank you for this lib.
Is it possible to do something like this, so everyone (inculded me) will be happy

in he yaml file

entity: User
ignoreDecorators: true
items:
  user1:
    username: foo
    password: test

then add the key in jFixturesSchema.js

ignoreDecorators: Joi.boolean(),

add it to in Resolver.js

l15  for (const { entity, items, parameters, processor, resolvedFields, ignoreDecorators } of fixtureConfigs) {
l29  for (const name of referenceNames) {
                    const data = Object.assign({}, propertyList);
                    this.stack.push({
                        parameters: parameters || {},
                        processor,
                        entity: entity,
                        name: name,
                        resolvedFields,
                        data,
                        dependencies: this.resolveDependencies(name, data),
                        ignoreDecorators
                    });

and finaly in Builder.js

add method

function isIgnoreDecorators(fixture) {
    return !!fixture.ignoreDecorators
}

then use it to configure the ignoreDecorators parameter

entity = class_transformer_1.plainToClassFromExist(entity, data, { ignoreDecorators: isIgnoreDecorators(fixture) });

This way it doesn't break anything and you can disable decorator for specific entities

@RobinCK I find this way more convenient than a cli option.

leosuncin added a commit to leosuncin/nest-auth-example that referenced this issue Jan 31, 2021
Lock typeorm-fixtures-cli to 1.8.1 version

SEE RobinCK/typeorm-fixtures#87
@wodka
Copy link

wodka commented Sep 11, 2022

we are having the problem that they are not ignored - even though setting ignoreDecorators to true. It will not add any field to an object that has the @Exclude() annotation on the class

Blue-BigTech added a commit to Blue-BigTech/Auth-Template-Nest.js that referenced this issue Jun 11, 2023
Lock typeorm-fixtures-cli to 1.8.1 version

SEE RobinCK/typeorm-fixtures#87
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants