Skip to content

fix(entity-manager): default invalidWhereValuesBehavior to throw on the write path - #12690

Merged
naorpeled merged 1 commit into
masterfrom
fix/write-criteria-default-throw
Jul 10, 2026
Merged

naorpeled merged 1 commit into
masterfrom
fix/write-criteria-default-throw

Conversation

@naorpeled

@naorpeled naorpeled commented Jul 10, 2026 •

Copy link
Copy Markdown
Member

Closes #12578.

Problem

invalidWhereValuesBehavior's documented default is throw, and the read/find path already honours it — SelectQueryBuilder.buildWhere resolves each sub-option with ?? "throw". The write path did not: OrmUtils.normalizeWhereCriteria short-circuited with if (!options) return criteria when the option was unset, so update / delete / softDelete / restore with a plain-object criteria containing null/undefined ran silently (col = NULL) instead of throwing. That's the bug reported in #12578 (repro: https://github.com/samkessaram/typeorm-undefined-where-bug).

Change

Default the options inside normalizeWhereCriteria (options ??= {}), so the existing per-key options?.null ?? "throw" / ?.undefined ?? "throw" fallbacks apply. The write path now matches the read path:

  • plain object { text: null } / { text: undefined } → throws by default
  • array (OR) [{ text: null }, { id: 1 }] → throws by default
  • nested plain { category: { name: null } } → throws by default
  • entity instances / FindOperator / Date / Buffer / primitives → pass through unchanged (the isPlainObject guard from fix(query-builder): reject empty where criteria on update and delete operations #12629 runs before the key loop) — so delete(entityInstance) and the tree-repository soft-delete paths are unaffected
  • configured ignore / sql-null → unchanged

No read-path change (already correct); the #12629 empty-criteria (WHERE 1=1) guards stay.

Notes

Tests

  • Functional — new invalidWhereValuesBehavior default (unconfigured) block: delete/update/softDelete throw on null/undefined by default; array-criteria null throws; entity-instance passthrough (no throw, deletes its row); read/write parity (findBy also throws).
  • Unit — normalizeWhereCriteria now throws by default with no options (and with empty {} options); entity/Date/Uint8Array/primitive passthrough retained.
  • Full suite audited locally on better-sqlite3 (persistence, query-builder, repository, tree-tables, relations/cascades/transaction/columns/embedded) — no pre-existing unconfigured plain-object null/undefined write relied on the old silent behaviour.

@pkg-pr-new

pkg-pr-new Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm@12690
    

commit: b4ea459

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (3) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Object-valued criteria throws 🐞 Bug ≡ Correctness
Description
OrmUtils.normalizeWhereCriteria now defaults options to {} and recursively validates nested
plain objects, so write methods can throw on nested null/undefined inside object-valued column
predicates (e.g., JSON/simple-json column values). QueryBuilder generally treats
non-embedded/non-relation object values as atomic parameters, so this introduces new write-path
errors for criteria shapes that previously flowed through to the driver/transformer unchanged.
Code

src/util/OrmUtils.ts[R675-676]

+        // default to "throw" when unconfigured, matching the read/find path
+        options ??= {}
Evidence
Write methods always pass the (often undefined) DataSource option into normalizeWhereCriteria,
which now defaults to {} and recurses into *any* nested plain object, throwing on nested
null/undefined. In contrast, QueryBuilder’s property-path traversal only descends into objects when
metadata says the path is an embedded/relation; otherwise it keeps the object as the column
parameter, and SelectQueryBuilder’s buildWhere only validates the top-level value for
null/undefined before building the column predicate.

src/entity-manager/EntityManager.ts[855-858]
src/util/OrmUtils.ts[670-736]
src/query-builder/QueryBuilder.ts[1419-1501]
src/query-builder/SelectQueryBuilder.ts[4371-4440]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`OrmUtils.normalizeWhereCriteria` now runs even when `invalidWhereValuesBehavior` is unset (`options ??= {}`), and it recursively descends into any nested plain object. That means object-valued *column* predicates (e.g. JSON/simple-json values) can now throw if they contain nested `null`/`undefined` keys, even though those nested keys are not “where paths” and are typically treated as part of an atomic column value.
## Issue Context
TypeORM’s QueryBuilder traverses nested objects only when metadata indicates an embedded or relation; otherwise it treats the object as the column’s parameter value. The write-path normalization currently lacks metadata, so it can misclassify atomic object values as nested where criteria.
## Fix Focus Areas
- src/util/OrmUtils.ts[656-739]
- src/entity-manager/EntityManager.ts[822-883]
- src/query-builder/QueryBuilder.ts[1409-1503]
- src/query-builder/SelectQueryBuilder.ts[4329-4448]
## Suggested fix approach
1. Make write-path normalization metadata-aware:
 - Change `EntityManager.normalizeAndValidateWhereCriteria(...)` to accept `target` (or resolve `metadata` from `target`) and normalize using metadata.
 - Recurse into nested objects **only** when the corresponding property is an embedded or relation in the entity metadata.
 - Treat nested plain objects for regular columns (including JSON/simple-json) as atomic values (do not traverse inside them), applying invalidWhereValuesBehavior only to the top-level property value.
2. Keep existing behavior for top-level OR arrays by normalizing each branch using the same metadata.
3. Add a regression test demonstrating a JSON/simple-json column predicate containing nested null/undefined that should behave consistently (either documented as supported and not traversed, or explicitly documented/covered as throwing).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Array doc contradicts code 🐞 Bug ⚙ Maintainability
Description
OrmUtils.normalizeWhereCriteria’s JSDoc claims arrays are “returned untouched”, but the
implementation explicitly maps top-level array (OR) criteria and normalizes each branch. This
documentation mismatch can mislead maintainers/users about how array criteria are handled (including
default-throw behavior).
Code

src/util/OrmUtils.ts[R658-666]

 * each own key a null/undefined value is thrown on, skipped, or converted
 * to IsNull() per the configured behavior. Only plain FindOptionsWhere
 * objects are normalized; anything else (entity/class instances,
-     * FindOperators, arrays, Date, Buffer, primitives) — and the criteria when
-     * no behavior is configured — is returned untouched.
+     * FindOperators, arrays, Date, Buffer, primitives) is returned untouched.
+     *
+     * When invalidWhereValuesBehavior is not configured, each sub-option
+     * defaults to "throw", so a null/undefined value in a plain-object (or
+     * array) criteria throws by default — matching the read/find path.
 *
