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

Transformer order is inconsistent #231

Closed
jbdemonte opened this issue Feb 6, 2019 · 4 comments · Fixed by #355
Closed

Transformer order is inconsistent #231

jbdemonte opened this issue Feb 6, 2019 · 4 comments · Fixed by #355

Comments

@jbdemonte
Copy link

jbdemonte commented Feb 6, 2019

The execution order of the transformers should always be the same.
It seems to be reverse each time it's used

    class TestDto {
      @Expose()
      @Transform(() => console.log('A'), { toClassOnly: true })
      @Transform(() => console.log('B'), { toClassOnly: true })
      @Transform(() => console.log('C'), { toClassOnly: true })
      value: string;
    }

    for (let i = 0; i < 4; i++) {
      console.log(`--- Loop ${i} ----`);
      plainToClass(TestDto, {});
    }

This test log:

--- Loop 0 ----
A
B
C
--- Loop 1 ----
C
B
A
--- Loop 2 ----
A
B
C
--- Loop 3 ----
C
B
A
@VinceOPS
Copy link

VinceOPS commented Feb 6, 2019

This is probably due to:

return (metadataFromAncestorsTarget).reverse().concat((metadataFromTarget || []).reverse());

in MetadataStorage#findMetadatas, called by MetadataStorage#findTransformMetadatas.

And is indeed a bug.

@247GradLabs any clue regarding the reason of these calls to reverse? Thanks a lot!

@alexpls
Copy link

alexpls commented Apr 18, 2019

@VinceOPS Indeed. I think the order should be reversed so the transformers get applied in the correct order, but since Array.prototype.reverse() mutates the array it's called on, every time that code is run the array switches ordering.

I'd be happy to work on a PR to fix this, once this package is under active maintenance again.

@kirit0s
Copy link

kirit0s commented Apr 13, 2020

This bug made my day. Apparently, this bug is not critical for open source.

leon19 added a commit to leon19/class-transformer that referenced this issue May 29, 2020
When several trasnformers are applied at the same the order is reversed
every time plainToClass is called.

This fix make the trsnsfomers oders consistent across multiple `plainToClass()`
calls.

Fixes typestack#231
leon19 added a commit to leon19/class-transformer that referenced this issue Jul 21, 2020
When several trasnformers are applied to the same property the order is reversed
every time plainToClass is called.

This fix make the transfomers orders consistent across multiple `plainToClass()`
calls.

Fixes typestack#231
leon19 added a commit to leon19/class-transformer that referenced this issue Jul 21, 2020
When several trasnformers are applied to the same property the order is reversed
every time plainToClass is called.

This fix make the transfomers orders consistent across multiple `plainToClass()`
calls.

Fixes typestack#231
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
4 participants