deepkit / deepkit-framework

A new full-featured and high-performance TypeScript framework
https://deepkit.io/
MIT License
3.22k stars 123 forks source link

[Bug] Joined `Reference` not Expanded in Serialization in Self-Reference Senarios #359

Closed Char2sGu closed 1 year ago

Char2sGu commented 2 years ago
test('joinWith', async () => {
    // Condition1:  Self-Reference Entity
    class MyEntity {
        id: number & PrimaryKey & AutoIncrement = 0;
        ref?: MyEntity & Reference;
        refs?: MyEntity[] & BackReference;
    }

    const database = new Database(new SQLiteDatabaseAdapter(':memory:'), [MyEntity]);
    database.logger.enableLogging();
    await database.migrate();
    const entity1 = new MyEntity();
    const entity2 = new MyEntity();
    entity1.ref = entity2;
    await database.persist(entity1, entity2);
    // Condition2: Both Reference and BackReference fields are Joined.
    const result = await database
        .query(MyEntity)
        .joinWith('ref')
        .joinWith('refs')
        .find();
    expect(serialize<MyEntity[]>(result)).toEqual([
        { id: 1, ref: null, refs: [{ id: 2, ref: 1 }] },
        { id: 2, ref: { id: 1, ref: null } }, //  <--- Bug: Here `ref` is `null` instead of a nested object
    ]);
});
marcj commented 1 year ago

Thanks @TheNightmareX, this should be fixed in master. Please check it out and let me know. (tests added in https://github.com/deepkit/deepkit-framework/commit/aa90b17a28cdf0b081736579c7463920eb97412a)