Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.

Commit 50798f3

Browse files
os-teslaclaude
andauthored
fix(core): the TRUE-identity fold counts only the keys the loop processed (#9166)
`convertFiltersToAST` folds a filter that is nothing but TRUE-identity combinators to `undefined` (objectui#8770), and folds a filter whose every key the loop SKIPPED for a null/undefined value to `undefined` too (objectui#9020). Each fold compared its own count with `Object.keys(filter).length`. The loop's first statement skips the null/undefined keys without incrementing anything the identity fold counts, while that denominator counted them anyway. So one identity group beside one skipped key read `1 === 2`, the fold declined, and the caller's original object came back — though each of those keys, alone, folds. Behaviour decided by a sibling: adding an always-TRUE `$and: []` to a folding filter could stop it folding. The denominator is now the count of keys the loop actually processed. Both skipped spellings come along: the loop skips them with one statement, and objectui#9020 already ruled what that skip means when it is all that is left. The two counts stay apart — only one denominator moved. Unchanged and pinned as controls: the skip itself, the FALSE identity `{ $or: [] }`, an identity group beside a key that does lower, a plain filter's AST, and the object tail for `{}`, `{ a: {} }` and an empty operator map beside a skipped key. Claude-Session: https://claude.ai/code/session_01UzHd6hDYatoDn17BuwKxnZ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6214db6 commit 50798f3

8 files changed

Lines changed: 701 additions & 46 deletions
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
'@object-ui/core': patch
3+
---
4+
5+
The TRUE-identity fold now counts only the keys the loop PROCESSED, so a filter
6+
that mixes an identity group with a skipped key folds too (objectui#9030).
7+
8+
`convertFiltersToAST` folds a filter that is nothing but TRUE-identity
9+
combinators to `undefined` (objectui#8770), and folds a filter whose every key
10+
the loop SKIPPED for a `null` / `undefined` value to `undefined` too
11+
(objectui#9020). Each fold compared its own count with
12+
`Object.keys(filter).length`.
13+
14+
But the loop's first statement skips the null/undefined keys without
15+
incrementing anything the identity fold counts, while that denominator counted
16+
them anyway. So a filter carrying one identity group beside one skipped key read
17+
`1 === 2`, the fold declined, and the caller's original object came back — even
18+
though each of its keys, taken alone, folds:
19+
20+
```
21+
{ $and: [] } -> undefined folded
22+
{ b: undefined } -> undefined folded
23+
{ $and: [], b: undefined } -> { $and: [], … } did NOT fold
24+
```
25+
26+
Behaviour decided by a sibling, read the other way round: adding an always-TRUE
27+
`$and: []` to a filter that folded could stop it folding.
28+
29+
What that cost, measured on both `find()` routes of `@object-ui/data-objectstack`
30+
rather than assumed. The plain route hands a non-AST object to
31+
`client.data.find`, whose else-branch spreads its entries as query parameters, so
32+
`{ $and: [], b: undefined }` left as `?$and=` with no `filter` parameter at all —
33+
the `400 UNSUPPORTED_QUERY_PARAM` objectui#8770 exists to end. The
34+
`$expand` / `$search` route JSON-serialises the same object into `filter=`, where
35+
`{ $and: [], a: null }` arrived carrying a real `a IS NULL` predicate the
36+
converter had already decided contributes nothing. One authored filter, two row
37+
sets, and neither matched the converter's own answer for either key alone.
38+
39+
The denominator is now the count of keys the loop actually processed — every key
40+
minus the ones its own first statement skipped. Both skipped spellings come
41+
along, because the loop skips them with one statement and objectui#9020 already
42+
ruled what that skip means when it is all that is left.
43+
44+
**Unchanged, and pinned as controls in the same run:** the null/undefined skip
45+
itself (`{ a: null, s: 1 }` still lowers to `['s', '=', 1]`); the FALSE identity
46+
`{ $or: [] }`, which still selects no row; an identity group beside a key that
47+
does lower (`{ $and: [], a: 'x' }` still lowers to `['a', '=', 'x']`); a plain
48+
filter's AST; and the object tail, which still serves `{}`, an empty operator map
49+
`{ a: {} }`, and an empty operator map beside a skipped key — a key the loop
50+
ENTERED and that produced no condition is neither an identity group nor a skipped
51+
key, so no arm claims it. The two counts stay apart; only one denominator moved.
52+
53+
This supersedes one sentence in objectui#9020's own entry above, which named
54+
`{ $and: [], a: null }` as still returning the object and objectui#9030 as the
55+
open question about it.

‎packages/core/src/utils/__tests__/filter-all-skipped-9020.test.ts‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,28 @@ describe('objectui#9020 — the boundary that proves the two folds were told apa
160160
it.each([
161161
['{ $and: [], a: null }', { $and: [], a: null }],
162162
['{ $and: [], b: undefined }', { $and: [], b: undefined }],
163-
])('%s satisfies NEITHER guard and keeps the object', (_label, filter) => {
164-
// ⭐ Each guard is keyed on "EVERY key was of MY kind". One identity group
165-
// beside one skipped key satisfies neither — even though each key ALONE now
166-
// folds. A single merged counter would swallow these, which is exactly the
167-
// accidental merge this card was fenced against. Whether they should fold is
168-
// objectui#9030's open question, and neither card answers it.
169-
expect(convertFiltersToAST(filter)).toEqual(filter);
163+
])('%s folds — answered by objectui#9030, not by a merged counter', (_label, filter) => {
164+
// ⚠️ UPDATED. These used to pin the object coming back: each guard was keyed
165+
// on "EVERY key was of MY kind", so one identity group beside one skipped
166+
// key satisfied neither — even though each key ALONE folds.
167+
//
168+
// objectui#9030 answered it, and NOT by merging the counters. The identity
169+
// fold's denominator became the count of keys the loop actually PROCESSED,
170+
// which is every key minus the ones its own first statement skipped; this
171+
// card's guard still has its own count and its own arm. The control for
172+
// "not merged" is the case below, where a PROCESSED key produced no
173+
// condition and neither arm fires.
174+
expect(convertFiltersToAST(filter)).toBeUndefined();
175+
});
176+
177+
it('⭐ a PROCESSED key that produced nothing still satisfies neither guard', () => {
178+
// Where the arms are still told apart. `{ a: {} }` is entered by the loop
179+
// and pushes no condition; it is neither an identity group nor a skipped
180+
// key, so it stays in the denominator and the object comes back. A repair
181+
// that had keyed the fold on "no conditions were produced" would swallow
182+
// these — which is the accidental merge this card was fenced against.
183+
expect(convertFiltersToAST({ a: {}, b: undefined })).toEqual({ a: {}, b: undefined });
184+
expect(convertFiltersToAST({ $and: [], a: {} })).toEqual({ $and: [], a: {} });
170185
});
171186

172187
it('a nested all-skipped child was already handled and is unchanged', async () => {
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* The TRUE-identity fold counts only the keys the loop PROCESSED — objectui#9030.
11+
*
12+
* ## What was wrong
13+
*
14+
* `convertFiltersToAST` folds a filter that is nothing but TRUE-identity
15+
* combinators to `undefined` (objectui#8770), and folds a filter whose every key
16+
* the loop SKIPPED for a `null` / `undefined` value to `undefined` too
17+
* (objectui#9020). Each fold was keyed on its own count compared with
18+
* `Object.keys(filter).length`.
19+
*
20+
* But the loop's first statement skips the null/undefined keys without
21+
* incrementing anything the identity fold counts, while that denominator counted
22+
* them anyway. So a filter carrying one identity group beside one skipped key
23+
* read `1 === 2`, the fold declined, and the CALLER'S ORIGINAL OBJECT came back:
24+
*
25+
* ```
26+
* { $and: [] } => undefined folds
27+
* { b: undefined } => undefined folds
28+
* { $and: [], b: undefined } => { $and: [], … } did NOT fold
29+
* ```
30+
*
31+
* ⭐ Each key folds ALONE and the two together did not — behaviour decided by a
32+
* sibling, the hazard objectui#8555 named on this same function. Read the other
33+
* way round: adding an always-TRUE `$and: []` to a filter that folded could stop
34+
* it folding.
35+
*
36+
* ## Why BOTH skipped classes come along, and why that is not a new ruling
37+
*
38+
* The loop skips `null` and `undefined` with ONE statement, and objectui#9020
39+
* already ruled what that skip means when it is all that is left: the key
40+
* contributes no condition, so the filter constrains nothing and says so. That
41+
* ruling is what this card carries into the mixed case; it does not re-decide
42+
* it, and it does not touch the skip — `{ a: null, s: 1 }` still lowers to
43+
* `['s', '=', 1]`, pinned in the controls below.
44+
*
45+
* ⚠️ The card that filed this proposed admitting `undefined` while keeping
46+
* `null` out, on the reasoning that a null-valued key rides the `$expand` route
47+
* as a real `a IS NULL` predicate. That reasoning was measured before
48+
* objectui#9020 landed and no longer holds: `{ a: null }` no longer reaches
49+
* either route as a predicate. Keeping `null` out would ALSO have meant reading
50+
* the two spellings apart for the first time anywhere in this file, and would
51+
* have left `{ $and: [], a: null }` as the one member of the family that still
52+
* 400s on the plain route — the very defect objectui#8770 exists to end. The
53+
* two-route measurement is in
54+
* `filter-identity-skipped-mix-two-routes-9030.test.ts`.
55+
*
56+
* ## Why the assertions are shaped the way they are
57+
*
58+
* Two halves, the shape taken from objectui#8770's own file, because either
59+
* alone passes on something worse than the bug:
60+
*
61+
* - a SHAPE assertion (`toBeUndefined`) alone passes on a converter that has
62+
* stopped emitting anything at all;
63+
* - a ROW-SET assertion alone cannot tell "honoured" from "dropped" — a filter
64+
* whose ruled answer is every row and an absent filter select the same rows.
65+
*
66+
* So §0 proves the fixture distinguishes every-row from a real subset before any
67+
* of it is read that way, the shape half is asserted against the spec's own door
68+
* (`isFilterAST`) rather than a literal, and the FALSE identity `{ $or: [] }` is
69+
* carried as a control that must keep selecting NO rows.
70+
*/
71+
72+
import { describe, it, expect } from 'vitest';
73+
import { isFilterAST } from '@objectstack/spec/data';
74+
import { convertFiltersToAST, toFilterNode, mergeFilterNodes } from '../filter-converter';
75+
import { ValueDataSource } from '../../adapters/ValueDataSource';
76+
77+
const ROWS = [
78+
{ id: '1', a: 'x', s: 1 },
79+
{ id: '2', a: null, s: 1 },
80+
{ id: '3', a: 'y', s: 2 },
81+
{ id: '4', a: 'z', s: 2 },
82+
];
83+
const ALL_IDS = ['1', '2', '3', '4'];
84+
85+
async function selectedIds(filter: unknown): Promise<string[]> {
86+
const ds = new ValueDataSource({ items: ROWS as never });
87+
const result = await ds.find('rows', (filter === undefined ? {} : { $filter: filter }) as never);
88+
return (result.data as Array<{ id: string }>).map((r) => String(r.id));
89+
}
90+
91+
/**
92+
* The census, enumerated rather than exemplified — a repair aimed at the card's
93+
* one literal would be re-touched immediately.
94+
*
95+
* The family is stated as a rule, not a list: a filter that produces NO
96+
* condition and whose every key was either a TRUE-identity group or a key the
97+
* loop skipped. The rows below walk that rule's degrees of freedom — which
98+
* identity spelling, which skipped spelling, key ORDER, more than one of each,
99+
* and an identity group that only becomes one because ITS child folded.
100+
*/
101+
const MIXED_FAMILY: ReadonlyArray<[string, Record<string, unknown>]> = [
102+
['{ $and: [], b: undefined }', { $and: [], b: undefined }],
103+
['{ $and: [], a: null }', { $and: [], a: null }],
104+
['{ $or: [{}], b: undefined }', { $or: [{}], b: undefined }],
105+
['{ $and: [{}], a: null }', { $and: [{}], a: null }],
106+
['{ b: undefined, $and: [] }', { b: undefined, $and: [] }],
107+
[
108+
'{ $and: [], $or: [{}], a: null, b: undefined }',
109+
{ $and: [], $or: [{}], a: null, b: undefined },
110+
],
111+
['{ $and: [{ a: null }], b: undefined }', { $and: [{ a: null }], b: undefined }],
112+
];
113+
114+
// ---------------------------------------------------------------------------
115+
// 0. The harness has to be able to fail
116+
// ---------------------------------------------------------------------------
117+
118+
describe('objectui#9030 — harness', () => {
119+
it('every row, the `a = null` subset and a real predicate are three answers here', async () => {
120+
expect(await selectedIds(undefined)).toEqual(ALL_IDS);
121+
// The predicate the object became on one route. It is NOT "every row".
122+
expect(await selectedIds({ a: null })).toEqual(['2']);
123+
// A filter that really constrains lands strictly between the two, so "every
124+
// row" below is a measurement and not this fixture's only answer.
125+
expect(await selectedIds(['s', '=', 1])).toEqual(['1', '2']);
126+
});
127+
});
128+
129+
// ---------------------------------------------------------------------------
130+
// 1. The family folds
131+
// ---------------------------------------------------------------------------
132+
133+
describe('objectui#9030 — an identity group beside a skipped key folds', () => {
134+
it.each(MIXED_FAMILY)('%s lowers to "no constraint"', (_label, filter) => {
135+
expect(convertFiltersToAST(filter)).toBeUndefined();
136+
});
137+
138+
it.each(MIXED_FAMILY)('%s is no longer the caller\'s own object', (_label, filter) => {
139+
// Stated as identity rather than shape: what came back WAS the input, so a
140+
// caller could not tell "lowered" from "handed back untouched".
141+
expect(convertFiltersToAST(filter)).not.toBe(filter);
142+
});
143+
144+
it('nothing unreadable is handed back', () => {
145+
// The property objectui#8770 and objectui#9020 each stated for their own
146+
// member: a value this function returns is either `undefined` — no filter —
147+
// or something the spec's own door accepts. A plain object is neither.
148+
for (const [, filter] of MIXED_FAMILY) {
149+
const out = convertFiltersToAST(filter);
150+
expect(out === undefined || isFilterAST(out)).toBe(true);
151+
}
152+
});
153+
154+
it('⭐ the property the counting broke — each key alone, and the two together', () => {
155+
// The defect in one assertion, and the reason this is a counting fix rather
156+
// than a new member: both halves already folded, so their conjunction had
157+
// no third answer available to it.
158+
expect(convertFiltersToAST({ $and: [] })).toBeUndefined();
159+
expect(convertFiltersToAST({ b: undefined })).toBeUndefined();
160+
expect(convertFiltersToAST({ $and: [], b: undefined })).toBeUndefined();
161+
});
162+
});
163+
164+
// ---------------------------------------------------------------------------
165+
// 2. The sinks
166+
// ---------------------------------------------------------------------------
167+
168+
describe('objectui#9030 — the sinks', () => {
169+
it.each(MIXED_FAMILY)('%s is skipped outright by `toFilterNode`', (_label, filter) => {
170+
expect(toFilterNode(filter)).toBeUndefined();
171+
});
172+
173+
it.each(MIXED_FAMILY)('%s contributes nothing to the merged `and`', async (_label, filter) => {
174+
// What `plugin-list`'s `buildEffectiveFilter` and `plugin-view`'s
175+
// `ObjectView` build. Before the fix the object landed in AST CHILD
176+
// position — `['and', { $and: [] }, ['s', '=', 1]]` — which `isFilterAST`
177+
// refuses, so a list whose filter narrowed nothing failed to load outright.
178+
const merged = mergeFilterNodes(filter, ['s', '=', 1]);
179+
expect(merged).toEqual(['s', '=', 1]);
180+
expect(isFilterAST(merged)).toBe(true);
181+
expect(await selectedIds(merged)).toEqual(['1', '2']);
182+
});
183+
184+
it.each(MIXED_FAMILY)('%s selects EVERY row through a real matcher', async (_label, filter) => {
185+
expect(await selectedIds(toFilterNode(filter))).toEqual(ALL_IDS);
186+
});
187+
});
188+
189+
// ---------------------------------------------------------------------------
190+
// 3. Controls — everything that must NOT have moved
191+
// ---------------------------------------------------------------------------
192+
193+
describe('objectui#9030 — controls', () => {
194+
it('⭐ the skip pin itself is untouched', async () => {
195+
// ⛔ The one repair this card was forbidden: making a null-valued key
196+
// meaningful. If this reddens, every caller's row set moved.
197+
const node = convertFiltersToAST({ a: null, s: 1 });
198+
expect(node).toEqual(['s', '=', 1]);
199+
expect(await selectedIds(node)).toEqual(['1', '2']);
200+
});
201+
202+
it('⭐ a plain filter lowers to exactly the AST it always did', async () => {
203+
expect(convertFiltersToAST({ a: 'x' })).toEqual(['a', '=', 'x']);
204+
expect(convertFiltersToAST({ a: { $null: true } })).toEqual(['a', 'is_null', true]);
205+
expect(convertFiltersToAST({ a: 'x', s: 1 })).toEqual([
206+
'and',
207+
['a', '=', 'x'],
208+
['s', '=', 1],
209+
]);
210+
expect(await selectedIds(convertFiltersToAST({ a: 'x' }))).toEqual(['1']);
211+
});
212+
213+
it('⭐ an identity group beside a key that DOES lower still lets the sibling carry it', async () => {
214+
// The fold must not start swallowing a live sibling now that its
215+
// denominator is smaller.
216+
const node = convertFiltersToAST({ $and: [], a: 'x' });
217+
expect(node).toEqual(['a', '=', 'x']);
218+
expect(await selectedIds(node)).toEqual(['1']);
219+
});
220+
221+
it('⭐ the FALSE identity `{ $or: [] }` still selects NO row', async () => {
222+
// FALSE is not "no constraint", and the AST has no contradiction literal, so
223+
// the pre-existing leaf is still the emission. A fold that swept the
224+
// identities together would take this control with it.
225+
const node = convertFiltersToAST({ $or: [] });
226+
expect(node).toEqual(['$or', '=', []]);
227+
expect(await selectedIds(node)).toEqual([]);
228+
// …and it survives a skipped key and a TRUE identity beside it.
229+
expect(convertFiltersToAST({ $or: [], a: null })).toEqual(['$or', '=', []]);
230+
expect(convertFiltersToAST({ $and: [], $or: [] })).toEqual(['$or', '=', []]);
231+
});
232+
233+
it('⛔ an empty operator map still returns the original object', () => {
234+
// NOT a member. Its key was PROCESSED — the loop entered it and the operator
235+
// loop ran zero times — so it is counted in the denominator and no arm
236+
// claims it. objectui#8770 measured this boundary and this card does not
237+
// move it.
238+
expect(convertFiltersToAST({ a: {} })).toEqual({ a: {} });
239+
});
240+
241+
it('⛔ an empty operator map beside a SKIPPED key still returns the original object', () => {
242+
// ⭐ The sharpest boundary case, and the one that says what "processed"
243+
// means: subtracting the skipped key leaves one processed key that produced
244+
// no condition and is not an identity group, so neither arm fires. A repair
245+
// that had folded on "no conditions were produced" instead would swallow
246+
// this.
247+
const filter = { a: {}, b: undefined };
248+
expect(convertFiltersToAST(filter)).toEqual(filter);
249+
expect(convertFiltersToAST({ $and: [], a: {} })).toEqual({ $and: [], a: {} });
250+
});
251+
252+
it('⛔ an empty filter still returns the original object', () => {
253+
// `trueIdentityGroups > 0` and `skippedNullKeys > 0` both exclude it: there
254+
// is no key of either kind. `toFilterNode` already folds `{}` one level up.
255+
expect(convertFiltersToAST({})).toEqual({});
256+
});
257+
258+
it('⭐ objectui#8770 and objectui#9020 keep their own answers', () => {
259+
// The two pure members, restated where a reader of this file will see them:
260+
// this card widened the denominator of one arm, it did not merge the arms.
261+
expect(convertFiltersToAST({ $and: [] })).toBeUndefined();
262+
expect(convertFiltersToAST({ $or: [{}] })).toBeUndefined();
263+
expect(convertFiltersToAST({ $and: [{}] })).toBeUndefined();
264+
expect(convertFiltersToAST({ a: null })).toBeUndefined();
265+
expect(convertFiltersToAST({ b: undefined })).toBeUndefined();
266+
expect(convertFiltersToAST({ a: null, b: undefined })).toBeUndefined();
267+
});
268+
});

‎packages/core/src/utils/__tests__/filter-text-comparand-9001.test.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,18 @@ describe('objectui#9001 — what the guard must NOT touch', () => {
386386
// adds no `continue` and skips no key, and the tail is unreachable from
387387
// it. Pinned rather than argued.
388388
//
389-
// ⚠️ UPDATED by objectui#9020, which gave the all-skipped tail its own
390-
// count and its own `undefined`. That is a SECOND guard beside this one,
391-
// not a change to it: the two cases below that mix the kinds still satisfy
392-
// neither guard, which is how this control keeps measuring what it was
393-
// written to measure.
389+
// ⚠️ UPDATED TWICE. objectui#9020 gave the all-skipped tail its own count
390+
// and its own `undefined` — a SECOND guard beside this one. objectui#9030
391+
// then narrowed THIS guard's denominator to the keys the loop actually
392+
// PROCESSED, so a mixed filter folds too. Neither touched what this control
393+
// measures: a refusal that THROWS adds no `continue`, skips no key, and so
394+
// moves neither the numerator nor the denominator. The empty-operator-map
395+
// case below is the one that still reaches the object tail, and it is what
396+
// keeps this control able to see a perturbation at all.
394397
expect(convertFiltersToAST({ $and: [] })).toBeUndefined();
395398
expect(convertFiltersToAST({ $or: [{}] })).toBeUndefined();
396-
expect(convertFiltersToAST({ $and: [], a: null })).toEqual({ $and: [], a: null });
399+
expect(convertFiltersToAST({ $and: [], a: null })).toBeUndefined();
400+
expect(convertFiltersToAST({ $and: [], a: {} })).toEqual({ $and: [], a: {} });
397401
expect(convertFiltersToAST({})).toEqual({});
398402
expect(convertFiltersToAST({ a: null })).toBeUndefined();
399403
expect(convertFiltersToAST({ $and: [], name: { $icontains: 'x' } })).toEqual([

0 commit comments

Comments
 (0)