You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am experiencing an issue with class-transformer in my NestJS project where getter methods are being called multiple times. This issue arises when using the instanceToPlain method.
Minimal code-snippet showcasing the problem
import{User}from'../domain/user.entity';import{Exclude,Expose}from'class-transformer';exportclassUserResponseDto{
@Exclude()privatereadonly_firstName: string;
@Exclude()privatereadonly_lastName: string;constructor(user: User){console.log('Constructor called');this._firstName=user.firstName;this._lastName=user.lastName;}
@Expose()getfirstName(): string{console.log('First Name getter called');returnthis._firstName;}
@Expose()getlastName(): string{console.log('Last Name getter called');returnthis._lastName;}}// Usage exampleconstuser=newUser();// Assume this is populated with dataconstuserDto=newUserResponseDto(user);// Constructor called once hereconstplainUser=instanceToPlain(userDto);// Multiple getter calls here
Expected behavior
The getter methods should be called once per property access, minimizing redundant calls.
Actual behavior
When a single request is made, the console logs show that the constructor and getters are called multiple times:
Constructor called
First Name getter called
First Name getter called
First Name getter called
First Name getter called
First Name getter called
Last Name getter called
Last Name getter called
Last Name getter called
Last Name getter called
Last Name getter called
In the above example, Constructor called is logged once when the UserResponseDto is instantiated. However, First Name getter called and Last Name getter called are logged multiple times when instanceToPlain is called, indicating multiple unnecessary calls to the getter methods during the serialization process.
This behavior results in multiple unnecessary calls to the getter methods during the serialization process.
The text was updated successfully, but these errors were encountered:
Description
I am experiencing an issue with class-transformer in my NestJS project where getter methods are being called multiple times. This issue arises when using the instanceToPlain method.
Minimal code-snippet showcasing the problem
Expected behavior
The getter methods should be called once per property access, minimizing redundant calls.
Actual behavior
When a single request is made, the console logs show that the constructor and getters are called multiple times:
In the above example, Constructor called is logged once when the UserResponseDto is instantiated. However, First Name getter called and Last Name getter called are logged multiple times when instanceToPlain is called, indicating multiple unnecessary calls to the getter methods during the serialization process.
This behavior results in multiple unnecessary calls to the getter methods during the serialization process.
The text was updated successfully, but these errors were encountered: