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

Commit 964d953

Browse files
os-teslaclaude
andauthored
fix(app-shell): bridge Is null to the spec's $null instead of erasing the filter (#9371)
`groupToCondition` had no row for `isNull` / `isNotNull`, so an `Is null` row fell through to the unmapped-operator drop. A dropped last row makes the function return `undefined`, and the dataset inspector commits on every change, so switching the only condition's operator to an ordinary menu entry committed `{ filter: undefined }` and destroyed the stored `dataset.filter`. Nothing errored and the panel still showed the condition. Both directions now carry the spec's `$null` predicate, kept distinct from the `$exists` pair the two `isEmpty` rows already used. Operators this bridge does not map are still dropped rather than emitted in a spelling that means something else; that behaviour and the list of offered-but-unmappable operators are pinned alongside the fix. Claude-Session: https://claude.ai/code/session_01UzHd6hDYatoDn17BuwKxnZ Co-authored-by: Claude <noreply@anthropic.com>
1 parent dab9f96 commit 964d953

3 files changed

Lines changed: 312 additions & 3 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
'@object-ui/app-shell': patch
3+
---
4+
5+
Stop the Studio dataset-filter bridge from ERASING a stored filter when the author
6+
picks `Is null` (objectui#9363).
7+
8+
`groupToCondition` had no mapping for `isNull` / `isNotNull`, so those rows fell
9+
through to the unmapped-operator drop. A dropped last row makes the function return
10+
`undefined`, and the dataset inspector commits on every change — so an author with a
11+
working `dataset.filter` (or a `measure.filter`) who opened the filter popover and
12+
switched the single condition's operator to **Is null** committed `undefined`, and the
13+
persisted filter was destroyed. Nothing errored and the panel still showed the
14+
condition. `Is null` is an ordinary entry in that menu, not an opt-in one.
15+
16+
Both directions now bridge the spec's `$null` predicate: `isNull` serializes to
17+
`{ field: { $null: true } }` and `isNotNull` to `{ $null: false }`, and a stored
18+
`$null` reads back as the operator the author picked instead of degrading the whole
19+
filter to "edit it in the Source tab".
20+
21+
`$null` stays distinct from `$exists`: `isEmpty` / `isNotEmpty` are unchanged, because
22+
the dropdown offers both pairs as their own rows and the spec's filter vocabulary
23+
carries both predicates.
24+
25+
Operators this bridge still does not map are still dropped rather than emitted in a
26+
spelling that means something else — that behaviour is deliberate and is now pinned
27+
alongside the fix, together with the list of operators the menu offers and this bridge
28+
cannot store (`notContains`, `between`, `startsWith`, `endsWith`), so the next unmapped
29+
addition fails a test instead of erasing a filter.
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* `Is null` must not ERASE the stored dataset filter (objectui#9363).
5+
*
6+
* ## The mechanism, and why it is data loss rather than a wrong result set
7+
*
8+
* `groupToCondition` returns `undefined` when no row survives serialization,
9+
* and the Studio dataset inspector commits on EVERY change — the filter popover
10+
* mounts the shared `FilterBuilder` and hands each emitted group straight to
11+
* `onCommit(groupToCondition(g))`, which lands as `onPatch({ filter })` and is
12+
* spread over the draft. So an author with a working `dataset.filter` who opens
13+
* the inspector and switches the single condition's operator to **Is null**
14+
* commits `undefined`: the persisted key is destroyed, nothing errors, and the
15+
* panel still shows the condition.
16+
*
17+
* The inspector passes no `extraOperators`, so `isNull` / `isNotNull` are
18+
* ordinary entries in the default menu — not opt-in ones. The parity block at
19+
* the bottom of this file reads that offering from the builder's own exported
20+
* bucket function rather than restating it.
21+
*
22+
* ## The distinction this file exists to keep
23+
*
24+
* The `continue` these two operators fell into is a DELIBERATE decision for
25+
* operators the bridge does not map: dropping beats emitting a filter that
26+
* means something else. That behaviour is kept, and pinned below, because a
27+
* repair that made the fallback stop dropping everything it cannot map would
28+
* emit wrong filters — a worse defect than this one.
29+
*
30+
* `isNull` / `isNotNull` are a different case and that is the whole repair: the
31+
* dialect CAN express them (the spec's own `$null`, asserted below against
32+
* `FILTER_OPERATORS` rather than assumed), the builder draws them as COMPLETE
33+
* rows with no value input, and this inspector offers them. The drop was an
34+
* unhandled operator falling into the fallback's path, not the fallback doing
35+
* its job. Both readings are pinned so they stay distinguishable.
36+
*
37+
* ## Red-first
38+
*
39+
* Before the repair, the two `$null` expectations fail with `undefined` while
40+
* the `equals` control in the same run passes — a table of all-`undefined`
41+
* answers and a dead function are otherwise indistinguishable.
42+
*/
43+
import { describe, it, expect } from 'vitest';
44+
import { FILTER_OPERATORS, FieldOperatorsSchema } from '@objectstack/spec/data';
45+
import {
46+
FILTER_BUILDER_OPERATORS,
47+
VALUELESS_FILTER_BUILDER_OPERATORS,
48+
operatorsForFieldType,
49+
} from '@object-ui/components';
50+
import { groupToCondition, conditionToGroup } from './datasetFilterCondition';
51+
import type { BuilderGroup } from './datasetFilterCondition';
52+
53+
/** One condition row, as the builder emits it. */
54+
const row = (operator: string, value: unknown = ''): BuilderGroup => ({
55+
id: 'g',
56+
logic: 'and',
57+
conditions: [{ id: 'c1', field: 'closed_at', operator, value }],
58+
});
59+
60+
describe('groupToCondition — the null predicates this inspector offers (objectui#9363)', () => {
61+
it('CONTROL: a mapped operator still serializes, so an empty answer below is about that operator', () => {
62+
expect(groupToCondition(row('equals', 'acme'))).toEqual({ closed_at: { $eq: 'acme' } });
63+
});
64+
65+
it('isNull serializes to the dialect\'s null predicate instead of vanishing', () => {
66+
expect(
67+
groupToCondition(row('isNull')),
68+
'an `Is null` row serialized to nothing; committing that ERASES dataset.filter',
69+
).toEqual({ closed_at: { $null: true } });
70+
});
71+
72+
it('isNotNull serializes to the same predicate negated', () => {
73+
expect(groupToCondition(row('isNotNull'))).toEqual({ closed_at: { $null: false } });
74+
});
75+
76+
it('keeps a null row alongside a complete one instead of dropping either', () => {
77+
expect(groupToCondition({
78+
id: 'g',
79+
logic: 'and',
80+
conditions: [
81+
{ id: 'c1', field: 'stage', operator: 'equals', value: 'won' },
82+
{ id: 'c2', field: 'closed_at', operator: 'isNull', value: '' },
83+
],
84+
})).toEqual({ $and: [{ stage: { $eq: 'won' } }, { closed_at: { $null: true } }] });
85+
});
86+
87+
it('THE DEFECT: switching the only row of a stored filter to Is null no longer commits `undefined`', () => {
88+
// The exact author gesture: a dataset that already has a filter, opened in
89+
// the inspector, one operator change. `undefined` here is not "unchanged" —
90+
// it is what the host spreads over the draft as `{ filter: undefined }`,
91+
// the same patch shape `objectChangePatch` uses to CLEAR the filter.
92+
const stored = { stage: { $eq: 'won' } };
93+
const { group, representable } = conditionToGroup(stored);
94+
expect(representable).toBe(true);
95+
const edited: BuilderGroup = {
96+
...group,
97+
conditions: [{ ...group.conditions[0], operator: 'isNull', value: '' }],
98+
};
99+
expect(
100+
groupToCondition(edited),
101+
'the commit for this gesture was `undefined`, which erases the stored filter',
102+
).toEqual({ stage: { $null: true } });
103+
});
104+
105+
it('leaves the $exists pair exactly as it was', () => {
106+
expect(groupToCondition(row('isEmpty'))).toEqual({ closed_at: { $exists: false } });
107+
expect(groupToCondition(row('isNotEmpty'))).toEqual({ closed_at: { $exists: true } });
108+
});
109+
110+
it('still drops an operator it does not map, rather than emitting a wrong filter', () => {
111+
// Deliberate, and kept: see this file's header. These four are OFFERED by
112+
// the menu and dropped, which erases the same way — tracked as its own
113+
// finding, not widened here on the way past.
114+
expect(groupToCondition(row('notContains', 'a'))).toBeUndefined();
115+
expect(groupToCondition(row('between', [1, 5]))).toBeUndefined();
116+
expect(groupToCondition(row('startsWith', 'a'))).toBeUndefined();
117+
expect(groupToCondition(row('endsWith', 'a'))).toBeUndefined();
118+
});
119+
120+
it('an empty group is still `undefined` — that is the author CLEARING the filter', () => {
121+
expect(groupToCondition({ id: 'g', logic: 'and', conditions: [] })).toBeUndefined();
122+
});
123+
});
124+
125+
describe('the emitted token is the spec\'s, not a local invention (objectui#9363)', () => {
126+
it('`$null` is a member of the spec\'s filter operator vocabulary', () => {
127+
expect(FILTER_OPERATORS).toContain('$null');
128+
// Negative control: membership is a real reading, not a list that contains
129+
// everything. A plausible-looking spelling this bridge could have invented
130+
// is NOT in it.
131+
expect(FILTER_OPERATORS).not.toContain('$isNull');
132+
});
133+
134+
it('the spec\'s field-operator door accepts the boolean comparand and refuses a wrong one', () => {
135+
expect(FieldOperatorsSchema.safeParse({ $null: true }).success).toBe(true);
136+
expect(FieldOperatorsSchema.safeParse({ $null: false }).success).toBe(true);
137+
// Negative control: this door judges the VALUE, so a string comparand is
138+
// refused — without this leg the assertion above would pass for a schema
139+
// that accepts anything.
140+
expect(FieldOperatorsSchema.safeParse({ $null: 'yes' }).success).toBe(false);
141+
});
142+
});
143+
144+
describe('conditionToGroup — the read half round-trips the new shape (objectui#9363)', () => {
145+
it('reads a stored $null back as the operator the author picked', () => {
146+
expect(conditionToGroup({ closed_at: { $null: true } })).toEqual({
147+
group: { id: 'g', logic: 'and', conditions: [{ id: 'c0', field: 'closed_at', operator: 'isNull', value: '' }] },
148+
representable: true,
149+
});
150+
expect(conditionToGroup({ closed_at: { $null: false } }).group.conditions[0].operator)
151+
.toBe('isNotNull');
152+
});
153+
154+
it('round-trips condition → group → condition', () => {
155+
for (const c of [{ closed_at: { $null: true } }, { closed_at: { $null: false } }]) {
156+
const { group, representable } = conditionToGroup(c);
157+
expect(representable, `${JSON.stringify(c)} fell back to the source editor`).toBe(true);
158+
expect(groupToCondition(group)).toEqual(c);
159+
}
160+
});
161+
});
162+
163+
/**
164+
* Offered ⇄ expressible parity for THIS inspector.
165+
*
166+
* The direction that broke: every guard in the repo sweeps spec → objectui,
167+
* asking whether an operator an author may DECLARE can be rendered. None asks
168+
* whether an operator this dropdown OFFERS can be stored by the consumer that
169+
* mounted it — and that is the direction where an unmapped operator becomes
170+
* silent data loss rather than a rendering gap.
171+
*
172+
* The offering is read from the builder's own bucket function with NO
173+
* `extraOperators`, which is exactly what `DatasetFilterField` passes, so a
174+
* future opt-in granted at that call site has to come through here.
175+
*/
176+
const PROBE_FIELD_TYPES: ReadonlyArray<string | undefined> = [
177+
undefined, 'text', 'a_type_this_builder_has_never_heard_of', 'number', 'currency',
178+
'percent', 'rating', 'boolean', 'date', 'datetime', 'time', 'select', 'status',
179+
'lookup', 'master_detail', 'user',
180+
];
181+
182+
function offeredAcrossBuckets(extra: readonly string[]): string[] {
183+
const ids = new Set<string>();
184+
for (const type of PROBE_FIELD_TYPES) for (const op of operatorsForFieldType(type, extra)) ids.add(op.value);
185+
return [...ids].sort();
186+
}
187+
188+
/** What the dataset inspector's filter popover offers. */
189+
const OFFERED = offeredAcrossBuckets([]);
190+
191+
/**
192+
* Offered, and deliberately NOT expressible by this bridge today.
193+
*
194+
* Each one drops on commit, and a drop of the last surviving row erases the
195+
* stored filter — the same mechanism objectui#9363 fixed for the null pair.
196+
* They are listed rather than fixed here because mapping them is a separate
197+
* decision per operator (`between` needs a both-bounds-present rule before it
198+
* can be emitted at all), and a blanket "stop dropping" would emit filters that
199+
* mean something else. Mapping one is what makes this list shrink — and this
200+
* assertion go red until it is updated.
201+
*/
202+
const DECLARED_UNEXPRESSIBLE = ['between', 'endsWith', 'notContains', 'startsWith'];
203+
204+
/** A value that keeps a row from being dropped as INCOMPLETE, per operator. */
205+
function probeValue(operator: string): unknown {
206+
if (VALUELESS_FILTER_BUILDER_OPERATORS.has(operator)) return '';
207+
if (operator === 'in' || operator === 'notIn') return ['a', 'b'];
208+
if (operator === 'between') return [1, 5];
209+
return 'x';
210+
}
211+
212+
describe('every operator this inspector OFFERS is either expressible or declared (objectui#9363)', () => {
213+
it('the probe types cover every bucket, so the offering below is the whole dropdown', () => {
214+
// Granting every id as an opt-in yields the full drawable vocabulary only
215+
// if the probe list reaches every bucket. Without this, a bucket added
216+
// later would silently shrink what the partition below is asserted over.
217+
expect(offeredAcrossBuckets(FILTER_BUILDER_OPERATORS)).toEqual([...FILTER_BUILDER_OPERATORS].sort());
218+
});
219+
220+
it('partitions the offering exactly — no operator is silently unhandled', () => {
221+
const expressible: string[] = [];
222+
const dropped: string[] = [];
223+
for (const operator of OFFERED) {
224+
(groupToCondition(row(operator, probeValue(operator))) === undefined ? dropped : expressible)
225+
.push(operator);
226+
}
227+
expect(dropped.sort()).toEqual(DECLARED_UNEXPRESSIBLE);
228+
// The other half of the equality: every remaining offered id serializes.
229+
expect(expressible.sort()).toEqual(OFFERED.filter((o) => !DECLARED_UNEXPRESSIBLE.includes(o)));
230+
// And the null pair is on the expressible side — the card's defect, stated
231+
// as a fact about the offering rather than about two literals.
232+
expect(expressible).toContain('isNull');
233+
expect(expressible).toContain('isNotNull');
234+
});
235+
});

‎packages/app-shell/src/views/metadata-admin/inspectors/datasetFilterCondition.ts‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
* (nested groups, `$or`, multi-operator objects, unmapped operators) is reported
1212
* as NOT representable so the caller can fall back to the source editor instead
1313
* of silently corrupting the author's filter.
14+
*
15+
* The value-less operators are the exception to "field op value": the builder
16+
* draws no input for them, so the row is complete without one. Both pairs the
17+
* spec's vocabulary carries — `$exists` (is empty) and `$null` (is null) — are
18+
* bridged here, in {@link VALUELESS_TO_MONGO}.
1419
*/
1520

1621
/** FilterBuilder camelCase operator → FilterCondition Mongo operator. */
@@ -26,6 +31,32 @@ const MONGO_TO_OP: Record<string, string> = {
2631
$contains: 'contains', $in: 'in', $nin: 'notIn',
2732
};
2833

34+
/**
35+
* Value-less builder operators, and the predicate each one lowers to.
36+
*
37+
* A row carrying one of these is COMPLETE without a value — the builder draws
38+
* no input for it — so they are matched ahead of the value-completeness check
39+
* in {@link groupToCondition}, not after it.
40+
*
41+
* `isNull` / `isNotNull` are not a spelling of `isEmpty` / `isNotEmpty`. The
42+
* dropdown offers both pairs as their own rows and the spec's filter vocabulary
43+
* carries both `$null` and `$exists`, so they stay distinct in both directions;
44+
* collapsing them would draw two labels for one wire predicate and rewrite the
45+
* author's choice when the filter is read back.
46+
*
47+
* objectui#9363: the null pair was missing here, so an `Is null` row — an
48+
* ordinary entry in this inspector's menu, drawn as a finished row — fell
49+
* through to the unmapped-operator `continue` below and was dropped. Dropping
50+
* the last surviving row makes this function return `undefined`, and the
51+
* inspector commits that as `{ filter: undefined }`, the same patch shape used
52+
* to CLEAR the filter. So picking the entry erased the author's stored filter,
53+
* with no error and the condition still on screen.
54+
*/
55+
const VALUELESS_TO_MONGO: Record<string, Record<string, boolean>> = {
56+
isEmpty: { $exists: false }, isNotEmpty: { $exists: true },
57+
isNull: { $null: true }, isNotNull: { $null: false },
58+
};
59+
2960
export interface BuilderCondition { id?: string; field: string; operator: string; value?: unknown }
3061
export interface BuilderGroup { id?: string; logic: 'and' | 'or'; conditions: BuilderCondition[] }
3162

@@ -53,10 +84,17 @@ export function groupToCondition(group: BuilderGroup | undefined): FilterConditi
5384
const conds = (group?.conditions ?? []).filter((c) => c && c.field);
5485
const parts: FilterCondition[] = [];
5586
for (const c of conds) {
56-
if (c.operator === 'isEmpty') { parts.push({ [c.field]: { $exists: false } }); continue; }
57-
if (c.operator === 'isNotEmpty') { parts.push({ [c.field]: { $exists: true } }); continue; }
87+
const valueless = VALUELESS_TO_MONGO[c.operator];
88+
if (valueless) { parts.push({ [c.field]: { ...valueless } }); continue; }
5889
const mop = OP_TO_MONGO[c.operator];
59-
if (!mop) continue; // unmapped (e.g. notContains/between) — drop rather than emit a bad filter
90+
// Still dropped rather than emitted in a spelling that means something
91+
// else. ⚠️ The drop is not free: it is what erases the stored filter when
92+
// no other row survives (see VALUELESS_TO_MONGO), and this menu offers
93+
// `notContains` / `between` / `startsWith` / `endsWith`, none of which this
94+
// table maps. Mapping one is a per-operator decision — `between` needs a
95+
// both-bounds-present rule before it can be emitted at all — so they are
96+
// declared, and pinned, in `datasetFilterCondition.nullOperators-9363`.
97+
if (!mop) continue;
6098
// Skip incomplete rows (no value typed yet) — emitting `{field:{$op:''}}` would
6199
// be a silently-wrong filter (matches only empty), not "no filter".
62100
const v = c.value;
@@ -96,6 +134,13 @@ export function conditionToGroup(cond: FilterCondition | undefined | null): { gr
96134
const mop = opKeys[0];
97135
if (mop === '$exists') {
98136
conditions.push({ id: `c${i}`, field, operator: v.$exists ? 'isNotEmpty' : 'isEmpty', value: '' });
137+
} else if (mop === '$null') {
138+
// The inverse of the write half: `$null: false` is "is not null", so
139+
// the boolean picks the operator rather than becoming the row's value.
140+
// Without this arm a filter this bridge now WRITES would read back as
141+
// non-representable, sending the author to the Source tab for a row the
142+
// builder can draw.
143+
conditions.push({ id: `c${i}`, field, operator: v.$null ? 'isNull' : 'isNotNull', value: '' });
99144
} else {
100145
const op = MONGO_TO_OP[mop];
101146
if (!op) return { group: empty, representable: false };

0 commit comments

Comments
 (0)