Skip to content

Commit 518baca

Browse files
fix(fields): a lookupFilters { $record } path through or to a multi-valued field is left out with a dev warning (#34618)
<!-- claude-projects-attribution: {"github_login":"os-steve"} --> _Requested by **Steve** · [project thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi99ewj5JRWXTB3u38E9HLhnj)_ Fixes #34389 Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:查找字段选择器遇到 `lookupFilters` 里 `{ $record }` 路径经过或指向多值字段(多选关联、tags、关联行上的多值字段)时,不再把列表当标量发出,也不再静默丢弃;该条过滤一律不发,并在开发构建里告警一次,点名路径和多值字段。 - 为什么改:以前直取多值字段会把整个列表塞进 eq 之类的标量比较,经多值关联跳转则悄悄丢掉,作者看不出过滤没生效;规格(#34378 已合)写明这类路径不解析。 - 风险与回滚:只影响写了多值 `$record` 路径的选择器(lint 本就拒收这种写法);单值路径、字面条目、dependsOn 行为不变。回滚即撤销本 PR。 - 先看:`ui/packages/fields/src/widgets/lookupRecordReference.ts` 的 `readRecordPath` 三处 `Array.isArray` 守卫与 `warnMultiValuedPath`;再看测试文件新增的 describe。 - 两分钟验证:`pnpm --filter @object-ui/fields exec vitest run --root ../.. packages/fields/src/widgets/LookupField.recordReference-34197.test.tsx -t multi-valued`,预期 4 个用例全绿(三种形状 + CONTROL)。 - 需要你做的:无,席位落地。 ## What changes `readRecordPath` (the picker's reader for a `lookupFilters` `{ $record }` path) now treats a list found anywhere on the path as a multi-valued path: - a plain path to a multi-valued field (`tags`) — before: sent with the whole list as the comparand of a scalar operator; - a hop through a `multiple: true` lookup (`accounts.cb_market`) — before: left out silently (`referencedId` finds no id in a list, so neither a read nor the unreadable-hop warning happened); - a hop that ends at a multi-valued field of the hop's row (`account.markets`) — before: sent as a list. In all three the entry is left out of the candidate query and a development build warns once per path, through the file's existing dev-warning channel (now `warnLeftOut`, shared with the unreadable-hop warning; production builds stay silent as before). The warning names the path, says it is multi-valued and names where the list sits (the field, or the lookup that holds a list of records). The spec half (merged on main) says in `LookupFilterRecordReferenceSchema`'s describe that such a path "has no single value: it is not resolved"; the picker now does exactly that. No `packages/spec/**` change. ## Tests New `describe` in the existing harness `ui/packages/fields/src/widgets/LookupField.recordReference-34197.test.tsx` (one shared harness per component): one case per shape (entry left out of the candidate query, warning given naming the path and the field) and a CONTROL (a single-valued field path and a single-valued one-hop path still resolve, no `[LookupField]` warning). No existing test was changed. <details><summary>Package run, type-check and ablations (fix head 2a3ed4f; after the main merge to bc61296121 the card test file re-ran 16/16 on a rebuilt spec)</summary> - `pnpm --filter '@object-ui/fields^...' build` (dependency closure, under the verify lock): VERDICT command-exit 0. - `@object-ui/fields` `pnpm run type-check` (`tsc --noEmit && tsc -p tsconfig.test.json`; the test file is in the test program, counted with `--listFiles`): VERDICT command-exit 0. - `@object-ui/fields` `pnpm test`: Test Files 307 passed, 1 skipped (308); Tests 4649 passed, 7 skipped (4656); VERDICT command-exit 0. - Ablations through `scripts/ablation-replace.mjs` (fix committed first; each guard disabled alone as `if (false && Array.isArray(...))`, anchor 1 -> 0 on disk, restore proven blob == HEAD and `git diff HEAD` empty). The test imports `./LookupField` from source, so no dist is involved: - plain-path guard off: "a plain path to a multi-valued field…" red; 1 failed, 3 passed. - hop-lookup guard off: "a hop through a multiple: true lookup…" red; 1 failed, 3 passed. - hop-end guard off: "a hop ending at a multi-valued field of the row…" red; 1 failed, 3 passed. - The CONTROL stayed green in all three. </details> ## Acceptance notes - Detection reads the VALUE (a list on the record or on the hop's row), not field metadata (`isMultiValueField`). The widget has no field map for the hop's target object, so a value read is the one signal available for all three shapes. Consequence: a multi-valued field that is currently null or unset is left out as an empty value without the multi-valued warning (the entry is still left out; `os lint` refuses the path at authoring time). An empty list `[]` now warns as multi-valued instead of being left out silently as empty. - The warning is said once per path per page load (module-level set, as the existing unreadable-hop warning already did). ## Verification Dispatch-gate derivation (`node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack`) run on the final head; every derived command run locally and reconciled with `--ran`. Families CI owns are read from this PR's check-runs. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01ADHkmSYVw1sqCsaHSJS9JC --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 4ec9cc2 commit 518baca

3 files changed

Lines changed: 191 additions & 23 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@object-ui/fields': patch
3+
---
4+
5+
fix(fields): a lookupFilters `{ $record }` path through or to a multi-valued field is left out of the candidate query, with a development warning
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
A `{ $record }` path through or to a multi-valued field has no single value to compare: a hop through a `multiple: true` lookup (`accounts.cb_market`), a plain path to a multi-valued field (`tags`), or a hop that ends at one (`account.markets`). The spec already states that such a path is not resolved and that lint refuses it. The lookup picker now does the same in all three shapes. It leaves the entry out of the candidate query and, in a development build, warns once per path. The warning names the path and the field that holds a list, and says the path is multi-valued.
11+
12+
Before, a hop through a multi-valued lookup was left out with no warning. A plain path to a multi-valued field was sent with the whole list as the comparand of a scalar operator.
13+
14+
Single-valued paths, literal entries and `dependsOn` query exactly as before.

‎ui/packages/fields/src/widgets/LookupField.recordReference-34197.test.tsx‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,123 @@ describe('lookupFiltersToRecord — an unresolved { $record } never reaches the
302302
expect(out[1]).toEqual({ field: 'valid_from', operator: 'lte', value: '2026-11-01' });
303303
});
304304
});
305+
306+
/**
307+
* A `{ $record }` path through or to a multi-valued field has no single value
308+
* (the spec's `LookupFilterRecordReferenceSchema` rule). The picker leaves its
309+
* entry out — never sends the list as a scalar comparand, never drops it
310+
* unsaid — and a development build warns once, naming the path.
311+
*/
312+
describe('LookupField — a { $record } path through or to a multi-valued field (#34389)', () => {
313+
const warnings = (spy: { mock: { calls: unknown[][] } }) => spy.mock.calls.map((c) => String(c[0]));
314+
315+
it('a hop through a multiple: true lookup is left out, with a warning naming the path as multi-valued', async () => {
316+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
317+
try {
318+
const ds = makeDataSource({ 'account:a1': { id: 'a1', cb_market: 'm1' } });
319+
const field = formField(
320+
[
321+
{ field: 'is_active', operator: 'eq', value: true },
322+
{ field: 'cb_market', operator: 'eq', value: { $record: 'accounts.cb_market' } },
323+
],
324+
{ accounts: { type: 'lookup', reference: 'account', multiple: true } },
325+
);
326+
render(
327+
<LookupField field={field} value={undefined} onChange={vi.fn()} dataSource={ds} dependentValues={{ accounts: ['a1', 'a2'] }} />,
328+
);
329+
await openPopover();
330+
await waitFor(() => expect(candidateFilters(ds)).toHaveLength(1));
331+
expect(candidateFilters(ds)[0]).toEqual({ is_active: true });
332+
expect(ds.findOne).not.toHaveBeenCalled();
333+
const said = warnings(warn).filter((m) => m.includes('"accounts.cb_market"'));
334+
expect(said).toHaveLength(1);
335+
expect(said[0]).toContain('multi-valued');
336+
expect(said[0]).toContain('"accounts"');
337+
} finally {
338+
warn.mockRestore();
339+
}
340+
});
341+
342+
it('a plain path to a multi-valued field is left out, not sent as a list, with a warning naming the field', async () => {
343+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
344+
try {
345+
const ds = makeDataSource();
346+
render(
347+
<LookupField
348+
field={formField([
349+
{ field: 'is_active', operator: 'eq', value: true },
350+
{ field: 'segment', operator: 'eq', value: { $record: 'tags' } },
351+
])}
352+
value={undefined}
353+
onChange={vi.fn()}
354+
dataSource={ds}
355+
dependentValues={{ tags: ['enterprise', 'emea'] }}
356+
/>,
357+
);
358+
await openPopover();
359+
await waitFor(() => expect(candidateFilters(ds)).toHaveLength(1));
360+
expect(candidateFilters(ds)[0]).toEqual({ is_active: true });
361+
const said = warnings(warn).filter((m) => m.includes('{ $record: "tags" }'));
362+
expect(said).toHaveLength(1);
363+
expect(said[0]).toContain('multi-valued');
364+
} finally {
365+
warn.mockRestore();
366+
}
367+
});
368+
369+
it('a hop ending at a multi-valued field of the row is left out, with a warning naming that field', async () => {
370+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
371+
try {
372+
const ds = makeDataSource({ 'account:a1': { id: 'a1', markets: ['m1', 'm2'] } });
373+
const field = formField(
374+
[
375+
{ field: 'is_active', operator: 'eq', value: true },
376+
{ field: 'cb_market', operator: 'eq', value: { $record: 'account.markets' } },
377+
],
378+
{ account: { type: 'lookup', reference: 'account' } },
379+
);
380+
render(
381+
<LookupField field={field} value={undefined} onChange={vi.fn()} dataSource={ds} dependentValues={{ account: 'a1' }} />,
382+
);
383+
await openPopover();
384+
await waitFor(() => expect(candidateFilters(ds)).toHaveLength(1));
385+
expect(ds.findOne).toHaveBeenCalledWith('account', 'a1', { $select: ['id', 'markets'] });
386+
expect(candidateFilters(ds)[0]).toEqual({ is_active: true });
387+
const said = warnings(warn).filter((m) => m.includes('"account.markets"'));
388+
expect(said).toHaveLength(1);
389+
expect(said[0]).toContain('multi-valued');
390+
expect(said[0]).toContain('"markets"');
391+
} finally {
392+
warn.mockRestore();
393+
}
394+
});
395+
396+
it('CONTROL: single-valued paths — a field and a one-hop path — still resolve, with no warning', async () => {
397+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
398+
try {
399+
const ds = makeDataSource({ 'account:a3': { id: 'a3', cb_market: 'm3' } });
400+
const field = formField(
401+
[
402+
{ field: 'segment', operator: 'eq', value: { $record: 'segment_code' } },
403+
{ field: 'cb_market', operator: 'eq', value: { $record: 'account.cb_market' } },
404+
],
405+
{ account: { type: 'lookup', reference: 'account' } },
406+
);
407+
render(
408+
<LookupField
409+
field={field}
410+
value={undefined}
411+
onChange={vi.fn()}
412+
dataSource={ds}
413+
dependentValues={{ segment_code: 'enterprise', account: 'a3' }}
414+
/>,
415+
);
416+
await openPopover();
417+
await waitFor(() => expect(candidateFilters(ds)).toHaveLength(1));
418+
expect(candidateFilters(ds)[0]).toEqual({ segment: 'enterprise', cb_market: 'm3' });
419+
expect(warnings(warn).filter((m) => m.includes('[LookupField]'))).toEqual([]);
420+
} finally {
421+
warn.mockRestore();
422+
}
423+
});
424+
});