Evidence
The JSDoc explicitly lists arrays as “returned untouched”, but the function has a dedicated branch
that maps top-level array criteria and recursively normalizes each element.

src/util/OrmUtils.ts[656-700]
src/util/OrmUtils.ts[681-691]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`OrmUtils.normalizeWhereCriteria` JSDoc says arrays are passed through untouched, but the implementation maps top-level OR arrays and normalizes each element. Update the doc comment to accurately describe the behavior (e.g., “top-level OR arrays are normalized by normalizing each element; non-plain objects are passed through”).
## Issue Context
This is user-/maintainer-facing documentation on a public utility method; it should match the actual behavior, especially now that unconfigured behavior defaults to throwing.
## Fix Focus Areas
- src/util/OrmUtils.ts[656-699]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Informational

3. Extra allocations on writes 🐞 Bug ➹ Performance
Description
normalizeWhereCriteria now defaults options to {}, causing plain-object criteria to always be
traversed and rebuilt (new object) even when invalidWhereValuesBehavior is unset and no
null/undefined are present. This adds avoidable allocations and changes referential behavior for
common write calls using object criteria.
Code

src/util/OrmUtils.ts[R676-680]

+        // Default to the documented "throw" behavior when unconfigured (the
+        // per-key `options?.null ?? "throw"` / `?.undefined ?? "throw"` below
+        // then applies), so the write path matches the read/find path.
+        options ??= {}
Evidence
EntityManager always calls normalizeWhereCriteria with an optional invalidWhereValuesBehavior; with
the new change, an undefined option now becomes {} and the code proceeds to rebuild a fresh result
object for any plain-object criteria.

