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

fix: Class constructor Model cannot be invoked without 'new'

Open jdpt0 opened this issue 2 years ago • 5 comments

Oops, this was meant for the NestJS repo, but is probably useful here too.

Description

nestjs/nest#7281 and this commit were intended to open up what could be transformed by the class-serializer interceptor, which was released with version 8.0.0 of NestJS. I've been trying to upgrade from version 7.16.0 to 8.2.3 but I'm running into issues with class-transformer, which is needed as we use Sequelize. I'm getting errors that seem similar to typestack/class-transformer#242 and typestack/class-transformer#596 using both class-transformer version 0.4.1 and 0.5.1.

Minimal code-snippet showcasing the problem I've attempted to create a minimal reproduction, but I'm so far unsuccessful. However, if I add the line removed above back in, everything works without a problem. I'll keep attempting to figure out exactly what the problem is, but realistically, it's probably a problem with class-transformer rather than NestJS. Most of this low-level stuff goes over my head, so I'm not sure where to go from here, or if I'll actually be able to understand exactly why this is breaking.

EDIT: I found the cause of the issue. It's related to nested Sequelize objects. A minimal reproduction can be found at https://github.com/jdpowell1/class-transformer-bug

Expected behavior

When querying an endpoint that returns a Sequelize object, transform it correctly.

Actual behavior

This error is thrown.

TypeError: Class constructor Model cannot be invoked without 'new'
    at TransformOperationExecutor.transform (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\TransformOperationExecutor.ts:207:39)
    at ...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\TransformOperationExecutor.ts:79:30
    at Array.forEach (<anonymous>)
    at TransformOperationExecutor.transform (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\TransformOperationExecutor.ts:44:24)
    at TransformOperationExecutor.transform (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\TransformOperationExecutor.ts:327:31)
    at TransformOperationExecutor.transform (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\TransformOperationExecutor.ts:327:31)
    at TransformOperationExecutor.transform (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\TransformOperationExecutor.ts:327:31)
    at ClassTransformer.instanceToPlain (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\ClassTransformer.ts:25:21)
    at Object.classToPlain (...\.yarn\cache\class-transformer-npm-0.5.1-96b5161e6c-f191c8b4cc.zip\node_modules\src\index.ts:23:27)
    at ClassSerializerInterceptor.transformToPlain (...\.yarn\__virtual__\@nestjs-common-virtual-63645f8124\0\cache\@nestjs-common-npm-8.2.3-f49d058617-2a06ffaf58.zip\node_modules\@nestjs\common\serializer\class-serializer.interceptor.js:47:32)

jdpt0 avatar Dec 05 '21 04:12 jdpt0

Related to nestjs/nest#8741

jdpt0 avatar Dec 05 '21 06:12 jdpt0

Exactly same problem here : can't serialize nested object... Any updates ??

Tchekda avatar May 20 '22 23:05 Tchekda

If you're using NestJS, I'd recommend patching it, that's what we're doing so we can still keep up to date with the latest version of NestJS

jdpt0 avatar May 21 '22 00:05 jdpt0

If you're using NestJS, I'd recommend patching it, that's what we're doing so we can still keep up to date with the latest version of NestJS

@jdpowell1 any help on how to patch it ?

We also want to use the NestJS latest features. For now we manually run the transformation in our services but would be appreciated to have the class-transformer back

Tchekda avatar May 21 '22 00:05 Tchekda

You can use this package to revert this commit which is what originally caused this issue to start appearing (although the bug lies in class-transformer rather than NestJS)

jdpt0 avatar May 21 '22 01:05 jdpt0