From 75d035dbb56dfe4aec78db39a7dfbc99e1d372a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ad=C3=A1mek?= Date: Sat, 13 Jan 2024 01:50:50 +0100 Subject: [PATCH] feat(core): do not map array types as `Loaded` when partially loaded Closes #5123 --- packages/core/src/typings.ts | 2 +- tests/features/partial-loading/GH5123.test.ts | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/features/partial-loading/GH5123.test.ts diff --git a/packages/core/src/typings.ts b/packages/core/src/typings.ts index dc659c4e1e81..d78821179aa9 100644 --- a/packages/core/src/typings.ts +++ b/packages/core/src/typings.ts @@ -965,7 +965,7 @@ type LoadedLoadable = ? LoadedReference : T extends ScalarReference ? LoadedScalarReference - : T extends Scalar | Scalar[] + : T extends Scalar | any[] ? T : E; diff --git a/tests/features/partial-loading/GH5123.test.ts b/tests/features/partial-loading/GH5123.test.ts new file mode 100644 index 000000000000..36eed9de1434 --- /dev/null +++ b/tests/features/partial-loading/GH5123.test.ts @@ -0,0 +1,38 @@ +import { Entity, PrimaryKey, Property, JsonType } from '@mikro-orm/core'; +import { MikroORM } from '@mikro-orm/sqlite'; + +@Entity() +class A { + + @PrimaryKey() + id!: number; + + @Property({ type: JsonType }) + array!: B[]; + +} + +interface B { + test: string; +} + +let orm: MikroORM; + +beforeAll(async () => { + orm = await MikroORM.init({ + entities: [A], + dbName: ':memory:', + }); + await orm.schema.createSchema(); +}); + +test('GH #5123', async () => { + const a = orm.em.create(A, { array: [{ test: 'test' }] }); + await orm.em.persistAndFlush(a); + + const a1 = await orm.em.fork().findOneOrFail(A, 1); + const a2 = await orm.em.fork().findOneOrFail(A, 1, { fields: ['array'] }); + + expect(a1.array.filter(i => i)).toEqual([{ test: 'test' }]); + expect(a2.array.filter(i => i)).toEqual([{ test: 'test' }]); +});