src/entity-manager/EntityManager.ts[822-883]
src/util/OrmUtils.ts[671-743]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
After `options ??= {}`, the function always proceeds to normalize plain-object criteria, which rebuilds a new object even when nothing needs to change. Consider adding a fast-path that returns the original `criteria` when:
- it’s a plain object,
- it has no null/undefined values,
- it contains no nested plain objects needing normalization,
- and contains no `__proto__` key.
Alternatively, explicitly document that the function returns a cloned normalized object even in the no-op case.
## Issue Context
EntityManager’s update/delete/softDelete/restore call this method on the object-criteria path, so the extra allocations occur on write operations that use object criteria.
## Fix Focus Areas
- src/util/OrmUtils.ts[671-743]
- src/entity-manager/EntityManager.ts[822-883]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

Previous review results

Review updated until commit b4ea459

Results up to commit 88d8c49


🐞 Bugs (3) 📘 Rule violations (0) 📜 Skill insights (0)


Remediation recommended
1. Object-valued criteria throws 🐞 Bug ≡ Correctness ⭐ New
Description
OrmUtils.normalizeWhereCriteria now defaults options to {} and recursively validates nested
plain objects, so write methods can throw on nested null/undefined inside object-valued column
predicates (e.g., JSON/simple-json column values). QueryBuilder generally treats
non-embedded/non-relation object values as atomic parameters, so this introduces new write-path
errors for criteria shapes that previously flowed through to the driver/transformer unchanged.
Code

src/util/OrmUtils.ts[R675-676]

+        // default to "throw" when unconfigured, matching the read/find path
+        options ??= {}
Evidence
Write methods always pass the (often undefined) DataSource option into normalizeWhereCriteria,
which now defaults to {} and recurses into *any* nested plain object, throwing on nested
null/undefined. In contrast, QueryBuilder’s property-path traversal only descends into objects when
metadata says the path is an embedded/relation; otherwise it keeps the object as the column
parameter, and SelectQueryBuilder’s buildWhere only validates the top-level value for
null/undefined before building the column predicate.

src/entity-manager/EntityManager.ts[855-858]
src/util/OrmUtils.ts[670-736]
src/query-builder/QueryBuilder.ts[1419-1501]
src/query-builder/SelectQueryBuilder.ts[4371-4440]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`OrmUtils.normalizeWhereCriteria` now runs even when `invalidWhereValuesBehavior` is unset (`options ??= {}`), and it recursively descends into any nested plain object. That means object-valued *column* predicates (e.g. JSON/simple-json values) can now throw if they contain nested `null`/`undefined` keys, even though those nested keys are not “where paths” and are typically treated as part of an atomic column value.

## Issue Context
TypeORM’s QueryBuilder traverses nested objects only when metadata indicates an embedded or relation; otherwise it treats the object as the column’s parameter value. The write-path normalization currently lacks metadata, so it can misclassify atomic object values as nested where criteria.

## Fix Focus Areas
- src/util/OrmUtils.ts[656-739]
- src/entity-manager/EntityManager.ts[822-883]
- src/query-builder/QueryBuilder.ts[1409-1503]
- src/query-builder/SelectQueryBuilder.ts[4329-4448]

## Suggested fix approach
1. Make write-path normalization metadata-aware:
  - Change `EntityManager.normalizeAndValidateWhereCriteria(...)` to accept `target` (or resolve `metadata` from `target`) and normalize using metadata.
  - Recurse into nested objects **only** when the corresponding property is an embedded or relation in the entity metadata.
  - Treat nested plain objects for regular columns (including JSON/simple-json) as atomic values (do not traverse inside them), applying invalidWhereValuesBehavior only to the top-level property value.
