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

Map returns undefined values with never versions

Open driescroons opened this issue 3 years ago • 4 comments

I've got the following code that I set up for testing purposes:

@Exclude()
class Transformer {
  @Expose()
  @IsString({ each: true })
  public map!: Map<string, string>;
}

const obj = {
  map: new Map([
    ["1", "123"],
    ["2", "123"],
    ["3", "123"]
  ])
};

let data = plainToClass(Transformer, obj);
console.log(data);

Ever since 0.4.1, the values of the map return undefined (after moving to plainToInstance).

The following printscreen is the result of using version 0.4.1: image

The following printscreen is the result of using0.4.0: image

As you can see, the values are not undefined.

Did the syntax of validating a map change? Or is there something wrong? Following link is the CSB for testing: https://codesandbox.io/s/busy-kilby-7e8z73?file=/src/index.ts

driescroons avatar Jun 28 '22 01:06 driescroons

This same issue is also reproducible in the latest version 0.5.1 for the function plainToInstance.

patil-milind avatar Jul 05 '22 11:07 patil-milind

issue: #596 PR: #374

// ...
if (this.transformationType === TransformationType.PLAIN_TO_CLASS) {
  subValue = value[valueKey];
}
// ...

I don't know why it changed to this 😂.

Maybe the maintainers think plain object should be a simple JSON object. Like this:

@Exclude()
class Transformer {
  @Expose()
  @Type(() => String)
  public map!: Map<string, string>;
}

const obj = {
  map: {
    '1': '123',
    '2': '123',
    '3': '123',
  }
};

const data = plainToInstance(Transformer, obj);
console.log(data);

Output:

Transformer {
  map: Map(3) { '1' => '123', '2' => '123', '3' => '123' }
}

CodyTseng avatar Jul 05 '22 14:07 CodyTseng

I was trying to have a map of type Map<string, object[]> with automatically generated docs. I've since then switched back to a regular object to get the proper result. For people running into this issue, this is how I fixed it:

  @Expose()
  @IsObject()
  @JSONSchema({
    type: "object",
    properties: {
      "{id}": {
        type: "array",
        items: {
          $ref: `#/components/schemas/${OtherClassTransformerClass.name}`,
        },
      },
    },
  })
  public collections!: { [key: string]: OtherClassTransformerClass[] };

driescroons avatar Jul 05 '22 15:07 driescroons

It seems the functionality that is described here is not supported. There is a comment with a workaround solution. Nested map?

victor-shelepen avatar Aug 18 '23 14:08 victor-shelepen