forked from typestack/class-transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make transformers order deterministic
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
- Loading branch information
Showing
2 changed files
with
26 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import "reflect-metadata"; | ||
import { classToPlain, plainToClass } from "../../src/index"; | ||
import { defaultMetadataStorage } from "../../src/storage"; | ||
import { Exclude, Expose, Transform } from "../../src/decorators"; | ||
|
||
describe("applying several transformations", () => { | ||
it("should keep the order of the applied decorators after several plainToClass() calls", () => { | ||
class User { | ||
@Transform(() => "Jonathan") | ||
@Transform(() => "John") | ||
@Expose() | ||
name: string; | ||
} | ||
|
||
const firstUser = plainToClass(User, { name: "Joe" }); | ||
expect(firstUser.name).toEqual("John"); | ||
|
||
// Prior to this pull request [#355](https://github.com/typestack/class-transformer/pull/355) | ||
// the order of the transformations was reversed after every `plainToClass()` call | ||
// So after consecutive calls `User#name` would be "John" - "Jonathan" - "John" - "Jonathan"... | ||
// This test ensures the last transformation is always the last one to be applied | ||
const secondUser = plainToClass(User, { name: "Joe" }); | ||
expect(secondUser.name).toEqual("John"); | ||
}); | ||
}); |