‎ui/packages/fields/src/widgets/lookupRecordReference.ts‎

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@
2525
* - Only a stamp `withRecordBeingEdited` issued counts. The key written as
2626
* data — on a document's form field, an action's params — is no stamp, so
2727
* no host is opted in by what it is handed.
28-
* - An entry whose referenced value is empty — null, unset, `''`, an empty
29-
* list, a hop whose lookup is unset — is left out of the query, the rule an
30-
* unset `dependsOn` parent follows.
28+
* - An entry whose referenced value is empty — null, unset, `''`, a hop whose
29+
* lookup is unset — is left out of the query, the rule an unset `dependsOn`
30+
* parent follows.
31+
* - A path through or to a multi-valued field — the record or the hop's row
32+
* holds a list there (`accounts.cb_market` through a `multiple: true`
33+
* lookup, `tags`, `account.markets`) — has no single value to compare, the
34+
* spec's rule (`LookupFilterRecordReferenceSchema`): its entry is left out,
35+
* never sent with the list as a scalar comparand, and a development build
36+
* says so once per path.
3137
* - A hop whose row cannot be read is left out the same way: the picker
3238
* narrows the choice only, and the save is the server's to judge.
3339
* - ⛔ No `{ $record }` value ever reaches the wire: `lookupFiltersToRecord`
@@ -170,10 +176,11 @@ export function readRecordBeingEdited(field: unknown): RecordBeingEdited | undef
170176
}
171177

