typestack / class-transformer

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

question: @Type() is not working anymore after compilation with rollup #613

Open MickL opened 3 years ago

MickL commented 3 years ago

Description

I do have a shared project that contains interfaces and dto's:

export class MySubDto {
  @IsString()
  name: string;
}

export class MyClassDto {
  @IsString()
  id: string;

  @Type(() => MySubDto)
  @ValidateNested()
  child: MySubDto;
}

Which works fine if I use plainToClass() in the same project:

MyClassDto { id: '1', child: MySubDto { name: 'cat' } }

But after I compiled the project with Rollup, import MyClassDto and use plainToClass() the @Type does not have any effect anymore:

MyClassDto { id: '1', child: { name: 'cat' } }

I wonder if you could give me any hint why this could be the case. For everything else (class validator) it works fine. Maybe this is a bug? Or what could it be?

I do compile to umd and es5, not sure which one my app (node.js) picks up.

Version: 0.4.0

I have created a minimal reproduction example: https://github.com/MickL/class-transformer-type

MickL commented 3 years ago

There are some notes in the console while compiling, not sure if they are related to the issuse:

(!) `this` has been rewritten to `undefined`
https://rollupjs.org/guide/en/#error-this-is-undefined
../node_modules/class-validator/esm5/validation/Validator.js
1: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
                    ^
2:     function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3:     return new (P || (P = Promise))(function (resolve, reject) {
...and 3 other occurrences
../node_modules/class-transformer/esm5/ClassTransformer.js
1: var __assign = (this && this.__assign) || function () {
                   ^
2:     __assign = Object.assign || function(t) {
3:         for (var s, i = 1, n = arguments.length; i < n; i++) {
...and 1 other occurrence
../node_modules/class-validator/esm5/decorator/common/ValidateNested.js
1: var __assign = (this && this.__assign) || function () {
                   ^
2:     __assign = Object.assign || function(t) {
3:         for (var s, i = 1, n = arguments.length; i < n; i++) {
...and 1 other occurrence
MickL commented 3 years ago

@NoNameProvided May you take a look at this? :)

NoNameProvided commented 3 years ago

The compiler most probably strips the type information. Make sure the built version contains the type metadata attached to the classes and properties. It should look something like this: __metadata("design:type", <class-type-here>)

MickL commented 3 years ago

@NoNameProvided I see this in the generated built files:

ES5: https://github.com/MickL/class-transformer-type/blob/e61dbf6161033c5dce321b23984c9193aa49234d/my-lib/dist/my-lib.es5.js#L710 UMD: https://github.com/MickL/class-transformer-type/blob/e61dbf6161033c5dce321b23984c9193aa49234d/my-lib/dist/my-lib.umd.js#L718

Is the issue related to the message of rollup about "this"? May you have time to take a look at my minimal reproduction repo? https://github.com/MickL/class-transformer-type

MickL commented 3 years ago

@NoNameProvided Would be very glad if you could take a look at the repo. Is the issue related to the warning about "this"?

NoNameProvided commented 3 years ago

I am not super familiar with Rollup, so I don't know. I may take a look at this on the weekend but not promising a thing, as there is a lot of issue to look at.

azertyalex commented 1 year ago

I had a similar issue. I added class-transformer as a dependency in package.json of my shared-types package and it worked :D