Skip to content

Commit

Permalink
fix(core): fix extra updates on embedded array properties
Browse files Browse the repository at this point in the history
Closes #5530
  • Loading branch information
B4nan committed May 2, 2024
1 parent f613766 commit 48fde11
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/knex/src/AbstractSqlDriver.ts
Expand Up @@ -496,6 +496,8 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection

if (prop.kind === ReferenceKind.EMBEDDED && prop.object) {
if (prop.array && value) {
value = this.platform.cloneEmbeddable(value);

for (let i = 0; i < (value as Dictionary[]).length; i++) {
const item = (value as Dictionary[])[i];
value[i] = this.mapDataToFieldNames(item, false, prop.embeddedProps, options.convertCustomTypes);
Expand Down
53 changes: 53 additions & 0 deletions tests/issues/GH5530.test.ts
@@ -0,0 +1,53 @@
import { Entity, MikroORM, PrimaryKey, Property, Embeddable, Embedded, PrimaryKeyProp, Opt } from '@mikro-orm/sqlite';
import { mockLogger } from '../helpers';

@Embeddable()
class StepEntity {

@Property()
camelCasePropertyName!: number;

}

@Entity({ tableName: 'test_case' })
class TestCaseEntity {

[PrimaryKeyProp]?: 'sid';

@PrimaryKey({ autoincrement: true })
sid!: number;

@Embedded(() => StepEntity, { object: true, nullable: true })
step?: StepEntity;

@Embedded(() => StepEntity, { array: true })
steps: StepEntity[] & Opt = [];

}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
dbName: ':memory:',
entities: [TestCaseEntity],
});
await orm.schema.createSchema();
});

afterAll(async () => {
await orm.close(true);
});

test('extra updates on embedded arrays', async () => {
const testCase = orm.em.getRepository(TestCaseEntity).create({
step: { camelCasePropertyName: 2 },
steps: [{ camelCasePropertyName: 1 }],
});

await orm.em.persistAndFlush(testCase);

const mock = mockLogger(orm);
await orm.em.flush();
expect(mock).not.toHaveBeenCalled();
});

0 comments on commit 48fde11

Please sign in to comment.