2. Keep existing behavior for top-level OR arrays by normalizing each branch using the same metadata.
3. Add a regression test demonstrating a JSON/simple-json column predicate containing nested null/undefined that should behave consistently (either documented as supported and not traversed, or explicitly documented/covered as throwing).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Array doc contradicts code 🐞 Bug ⚙ Maintainability
Description
OrmUtils.normalizeWhereCriteria’s JSDoc claims arrays are “returned untouched”, but the
implementation explicitly maps top-level array (OR) criteria and normalizes each branch. This
documentation mismatch can mislead maintainers/users about how array criteria are handled (including
default-throw behavior).
Code

src/util/OrmUtils.ts[R658-666]

  * each own key a null/undefined value is thrown on, skipped, or converted
  * to IsNull() per the configured behavior. Only plain FindOptionsWhere
  * objects are normalized; anything else (entity/class instances,
-     * FindOperators, arrays, Date, Buffer, primitives) — and the criteria when
-     * no behavior is configured — is returned untouched.
+     * FindOperators, arrays, Date, Buffer, primitives) is returned untouched.
+     *
+     * When invalidWhereValuesBehavior is not configured, each sub-option
+     * defaults to "throw", so a null/undefined value in a plain-object (or
+     * array) criteria throws by default — matching the read/find path.
  *
Evidence
The JSDoc explicitly lists arrays as “returned untouched”, but the function has a dedicated branch
that maps top-level array criteria and recursively normalizes each element.

src/util/OrmUtils.ts[656-700]
src/util/OrmUtils.ts[681-691]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`OrmUtils.normalizeWhereCriteria` JSDoc says arrays are passed through untouched, but the implementation maps top-level OR arrays and normalizes each element. Update the doc comment to accurately describe the behavior (e.g., “top-level OR arrays are normalized by normalizing each element; non-plain objects are passed through”).
## Issue Context
This is user-/maintainer-facing documentation on a public utility method; it should match the actual behavior, especially now that unconfigured behavior defaults to throwing.
## Fix Focus Areas
- src/util/OrmUtils.ts[656-699]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Informational
3. Extra allocations on writes 🐞 Bug ➹ Performance
Description
normalizeWhereCriteria now defaults options to {}, causing plain-object criteria to always be
traversed and rebuilt (new object) even when invalidWhereValuesBehavior is unset and no
null/undefined are present. This adds avoidable allocations and changes referential behavior for
common write calls using object criteria.
Code

src/util/OrmUtils.ts[R676-680]

+        // Default to the documented "throw" behavior when unconfigured (the
+        // per-key `options?.null ?? "throw"` / `?.undefined ?? "throw"` below
+        // then applies), so the write path matches the read/find path.
+        options ??= {}
Evidence
EntityManager always calls normalizeWhereCriteria with an optional invalidWhereValuesBehavior; with
the new change, an undefined option now becomes {} and the code proceeds to rebuild a fresh result
object for any plain-object criteria.

src/entity-manager/EntityManager.ts[822-883]
src/util/OrmUtils.ts[671-743]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
After `options ??= {}`, the function always proceeds to normalize plain-object criteria, which rebuilds a new object even when nothing needs to change. Consider adding a fast-path that returns the original `criteria` when:
- it’s a plain object,
- it has no null/undefined values,
- it contains no nested plain objects needing normalization,
- and contains no `__proto__` key.
Alternatively, explicitly document that the function returns a cloned normalized object even in the no-op case.
## Issue Context
EntityManager’s update/delete/softDelete/restore call this method on the object-criteria path, so the extra allocations occur on write operations that use object criteria.
## Fix Focus Areas
- src/util/OrmUtils.ts[671-743]
- src/entity-manager/EntityManager.ts[822-883]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Results up to commit ca58f18


🐞 Bugs (2) 📘 Rule violations (0) 📜 Skill insights (0)


