Closed simplenotezy closed 1 month ago
Also having the same issue. As workaround moved getter to Transform like:
export class MyDTOResponse {
@Expose()
id: string;
@Expose()
name: string;
@Expose()
@Transform(() => 'yolo')
thisIsATest: string;
}
It is not an issue with class-transformer. Getter inside javascript class are not exposed at serialization. You must customize the serialization process by creating a custom toJSON method inside your class.
Also having the same issue. As workaround moved getter to Transform like:
export class MyDTOResponse { @Expose() id: string; @Expose() name: string; @Expose() @Transform(() => 'yolo') thisIsATest: string; }
Thanks, I met the same problem. Here's my workaround for exposing a new field based on existing field:
@Expose()
@Transform(({ obj }) => `${obj.id}+${obj.name}`)
idName: string;
where obj refers to the source object. https://github.com/typestack/class-transformer#advanced-usage
@simplenotezy I guess it's late, but anyway. Main purpose of plainToClass (plainToInstance) is to bind your plain object fields to class properties, so It doesn't really need to call your class getters. To make your getters called you need to transfer instance of your class back to plain object like so:
classToPlain(plainToClass(MyDTOResponse, MyRawDataObject));
This is absolutely right. Solved the issue. Thanks @edkhrian!
Closing as answered! Thank you @edkhrian for your answer.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
I've tried to define a custom getter like this:
This is how I transform it:
As described in the documentation here: https://github.com/typestack/class-transformer#exposing-getters-and-method-return-values
However, the response is only:
I am not sure what I am doing wrong exactly, but I am missing the "thisIsATest" property.