172178
/**
173-
* The value a `{ $record }` path names, or `undefined` when it is empty or
174-
* cannot be read. `hopRows` holds the rows read for each hop's lookup, by
175-
* `via`; an expanded row on the record itself is read first, so a host that
176-
* already holds the related record costs no request.
179+
* The value a `{ $record }` path names, or `undefined` when it is empty,
180+
* cannot be read, or is multi-valued (a list on the record or the hop's
181+
* row). `hopRows` holds the rows read for each hop's lookup, by `via`; an
182+
* expanded row on the record itself is read first, so a host that already
183+
* holds the related record costs no request.
177184
*/
178185
function readRecordPath(
179186
path: string,
@@ -182,22 +189,28 @@ function readRecordPath(
182189
): unknown {
183190
const dot = path.indexOf('.');
184191
if (dot < 0) {
185-
const v = collapseReference(record[path]);
192+
const own = record[path];
193+
if (Array.isArray(own)) return warnMultiValuedPath(path, `"${path}" holds a list`);
194+
const v = collapseReference(own);
186195
return isEmptyValue(v) ? undefined : v;
187196
}
188197
const via = path.slice(0, dot);
189198
const field = path.slice(dot + 1);
190199
const held = record[via];
200+
if (Array.isArray(held)) return warnMultiValuedPath(path, `the lookup "${via}" holds a list of records`);
191201
if (isEmptyValue(held)) return undefined;
192202
const row = isRow(held) && Object.prototype.hasOwnProperty.call(held, field) ? held : hopRows[via];
193203
if (!row) return undefined;
194-
const v = collapseReference(row[field]);
204+
const end = row[field];
205+
if (Array.isArray(end)) return warnMultiValuedPath(path, `"${field}" on the "${via}" row holds a list`);
206+
const v = collapseReference(end);
195207
return isEmptyValue(v) ? undefined : v;
196208
}
197209

198210
/**
199211
* `filters` with every `{ $record }` value replaced by the value it names on
200-
* `record`, and every entry whose value is empty or unreadable left out.
212+
* `record`, and every entry whose value is empty, unreadable or multi-valued
213+
* left out.
201214
* Literal entries pass through untouched, in order.
202215
*/
203216
export function resolveLookupFilters(
@@ -218,24 +231,45 @@ export function resolveLookupFilters(
218231
return out;
219232
}
220233

221-
const warnedHops = new Set<string>();
234+
const warned = new Set<string>();
222235

223236
/**
224-
* Said once per hop: the entry is left out, so the picker offers more than the
225-
* author wrote — visible here rather than only as a wider list.
237+
* The development-build warning for an entry left out of the candidate query,
238+
* said once per `key`: the picker then offers more than the author wrote —
239+
* visible here rather than only as a wider list.
226240
*/
227-
function warnUnreadableHop(via: string, field: string, object: string | undefined): void {
241+
function warnLeftOut(key: string, path: string, reason: string): void {
228242
if ((globalThis as { process?: { env?: { NODE_ENV?: string } } }).process?.env?.NODE_ENV === 'production') return;
229-
const key = `${via}.${field}`;
230-
if (warnedHops.has(key)) return;
231-
warnedHops.add(key);
232-
console.warn(
233-
`[LookupField] lookupFilters { $record: "${key}" } is left out of the candidate query: `
234-
+ (object
235-
? `no data source here can read ${object} rows.`
236-
: `the record holds "${via}" as an id, and this host did not name the object it points at `
237-
+ `(a record form stamps it with withRecordBeingEdited).`),
243+
if (warned.has(key)) return;
244+
warned.add(key);
245+
console.warn(`[LookupField] lookupFilters { $record: "${path}" } is left out of the candidate query: ${reason}`);
246+
}
247+
248+
/** Said once per hop whose row cannot be read. */
249+
function warnUnreadableHop(via: string, field: string, object: string | undefined): void {
250+
const path = `${via}.${field}`;
251+
warnLeftOut(
252+
`hop:${path}`,
253+
path,
254+
object
255+
? `no data source here can read ${object} rows.`
256+
: `the record holds "${via}" as an id, and this host did not name the object it points at `
257+
+ `(a record form stamps it with withRecordBeingEdited).`,
258+
);
259+
}
260+
261+
/**
262+
* Said once per path through or to a multi-valued field; answers `undefined`,
263+
* the value such a path resolves to.
264+
*/
265+
function warnMultiValuedPath(path: string, where: string): undefined {
266+
warnLeftOut(
267+
`multi:${path}`,
268+
path,
269+
`the path is multi-valued — ${where} — and a { $record } path names a single value. `
270+
+ 'Point it at a single-valued field.',
238271
);
272+
return undefined;
239273
}
240274

241275
interface HopSource {

0 commit comments

Comments
 (0)