class-transformer icon indicating copy to clipboard operation
class-transformer copied to clipboard

fix: Groups are ignored when using `name` with Expose

Open stingus opened this issue 4 years ago • 2 comments

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 color used as property name vs colour name 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.

stingus avatar Dec 10 '21 06:12 stingus

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;

SerkisExclusive avatar Dec 17 '21 14:12 SerkisExclusive

Unbelievable, it's still a pending issue.

flchaux avatar May 22 '24 09:05 flchaux