-
Notifications
You must be signed in to change notification settings - Fork 478
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
plainToClass
parses object to array with properties
#255
Comments
Hi! Can you please share the Can you check if it's working in |
I have tried to reproduce it with the following code, but it worked for me correctly, so please do share a minimal reproduction. Thanks! import 'reflect-metadata';
import { plainToClass, Transform, Expose, Type, Exclude } from 'class-transformer';
class User {
id: number;
name: string;
}
class Users {
@Type(() => User)
users: User[];
}
const instance = plainToClass(Users, { users: [{ id: 123, name: "Carl"}] });
console.log(instance) // corretly logs { users: [{ id: 123, name: "Carl"}] }
|
I got a similar issue after upgrading to 0.2.2, By |
I tried to reproduce the error with some snippets extracted from our production code, but couldn't reproduce either, yet. I have to give it another try the next days. |
Thanks, I am waiting for it.
@baflo I see your issue at MichalLytek/class-transformer-validator#20 but I cannot reproduce this issue with that example code either. (Stackblitz Demo) In the console, I see an instance of
|
Thank you for testing it out. My question is, But before v0.2.2, like v0.2.1, the same code could run with no error without |
You had to import reflect-metadata before. Does your code work when reflect-metadata is included at the first line of your application?
Now we are directly requesting type information generated by Typescript thats why you receive that error.
I don't understand what you want to say with this. We knew the old version transformed correctly. The question is about an invalid transform with 0.2.2. So please provide a demo with the 0.2.2 version what demonstrates the invalid behaviour. |
I finally had the time to investigate the problem and comprehend an example. I found that I actually can fix it (as I had an issue), but still the behaviour is odd: import { plainToClass, Type } from "class-transformer";
import { IsString, Validate, ValidatorConstraintInterface } from "class-validator";
class WrapperValidator implements ValidatorConstraintInterface {
public async validate(): Promise<boolean> {
return true;
}
}
describe("class-transformer.plainToClass", function() {
beforeEach(async function() {
class Wrapper {
@Validate(WrapperValidator)
@Type(() => Data) // This was missing, i.e. the odd behaviour occurs, if I remove this
public elements!: any[];
constructor(elements: any[]) {
this.elements = elements;
}
}
class Data {
@IsString()
public data!: string;
constructor(data: string) {
this.data = data;
}
}
this.event = JSON.parse(JSON.stringify(new Wrapper([new Data("Test")])));
this.class = plainToClass(Wrapper, this.event) as any;
});
fit("creates a correct class object", async function() {
expect(JSON.stringify(this.class)).toEqual(JSON.stringify(this.event));
});
}); So, if I remove the |
Will this work: export type Users = User[];
export class User {
id: number;
name: string;
} and then casting an object to an array: ...
usersObj: any = response ; // object typed user[], matches the type `Users`
const strongTyped = plainToClass(Users, usersObj); // 'Users' only refers to a type, but is being used as a value here
... While this does not work, what is the best way to achieve this? Would I have to implement it like below? usersObj.map(obj => plainToClass(User, obj)); |
Came across the same issue.
It seems to happen when an array of objects is not annotated with specific type (like workarounds here suggested). The error, as I see it, happens here:
this.transform is called recursively with type === Array . In recursive call, another branch is used:class-transformer/src/TransformOperationExecutor.ts Lines 77 to 86 in 4741231
And since on this iteration targetType was already an Array , it recurses further for deserialization of entries with the same Array type, when actually it had to guess the type from the scratch.
I should also point out that I'm using |
@JAspeling You got any other solving to resolve this issue ? |
Closing this as I still don't see a reproducible error. Everything works as expected. @baflo In your example, you have to specify
This is the expected behavior. |
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. |
I can't really tell what's happening and how, but since changing from
0.2.0
to0.2.2
we get an error, that nested objects are parsed to arrays. Something like this:The text was updated successfully, but these errors were encountered: