Skip to content
13 changes: 10 additions & 3 deletions packages/orm/src/client/crud/dialects/base-dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,16 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
.with('AND', () =>
this.and(...enumerate(payload).map((subPayload) => this.buildFilter(model, modelAlias, subPayload))),
)
.with('OR', () =>
this.or(...enumerate(payload).map((subPayload) => this.buildFilter(model, modelAlias, subPayload))),
)
.with('OR', () => {
const allBranches = enumerate(payload).map((subPayload) =>
this.buildFilter(model, modelAlias, subPayload)
);
const meaningfulBranches = allBranches.filter((expr) => !this.isTrue(expr));
if (meaningfulBranches.length === 0) {
return allBranches.length > 0 ? this.true() : this.false();
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
return this.or(...meaningfulBranches);
})
.with('NOT', () => this.eb.not(this.buildCompositeFilter(model, modelAlias, 'AND', payload)))
.exhaustive();
}
Expand Down
16 changes: 11 additions & 5 deletions packages/orm/src/client/crud/operations/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2531,11 +2531,17 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {

private doNormalizeArgs(args: unknown) {
if (args && typeof args === 'object') {
for (const [key, value] of Object.entries(args)) {
if (value === undefined) {
delete args[key as keyof typeof args];
} else if (value && isPlainObject(value)) {
this.doNormalizeArgs(value);
if (Array.isArray(args)) {
for (const element of args) {
this.doNormalizeArgs(element);
}
} else {
for (const [key, value] of Object.entries(args)) {
if (value === undefined) {
delete args[key as keyof typeof args];
} else if (value && isPlainObject(value)) {
this.doNormalizeArgs(value);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/e2e/orm/client-api/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -801,5 +801,35 @@ describe('Client filter tests ', () => {
await expect(client.user.findMany({ where: { id: undefined } })).toResolveWithLength(1);
});

it('ignores undefined branch inside OR filter', async () => {
await createUser('u1@test.com', {
name: 'First',
role: 'ADMIN',
profile: { create: { id: 'p1', bio: 'bio1' } },
});
const user2 = await createUser('u2@test.com', {
name: 'Second',
role: 'USER',
profile: { create: { id: 'p2', bio: 'bio2' } },
});

const baseline = await client.user.findFirst({
where: {
OR: [{ id: user2.id }],
} as any,
orderBy: { createdAt: 'asc' },
});

const withUndefinedBranch = await client.user.findFirst({
where: {
OR: [{ id: undefined }, { id: user2.id }],
} as any,
orderBy: { createdAt: 'asc' },
});

expect(baseline?.email).toBe(user2.email);
expect(withUndefinedBranch?.email).toBe(baseline?.email);
});

// TODO: filter for bigint, decimal, bytes
});
Loading