-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Class transformer not working with swc #2117
Comments
I'm coming from #1362 and this doesn't work #1362 (comment) Good repro btw @DanielRamosAcosta! |
I've been checking the generated code, and there's a problematic part. Minimal repro
import { IsOptional, IsString } from 'class-validator';
export class GetUsersOptionsInput {
@IsString()
@IsOptional()
public query?: string;
// it works
// public query?: string = undefined;
}
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.GetUsersOptionsInput = void 0;
var _classValidator = require("class-validator");
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
var desc = {
};
Object.keys(descriptor).forEach(function(key) {
desc[key] = descriptor[key];
});
desc.enumerable = !!desc.enumerable;
desc.configurable = !!desc.configurable;
if ("value" in desc || desc.initializer) {
desc.writable = true;
}
desc = decorators.slice().reverse().reduce(function(desc, decorator) {
return decorator ? decorator(target, property, desc) || desc : desc;
}, desc);
if (context && desc.initializer !== void 0) {
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
// IF NO INITILIZAER, THE RETURNED DESCRIPTOR IS NULL
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
}
return desc;
}
function _initializerDefineProperty(target, property, descriptor, context) {
if (!descriptor) return;
Object.defineProperty(target, property, {
enumerable: descriptor.enumerable,
configurable: descriptor.configurable,
writable: descriptor.writable,
value: descriptor.initializer ? descriptor.initializer.call(context) : void 0
});
}
var _class, _descriptor, _dec, _dec1, _dec2;
let GetUsersOptionsInput = ((_class = class GetUsersOptionsInput1 {
constructor(){
_initializerDefineProperty(this, "query", _descriptor/*THIS DESCRIPTOR IS NULL*/, this);
}
}) || _class, _dec = (0, _classValidator).IsString(), _dec1 = (0, _classValidator).IsOptional(), _dec2 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", String), _descriptor = _applyDecoratedDescriptor(_class.prototype, "query", [
_dec,
_dec1,
_dec2
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0 // THE INITIALIZER IS UNDEFINED
}), _class);
exports.GetUsersOptionsInput = GetUsersOptionsInput; This is the problematic part from if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
} When the property decorated has not I don't know exactly why that check is neccesary, but there is the problem. I hope it helps @kdy1! Excellent project btw 🆙 |
Great work @tonivj5! i tried diving into the generated code but I didn't reach any conclusions, thanks for your insight! |
Issue on |
@unlight if class-validator works perfect with tsc and fail with swc. I think it's a problem of swc handling decorators... |
@tonivj5 Did you try importing |
@kdy1 yep! It doesn't work. This code does not work: import "reflect-metadata";
import { IsOptional, IsString } from "class-validator";
export class GetUsersOptionsInput {
@IsString()
@IsOptional()
public query?: string;
} This code does work: import "reflect-metadata";
import { IsOptional, IsString } from "class-validator";
export class GetUsersOptionsInput {
@IsString()
@IsOptional()
public query?: string = undefined;
} I did a deeper research here #2117 (comment) |
Coming from: https://stackoverflow.com/questions/70956406/plaintoclass-from-class-transform-converts-incorrecly |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Describe the bug
When applying the
plainToClass
function to a class defined withclass-validator
, it returns the class with no attributesInput code
Here is a repo with a test reproducing the error
Config
You can also find the config at the repo reproducing the error
Expected behavior
I would expect the instance of the class to have all the defined attributes.
Version
The version of @swc/core: 1.2.80
Additional context
Just one more time, I created this repo in order to reproduce the error
This error is related to #1362
The text was updated successfully, but these errors were encountered: