class-transformer
class-transformer copied to clipboard
fix: Groups are ignored when using `name` with Expose
Description
When using plainToClass, groups are ignored if the property name and the @Expose name don't match.
Minimal code-snippet showcasing the problem
class Foo {
@Expose({ name: 'colour', groups: ['bar'] })
color: string;
}
const obj = { colour: 'red' };
const foo = plainToClass(Foo, obj, { groups: ['baz'] });
console.log(foo);
- please note
colorused as property name vscolourname used in@Expose
Expected behavior
Foo object should have the color property undefined, since I'm requesting the group baz in plainToClass, and the property is exposed for bar group only.
Foo {
"color": undefined,
}
Actual behavior
The property value is set, regardless of the requested groups:
Foo {
"color": "red",
}
The behavior is correct if I'm not using a different name in @Expose.
Same issue here, if the name of the Expose is not much the property the groups are ignored, please advise
Code example:
@Expose({name: "my_test_id", groups: [Group.TEST]})
myTestId?: string;
Workaround that I did and working is:
@Transform(({}) => {
return;
}, {groups: [Group.UI, Group.BACKEND]})
@Expose({name: "my_test_id"})
myTestId?: string;
Unbelievable, it's still a pending issue.