Skip to content

Commit

Permalink
fix(core): fix query processing when PK is falsy (#4713)
Browse files Browse the repository at this point in the history
Fix case of `processWhere` returning empty object when the where clause
is an entity with an int pk of 0

---------

Co-authored-by: Martin Adámek <banan23@gmail.com>
  • Loading branch information
Robert-Schirmer and B4nan committed Sep 20, 2023
1 parent 72f8337 commit 3624cb7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/utils/QueryHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class QueryHelper {
QueryHelper.inlinePrimaryKeyObjects(where as Dictionary, meta, metadata);
}

where = QueryHelper.processParams(where) || {};
where = QueryHelper.processParams(where) ?? {};

/* istanbul ignore next */
if (!root && Utils.isPrimaryKey<T>(where)) {
Expand Down
1 change: 0 additions & 1 deletion tests/EntityManager.mongo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,6 @@ describe('EntityManagerMongo', () => {
});

test('findOne with empty where will throw', async () => {
await expect(orm.em.findOne(Author, '')).rejects.toThrowError(`You cannot call 'EntityManager.findOne()' with empty 'where' parameter`);
await expect(orm.em.findOne(Author, {})).rejects.toThrowError(`You cannot call 'EntityManager.findOne()' with empty 'where' parameter`);
await expect(orm.em.findOne(Author, undefined!)).rejects.toThrowError(`You cannot call 'EntityManager.findOne()' with empty 'where' parameter`);
await expect(orm.em.findOne(Author, null!)).rejects.toThrowError(`You cannot call 'EntityManager.findOne()' with empty 'where' parameter`);
Expand Down
13 changes: 12 additions & 1 deletion tests/SmartQueryHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MikroORM } from '@mikro-orm/core';
import { Reference, QueryHelper } from '@mikro-orm/core';
import { initORMMySql } from './bootstrap';
import { Author2, Book2, FooBar2, FooBaz2, Test2 } from './entities-sql';
import { Author2, Book2, BookTag2, FooBar2, FooBaz2, Test2 } from './entities-sql';
import { FooParam2 } from './entities-sql/FooParam2';

describe('QueryHelper', () => {
Expand Down Expand Up @@ -85,6 +85,17 @@ describe('QueryHelper', () => {
expect(QueryHelper.processWhere({ where: undefined as any, entityName: 'id', metadata: orm.getMetadata(), platform: orm.em.getDriver().getPlatform() })).toEqual({});
});

test('processWhere returns pk when pk is empty string and condition is entity', async () => {
const test = new BookTag2('Test');
test.id = '';
expect(QueryHelper.processWhere({ where: test, entityName: 'id', metadata: orm.getMetadata(), platform: orm.em.getDriver().getPlatform() })).toEqual('');
});

test('processWhere returns pk when pk is 0 and condition is entity', async () => {
const test = new Test2({ id: 0 });
expect(QueryHelper.processWhere({ where: test, entityName: 'id', metadata: orm.getMetadata(), platform: orm.em.getDriver().getPlatform() })).toEqual(0);
});

test('test entity conversion to PK', async () => {
const test = Test2.create('t123');
test.id = 123;
Expand Down

0 comments on commit 3624cb7

Please sign in to comment.