Skip to content

Commit

Permalink
fix(core): fix diffing of JSON properties
Browse files Browse the repository at this point in the history
Closes #4078
  • Loading branch information
B4nan committed Feb 28, 2023
1 parent e721ad7 commit 2e9a026
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
9 changes: 1 addition & 8 deletions packages/core/src/hydration/ObjectHydrator.ts
Expand Up @@ -96,14 +96,7 @@ export class ObjectHydrator extends Hydrator {
` if (${preCond}typeof data${dataKey} !== 'undefined') {`,
` if (convertCustomTypes) {`,
` const value = convertToJSValue_${convertorKey}(data${dataKey});`,
);

// make sure the value is comparable, but skip this for JSON props as it can result in double encoding
if (!(prop.customType instanceof JsonType)) {
ret.push(` data${dataKey} = convertToDatabaseValue_${convertorKey}(value);`);
}

ret.push(
` data${dataKey} = convertToDatabaseValue_${convertorKey}(value);`, // make sure the value is comparable
` entity${entityKey} = value;`,
` } else {`,
` entity${entityKey} = data${dataKey};`,
Expand Down
54 changes: 54 additions & 0 deletions tests/issues/GH4078.test.ts
@@ -0,0 +1,54 @@
import { Entity, JsonType, PrimaryKey, Property } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/mysql';
import { mockLogger } from '../helpers';

type Setup = {
limits?: boolean;
fallbackFees?: boolean;
};

@Entity()
class Contract {

@PrimaryKey()
id!: number;

@Property()
title!: string;

@Property({ type: JsonType, nullable: true })
setup: Setup | null = null;

}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [Contract],
dbName: `mikro_orm_test_4078`,
port: 3308,
});

await orm.schema.refreshDatabase();
});

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

test('4078', async () => {
await orm.em.insert(Contract, {
id: 1,
title: 't',
setup: {
limits: true,
fallbackFees: false,
},
});
orm.em.clear();
await orm.em.findOneOrFail(Contract, 1);
const mock = mockLogger(orm);
await orm.em.flush();
expect(mock).not.toBeCalled();
});

0 comments on commit 2e9a026

Please sign in to comment.