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

fix: @Expose with same name not evaluated for all properties #1503

Open
JeffreyArt1 opened this issue Apr 12, 2023 · 1 comment
Open

fix: @Expose with same name not evaluated for all properties #1503

JeffreyArt1 opened this issue Apr 12, 2023 · 1 comment
Assignees
Labels
type: fix Issues describing a broken feature.

Comments

@JeffreyArt1
Copy link

JeffreyArt1 commented Apr 12, 2023

Description

I have noticed that when using the @expose decorator with the same name for multiple properties in the same class, the transform function is only evaluated for the first property.

For example, consider the following code:

const plain = {
  name: {
    first: 'John',
    last: 'Doe',
  },
};

@Exclude()
class Person {
  @Expose({ name: 'name' })
  @Transform(({ value }) => value.first)
  firstName: string;

  @Expose({ name: 'name' })
  @Transform(({ value }) => value.last)
  lastName: string;
}

Here's a snippet

Expected behavior

console.log(plainToClass(Person, plain)); // Person { firstName: "John", lastName: "Doe" }

Actual behavior

console.log(plainToClass(Person, plain)); // Person { firstName: "John" }

When plainToClass is called with this class and a plain object, only the transform function for the firstName property is evaluated, while the lastName property remains undefined.

I believe this is a bug in the class-transformer library, as both properties should be transformed based on their respective transform functions. Correct me if i'm wrong.

@JeffreyArt1 JeffreyArt1 added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Apr 12, 2023
@amrsalama
Copy link

@JeffreyArt1 I have the same issue, my workaround is

const plain = {
  name: {
    first: 'John',
    last: 'Doe',
  },
};

@Exclude()
class Person {
  @Expose()
  @Transform(({ obj }) => obj.name.first)
  firstName: string;

  @Expose()
  @Transform(({ obj }) => obj.name.last)
  lastName: string;
}

@diffy0712 diffy0712 self-assigned this May 7, 2024
@diffy0712 diffy0712 removed the status: needs triage Issues which needs to be reproduced to be verified report. label May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: fix Issues describing a broken feature.
Development

No branches or pull requests

3 participants