deepkit-framework icon indicating copy to clipboard operation
deepkit-framework copied to clipboard

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

Open Char2sGu opened this issue 2 years ago • 0 comments

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
    ]);
});

Char2sGu avatar Aug 21 '22 01:08 Char2sGu