typestack / class-transformer

Decorator-based transformation, serialization, and deserialization between objects and classes.
MIT License
6.78k stars 496 forks source link

fix: Transform fails if the constructor expects an argument #1385

Closed pavankris closed 3 months ago

pavankris commented 1 year ago

Description

https://github.com/typestack/class-transformer/blob/develop/src/TransformOperationExecutor.ts#L160 TargetType is DecimalJS https://github.com/MikeMcl/decimal.js/blob/master/decimal.js#L4285 and the valueType is Decimal None of the if block in TransformOperationExecutor.ts covers this as a result it falls into newValue = new (targetType as any)(); and the default constructor expects a valid argument

but if we pass newValue = new (targetType as any)(value); everything works

Expected behavior

Why shouldn't the value be passed in constructor ?

bijela-gora commented 1 year ago

@pavankris hi!

Why shouldn't the value be passed in constructor ?

I think because class-transformer doesn't support such case. The reasons might be the follow:

  1. It is possible to define anything as a constructor parameter, for example an array: https://github.com/typestack/class-transformer/issues/1381#issue-1422898041
  2. Or constructor might have multiple parameters.

Those two points should tell us, that to support such feature we need to change somehow interface of plainToInstance function, in order to give it a info of how to get constructor parameters and how they should look like.

diffy0712 commented 3 months ago

The issue does not contain a simple reproduction case.

Closing as invalid.

github-actions[bot] commented 2 months ago

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.