Remediation recommended
1. Array doc contradicts code 🐞 Bug ⚙ Maintainability
Description
OrmUtils.normalizeWhereCriteria’s JSDoc claims arrays are “returned untouched”, but the
implementation explicitly maps top-level array (OR) criteria and normalizes each branch. This
documentation mismatch can mislead maintainers/users about how array criteria are handled (including
default-throw behavior).
Code

src/util/OrmUtils.ts[R658-666]

   * each own key a null/undefined value is thrown on, skipped, or converted
   * to IsNull() per the configured behavior. Only plain FindOptionsWhere
   * objects are normalized; anything else (entity/class instances,
-     * FindOperators, arrays, Date, Buffer, primitives) — and the criteria when
-     * no behavior is configured — is returned untouched.
+     * FindOperators, arrays, Date, Buffer, primitives) is returned untouched.
+     *
+     * When invalidWhereValuesBehavior is not configured, each sub-option
+     * defaults to "throw", so a null/undefined value in a plain-object (or
+     * array) criteria throws by default — matching the read/find path.
   *
Evidence
The JSDoc explicitly lists arrays as “returned untouched”, but the function has a dedicated branch
that maps top-level array criteria and recursively normalizes each element.

src/util/OrmUtils.ts[656-700]
src/util/OrmUtils.ts[681-691]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`OrmUtils.normalizeWhereCriteria` JSDoc says arrays are passed through untouched, but the implementation maps top-level OR arrays and normalizes each element. Update the doc comment to accurately describe the behavior (e.g., “top-level OR arrays are normalized by normalizing each element; non-plain objects are passed through”).
## Issue Context
This is user-/maintainer-facing documentation on a public utility method; it should match the actual behavior, especially now that unconfigured behavior defaults to throwing.
## Fix Focus Areas
- src/util/OrmUtils.ts[656-699]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Informational
2. Extra allocations on writes 🐞 Bug ➹ Performance
Description
normalizeWhereCriteria now defaults options to {}, causing plain-object criteria to always be
traversed and rebuilt (new object) even when invalidWhereValuesBehavior is unset and no
null/undefined are present. This adds avoidable allocations and changes referential behavior for
common write calls using object criteria.
Code

src/util/OrmUtils.ts[R676-680]

+        // Default to the documented "throw" behavior when unconfigured (the
+        // per-key `options?.null ?? "throw"` / `?.undefined ?? "throw"` below
+        // then applies), so the write path matches the read/find path.
+        options ??= {}
Evidence
EntityManager always calls normalizeWhereCriteria with an optional invalidWhereValuesBehavior; with
the new change, an undefined option now becomes {} and the code proceeds to rebuild a fresh result
object for any plain-object criteria.

src/entity-manager/EntityManager.ts[822-883]
src/util/OrmUtils.ts[671-743]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
After `options ??= {}`, the function always proceeds to normalize plain-object criteria, which rebuilds a new object even when nothing needs to change. Consider adding a fast-path that returns the original `criteria` when:
- it’s a plain object,
- it has no null/undefined values,
- it contains no nested plain objects needing normalization,
- and contains no `__proto__` key.
Alternatively, explicitly document that the function returns a cloned normalized object even in the no-op case.
## Issue Context
EntityManager’s update/delete/softDelete/restore call this method on the object-criteria path, so the extra allocations occur on write operations that use object criteria.
## Fix Focus Areas
- src/util/OrmUtils.ts[671-743]
- src/entity-manager/EntityManager.ts[822-883]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Results up to commit 416c105


🐞 Bugs (2) 📘 Rule violations (0) 📜 Skill insights (0)


Remediation recommended
1. Array doc contradicts code 🐞 Bug ⚙ Maintainability
Description
OrmUtils.normalizeWhereCriteria’s JSDoc claims arrays are “returned untouched”, but the
implementation explicitly maps top-level array (OR) criteria and normalizes each branch. This
documentation mismatch can mislead maintainers/users about how array criteria are handled (including
default-throw behavior).
Code

