entity icon indicating copy to clipboard operation
entity copied to clipboard

`.toJson(as_string: true)` for nested entities

Open hkan opened this issue 2 years ago • 0 comments

class A extends Entity {
    public foo: string = null;
}

class B extends Entity {
    @Type(A)
    children: A[] = null;
}

const instance = EntityBuilder.buildOne<B>(B, {
    children: [
        { foo: 'bar' },
    ],
});

console.log(instance.toJson(true, true));

The code above yields this result:

{
    "children": [
        "{\"foo\": \"bar\"}"
    ]
}

... while the expected result should be:

{
    "children": [
        {
            "foo": "bar"
        }
    ]
}

... because nested entities get converted to string before being assigned to the result object which in the end gets stringified as well.

hkan avatar Jan 09 '22 16:01 hkan