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

PickType ignore parent's @Transform when grandparent use @Transform #2659

Closed
2 of 4 tasks
KuanWenChen opened this issue Oct 17, 2023 · 3 comments
Closed
2 of 4 tasks

Comments

@KuanWenChen
Copy link

KuanWenChen commented Oct 17, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

My ValidationPipe setting is

app.useGlobalPipes(new ValidationPipe({ transform: true }));

My Dto is

import { PickType } from '@nestjs/swagger';
import { Expose, Transform } from 'class-transformer';

export class TestDto1 {
  @Expose()
  @Transform(() => {
    console.log('TestDto1 Transform');
    return 1;
  })
  version!: number;
}

export class TestDto2 extends PickType(TestDto1, ['version']) {
  @Transform(() => {
    console.log('TestDto2 Transform');
    return 2;
  })
  version!: number;
}

export class TestDto3PickTypeVersion extends PickType(TestDto2, ['version']) {
  @Transform(() => {
    console.log('TestDto3 Transform');
    return 3;
  })
  version!: number;
}

export class TestDto3ExtendVersion extends TestDto2 {
  @Transform(() => {
    console.log('TestDto3 Transform');
    return 3;
  })
  version!: number;
}

My Controller is

@Controller('test')
export class TestController {
  @Post('/pick-type-version')
  test1(@Body() testDto3PickTypeVersion: TestDto3PickTypeVersion) {
    return testDto3PickTypeVersion;
  }

  @Post('/extend-version')
  test2(@Body() testDto3ExtendVersion: TestDto3ExtendVersion) {
    return testDto3ExtendVersion;
  }
}

When I call 「http://localhost:3000/pick-type-version」
image

TestDto3PickTypeVersion ignore TestDto2's @Transform.

When I call 「http://localhost:3000/extend-version」
image

TestDto2's @Transform would work.

If I change TestDto1 as

export class TestDto1 {
  @Expose()
  version!: number;
}

TestDto2's @Transform would be work too.

Minimum reproduction code

https://github.com/KuanWenChen/swagger-issue

Steps to reproduce

npm install
npm run start:dev

goto website http://localhost:3000/swagger
image

Call Api and See log.

Expected behavior

When I call「http://localhost:3000/pick-type-version」, I need to see 「TestDto2 Transform」

TestDto3PickTypeVersion should not ignore TestDto2's @Transform.

Package version

^7.1.13

NestJS version

10.2.7

Node.js version

v18.16.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

@KuanWenChen
Copy link
Author

OK, I has found the problem is happen at mapped-types function inheritTransformationMetadata

The problem is occurring at existingRules, and targetMetadataEntries are overwriting values taht with the same key ; they should merge.

I have created another issue for this problem.

@kamilmysliwiec
Copy link
Member

nestjs/mapped-types#1212

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants