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

fix: exposed getter function returns null if it returns other, excluded, parameter

Open evgenyidf opened this issue 4 years ago • 3 comments

Description

The getter function used in the NestJS controller omits the result if inside function body we use other (excluded) property. Meaning in my example, controller will return JSON without 'name' property at all..

Minimal code-snippet showcasing the problem

@Exclude()
export class exampleDto {

  @Expose()
  get name(): string {
    return this.intlName
  }

  readonly intlName?: string;
}

Expected behavior

'name' property should be visible with the value of 'intlName' property

Actual behavior

'name' property is missing in response json

evgenyidf avatar Jul 23 '21 10:07 evgenyidf

any update ?

evgenyidf avatar Jun 20 '22 10:06 evgenyidf

It is originated from the inability of es6 class I would expect class-transformer handles this case internally

Workaround can be found here: https://stackoverflow.com/questions/42107492/json-stringify-es6-class-property-with-getter-setter

maybe we can include this logic into class-transformer 🤔

kpkonghk01 avatar Sep 30 '22 08:09 kpkonghk01

I checked. It's working now

ElectroForez avatar Nov 11 '23 16:11 ElectroForez