src/util/OrmUtils.ts[R658-666]

    * each own key a null/undefined value is thrown on, skipped, or converted
    * to IsNull() per the configured behavior. Only plain FindOptionsWhere
    * objects are normalized; anything else (entity/class instances,
-     * FindOperators, arrays, Date, Buffer, primitives) — and the criteria when
-     * no behavior is configured — is returned untouched.
+     * FindOperators, arrays, Date, Buffer, primitives) is returned untouched.
+     *
+     * When invalidWhereValuesBehavior is not configured, each sub-option
+     * defaults to "throw", so a null/undefined value in a plain-object (or
+     * array) criteria throws by default — matching the read/find path.
    *
Evidence
The JSDoc explicitly lists arrays as “returned untouched”, but the function has a dedicated branch
that maps top-level array criteria and recursively normalizes each element.

src/util/OrmUtils.ts[656-700]
src/util/OrmUtils.ts[681-691]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`OrmUtils.normalizeWhereCriteria` JSDoc says arrays are passed through untouched, but the implementation maps top-level OR arrays and normalizes each element. Update the doc comment to accurately describe the behavior (e.g., “top-level OR arrays are normalized by normalizing each element; non-plain objects are passed through”).
## Issue Context
This is user-/maintainer-facing documentation on a public utility method; it should match the actual behavior, especially now that unconfigured behavior defaults to throwing.
## Fix Focus Areas
- src/util/OrmUtils.ts[656-699]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Informational
2. Extra allocations on writes 🐞 Bug ➹ Performance
Description
normalizeWhereCriteria now defaults options to {}, causing plain-object criteria to always be
traversed and rebuilt (new object) even when invalidWhereValuesBehavior is unset and no
null/undefined are present. This adds avoidable allocations and changes referential behavior for
common write calls using object criteria.
Code

src/util/OrmUtils.ts[R676-680]

+        // Default to the documented "throw" behavior when unconfigured (the
+        // per-key `options?.null ?? "throw"` / `?.undefined ?? "throw"` below
+        // then applies), so the write path matches the read/find path.
+        options ??= {}
Evidence
EntityManager always calls normalizeWhereCriteria with an optional invalidWhereValuesBehavior; with
the new change, an undefined option now becomes {} and the code proceeds to rebuild a fresh result
object for any plain-object criteria.

src/entity-manager/EntityManager.ts[822-883]
src/util/OrmUtils.ts[671-743]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
After `options ??= {}`, the function always proceeds to normalize plain-object criteria, which rebuilds a new object even when nothing needs to change. Consider adding a fast-path that returns the original `criteria` when:
- it’s a plain object,
- it has no null/undefined values,
- it contains no nested plain objects needing normalization,
- and contains no `__proto__` key.
Alternatively, explicitly document that the function returns a cloned normalized object even in the no-op case.
## Issue Context
EntityManager’s update/delete/softDelete/restore call this method on the object-criteria path, so the extra allocations occur on write operations that use object criteria.
## Fix Focus Areas
- src/util/OrmUtils.ts[671-743]
- src/entity-manager/EntityManager.ts[822-883]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Results up to commit b16a51f


🐞 Bugs (2) 📘 Rule violations (0) 📜 Skill insights (0)


Remediation recommended
1. Array doc contradicts code 🐞 Bug ⚙ Maintainability
Description
OrmUtils.normalizeWhereCriteria’s JSDoc claims arrays are “returned untouched”, but the
implementation explicitly maps top-level array (OR) criteria and normalizes each branch. This
documentation mismatch can mislead maintainers/users about how array criteria are handled (including
default-throw behavior).
Code

