Skip to content
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

Heterogeneous array serialization not working as expected #168

Closed
monesidn opened this issue Aug 13, 2022 · 0 comments
Closed

Heterogeneous array serialization not working as expected #168

monesidn opened this issue Aug 13, 2022 · 0 comments

Comments

@monesidn
Copy link
Collaborator

NOTE: This is part of a series of issues I'm creating to keep track of the work I've done, and that will be submitted, across multiple PRs

Currently when serializing an array containing heterogeneous objects without providing an explicit schema, the schema of the first item is used for all. This is highly counter-intuitive.

See this example:

class Person {
    @serializable name: string;
    @serializable surname: string;
}

class Car {
    @serializable model: string;
    @serializable color: string;
}

const serialized = serialize([
    Object.assign(new Person(), {
        name: "John",
        surname: "Doe",
    }),
    Object.assign(new Car(), {
        model: "Fiat 500",
        color: "Red",
    }),
]);

// Expected:
// [
//     {
//         name: "John",
//         surname: "Doe",
//     },
//     {
//         model: "Fiat 500",
//         color: "Red",
//     },
// ];

// Found:
// [
//     { name: "John", surname: "Doe" },
//     { name: undefined, surname: undefined },
// ];

To solve this problem when serializing an array the actual schema (if not provided) should be inferred on a per-element basis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant