Repository navigation
fix: apply default throw behavior in normalizeWhereCriteria when options is not provided - #12579
samkessaram wants to merge 6 commits into
Conversation
…ons is not provided When invalidWhereValuesBehavior is not configured on the DataSource, normalizeWhereCriteria short-circuits and returns criteria without validation. This causes update() and delete() to silently accept undefined/null values in where conditions, while find() correctly throws. Replace the early return with an empty object fallback so the existing default logic runs. Closes typeorm#12578 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Code Review by Qodo
1.
|
commit: |
gioboa
left a comment
There was a problem hiding this comment.
Thanks @samkessaram LGTM 👍
|
Cool, looks like it is affecting other tests so I'll check that out. |
…malizeWhereCriteria When an entity instance (e.g. from repository.save()) is passed to softDelete/delete/update, nullable columns like foreign keys are not intended as filter conditions. Use isPlainObject to distinguish entity instances from plain FindOptionsWhere objects and default to ignoring null/undefined on the former while preserving strict throw behavior for the latter. Also fix typo: indempotence → idempotence in test description. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
| if (OrmUtils.isPlainObject(criteria)) { | ||
| options ??= {} | ||
| } else { | ||
| // Entity class instances may carry nullable columns (e.g. foreign keys) | ||
| // that are not intended as filter conditions — default to ignoring them. | ||
| options ??= { null: "ignore", undefined: "ignore" } |
There was a problem hiding this comment.
Would like some extra attention here, as this check wasn't in before. The commit message, for reference:
When an entity instance (e.g. from repository.save()) is passed to softDelete/delete/update,
nullable columns like foreign keys are not intended as filter conditions. Use isPlainObject to
distinguish entity instances from plain FindOptionsWhere objects and default to ignoring
null/undefined on the former while preserving strict throw behavior for the latter.
There was a problem hiding this comment.
I'm not sure about this one, can you please share some examples that emphasize using ignore and not throw on both? 🙏
Thanks in advance
There was a problem hiding this comment.
Here's what I'm thinking:
Plain objects ({ id: 1, name: undefined }): more likely a bug I think, either some value not being returned when expected or just human error. Default: throw.
Entity class instances: these carry all columns including nullable foreign keys (parentId: null, deletedAt: undefined). When passed to softDelete(entity), only the non-null fields should form the WHERE clause. Default: ignore.
// Plain object --> throw
const name = await fetchUserName(id) // returns null unexpectedly
repo.delete({ name }) // throws — prevents unfiltered delete
// Entity instance --> ignore
class Post {
id = 1
authorId: number | null = null // nullable FK
}
repo.softDelete(new Post()) // Not plain object, drop authorId, filter is just WHERE id = 1We use isPlainObject() to distinguish these cases — entity instances created with new have a class prototype and aren't plain objects.
There was a problem hiding this comment.
I'm thinking that right now the logic is a bit convoluted.
The isPlainObject check should be done before iterating through the keys (we should have an early return if it's not a plain object) and the options should determined at the beginning of the functions in a single way (and not have multiple fallback values).
Also, I'm thinking that maybe we should have two PRs, one for the plain object check which we can backport to 0.3 and one for the default value which we should not.
There was a problem hiding this comment.
Sounds good to me, @samkessaram would you like to take this ? Or would you rather me to take it?🙏
There was a problem hiding this comment.
Hey @naorpeled, yeah I wouldn't mind if you took on that. Sorry—some things have come up on my side. I can check back in after I get some stuff sorted and am happy to help wherever needed.
|
For reference, the new commit was in response to this failure: https://github.com/typeorm/typeorm/actions/runs/27433004792/job/81089129076?pr=12579#step:6:3695 |
|
Hey @samkessaram, |
Array criteria (OR semantics) were falling into the entity-instance branch and defaulting to ignore null/undefined instead of throwing. Move array handling before the plain-object/entity split and restore the correct return type.
|
Since #12595 was closed as a duplicate of this PR, here are the tested pieces from that implementation that may help finish the canonical fix:
The implementation and commits remain available in #12595. I’m happy to split or rebase the relevant tests/fixes if that is preferable to cherry-picking them. |
…he write path
When invalidWhereValuesBehavior was unset, OrmUtils.normalizeWhereCriteria
short-circuited via `if (!options) return criteria`, so update/delete/
softDelete/restore silently ran with null/undefined in plain-object criteria
instead of throwing — even though the read/find path already throws by default.
Default the options (`options ??= {}`) so the write path matches: a
null/undefined value in a plain-object (or array) criteria throws by default.
Entity instances, FindOperators, Date/Buffer and primitives still pass through
(the isPlainObject guard); configured ignore/sql-null are unchanged.
Closes #12578. Completes the approach from the closed #12579.
Co-authored-by: samkessaram <[email protected]>
…he write path
When invalidWhereValuesBehavior was unset, OrmUtils.normalizeWhereCriteria
short-circuited via `if (!options) return criteria`, so update/delete/
softDelete/restore silently ran with null/undefined in plain-object criteria
instead of throwing — even though the read/find path already throws by default.
Default the options (`options ??= {}`) so the write path matches: a
null/undefined value in a plain-object (or array) criteria throws by default.
Entity instances, FindOperators, Date/Buffer and primitives still pass through
(the isPlainObject guard); configured ignore/sql-null are unchanged.
Closes #12578. Completes the approach from the closed #12579.
Co-authored-by: samkessaram <[email protected]>
…he write path
When invalidWhereValuesBehavior was unset, OrmUtils.normalizeWhereCriteria
short-circuited via `if (!options) return criteria`, so update/delete/
softDelete/restore silently ran with null/undefined in plain-object criteria
instead of throwing — even though the read/find path already throws by default.
Default the options (`options ??= {}`) so the write path matches: a
null/undefined value in a plain-object (or array) criteria throws by default.
Entity instances, FindOperators, Date/Buffer and primitives still pass through
(the isPlainObject guard); configured ignore/sql-null are unchanged.
Closes #12578. Completes the approach from the closed #12579.
Co-authored-by: samkessaram <[email protected]>
…he write path
When invalidWhereValuesBehavior was unset, OrmUtils.normalizeWhereCriteria
short-circuited via `if (!options) return criteria`, so update/delete/
softDelete/restore silently ran with null/undefined in plain-object criteria
instead of throwing — even though the read/find path already throws by default.
Default the options (`options ??= {}`) so the write path matches: a
null/undefined value in a plain-object (or array) criteria throws by default.
Entity instances, FindOperators, Date/Buffer and primitives still pass through
(the isPlainObject guard); configured ignore/sql-null are unchanged.
Closes #12578. Completes the approach from the closed #12579.
Co-authored-by: samkessaram <[email protected]>
Fixes #12578
OrmUtils.normalizeWhereCriteria()returns criteria unvalidated wheninvalidWhereValuesBehavioris not configured, becauseif (!options) return criteriashort-circuits before the default-throw logic runs. This causesupdate()anddelete()to silently acceptundefined/nullin where conditions whilefind()correctly throws.Fix: Replace the early return with
options ??= {}so the existing fallback (?? "throw") applies.Tests: Added unit tests covering default-throw for undefined, null, nested objects, and explicit ignore.