src/util/OrmUtils.ts[R658-666]

     * each own key a null/undefined value is thrown on, skipped, or converted
     * to IsNull() per the configured behavior. Only plain FindOptionsWhere
     * objects are normalized; anything else (entity/class instances,
-     * FindOperators, arrays, Date, Buffer, primitives) — and the criteria when
-     * no behavior is configured — is returned untouched.
+     * FindOperators, arrays, Date, Buffer, primitives) is returned untouched.
+     *
+     * When invalidWhereValuesBehavior is not configured, each sub-option
+     * defaults to "throw", so a null/undefined value in a plain-object (or
+     * array) criteria throws by default — matching the read/find path.
     *
Evidence
The JSDoc explicitly lists arrays as “returned untouched”, but the function has a dedicated branch
that maps top-level array criteria and recursively normalizes each element.

src/util/OrmUtils.ts[656-700]
src/util/OrmUtils.ts[681-691]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`OrmUtils.normalizeWhereCriteria` JSDoc says arrays are passed through untouched, but the implementation maps top-level OR arrays and normalizes each element. Update the doc comment to accurately describe the behavior (e.g., “top-level OR arrays are normalized by normalizing each element; non-plain objects are passed through”).

## Issue Context
This is user-/maintainer-facing documentation on a public utility method; it should match the actual behavior, especially now that unconfigured behavior defaults to throwing.

## Fix Focus Areas
- src/util/OrmUtils.ts[656-699]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Informational
2. Extra allocations on writes 🐞 Bug ➹ Performance
Description
normalizeWhereCriteria now defaults options to {}, causing plain-object criteria to always be
traversed and rebuilt (new object) even when invalidWhereValuesBehavior is unset and no
null/undefined are present. This adds avoidable allocations and changes referential behavior for
common write calls using object criteria.
Code

src/util/OrmUtils.ts[R676-680]

+        // Default to the documented "throw" behavior when unconfigured (the
+        // per-key `options?.null ?? "throw"` / `?.undefined ?? "throw"` below
+        // then applies), so the write path matches the read/find path.
+        options ??= {}
Evidence
EntityManager always calls normalizeWhereCriteria with an optional invalidWhereValuesBehavior; with
the new change, an undefined option now becomes {} and the code proceeds to rebuild a fresh result
object for any plain-object criteria.

src/entity-manager/EntityManager.ts[822-883]
src/util/OrmUtils.ts[671-743]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
After `options ??= {}`, the function always proceeds to normalize plain-object criteria, which rebuilds a new object even when nothing needs to change. Consider adding a fast-path that returns the original `criteria` when:
- it’s a plain object,
- it has no null/undefined values,
- it contains no nested plain objects needing normalization,
- and contains no `__proto__` key.
Alternatively, explicitly document that the function returns a cloned normalized object even in the no-op case.

## Issue Context
EntityManager’s update/delete/softDelete/restore call this method on the object-criteria path, so the extra allocations occur on write operations that use object criteria.

## Fix Focus Areas
- src/util/OrmUtils.ts[671-743]
- src/entity-manager/EntityManager.ts[822-883]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Qodo Logo

@naorpeled
naorpeled force-pushed the fix/write-criteria-default-throw branch 3 times, most recently from ca58f18 to 88d8c49 Compare July 10, 2026 17:15
…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]>
@naorpeled
naorpeled force-pushed the fix/write-criteria-default-throw branch from 88d8c49 to b4ea459 Compare July 10, 2026 17:39
@sonarqubecloud

Copy link
Copy Markdown

@naorpeled
naorpeled requested review from alumni and smith-xyz July 10, 2026 18:26

@smith-xyz smith-xyz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

one thing to maybe note is that increment/decrement might bypass this error? Not sure if that was pre-existing or needs adjusted here

@naorpeled
naorpeled merged commit 44d8052 into master Jul 10, 2026
86 of 88 checks passed
@naorpeled
naorpeled deleted the fix/write-criteria-default-throw branch July 10, 2026 22:33
@github-actions github-actions Bot added this to the 1.0 milestone Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

OrmUtils.normalizeWhereCriteria() does not implement described default behaviour for invalidWhereValuesBehavior

3 participants