Skip to content

Commit 26466fb

Browse files
committed
fix(fields): a lookupFilters { $record } path through or to a multi-valued field is left out with a dev warning
A hop through a multiple: true lookup was dropped unsaid, and a plain path to a multi-valued field sent its list as a scalar comparand. Both now leave the entry out and warn once per path in a development build, the spec's rule that such a path has no single value. Claude-Session: https://claude.ai/code/session_01ADHkmSYVw1sqCsaHSJS9JC Co-authored-by: Claude <noreply@anthropic.com>
1 parent 1e4656d commit 26466fb

2 files changed

Lines changed: 174 additions & 21 deletions

File tree

‎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: 54 additions & 21 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,8 +176,8 @@ 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
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 row). `hopRows` holds the rows read for each hop's lookup, by
175181
* `via`; an expanded row on the record itself is read first, so a host that
176182
* already holds the related record costs no request.
177183
*/
@@ -182,22 +188,28 @@ function readRecordPath(
182188
): unknown {
183189
const dot = path.indexOf('.');
184190
if (dot < 0) {
185-
const v = collapseReference(record[path]);
191+
const own = record[path];
192+
if (Array.isArray(own)) return warnMultiValuedPath(path, `"${path}" holds a list`);
193+
const v = collapseReference(own);
186194
return isEmptyValue(v) ? undefined : v;
187195
}
188196
const via = path.slice(0, dot);
189197
const field = path.slice(dot + 1);
190198
const held = record[via];
199+
if (Array.isArray(held)) return warnMultiValuedPath(path, `the lookup "${via}" holds a list of records`);
191200
if (isEmptyValue(held)) return undefined;
192201
const row = isRow(held) && Object.prototype.hasOwnProperty.call(held, field) ? held : hopRows[via];
193202
if (!row) return undefined;
194-
const v = collapseReference(row[field]);
203+
const end = row[field];
204+
if (Array.isArray(end)) return warnMultiValuedPath(path, `"${field}" on the "${via}" row holds a list`);
205+
const v = collapseReference(end);
195206
return isEmptyValue(v) ? undefined : v;
196207
}
197208

198209
/**
199210
* `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.
211+
* `record`, and every entry whose value is empty, unreadable or multi-valued
212+
* left out.
201213
* Literal entries pass through untouched, in order.
202214
*/
203215
export function resolveLookupFilters(
@@ -218,24 +230,45 @@ export function resolveLookupFilters(
218230
return out;
219231
}
220232

221-
const warnedHops = new Set<string>();
233+
const warned = new Set<string>();
222234

223235
/**
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.
236+
* The development-build warning for an entry left out of the candidate query,
237+
* said once per `key`: the picker then offers more than the author wrote —
238+
* visible here rather than only as a wider list.
226239
*/
227-
function warnUnreadableHop(via: string, field: string, object: string | undefined): void {
240+
function warnLeftOut(key: string, path: string, reason: string): void {
228241
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).`),
242+
if (warned.has(key)) return;
243+
warned.add(key);
244+
console.warn(`[LookupField] lookupFilters { $record: "${path}" } is left out of the candidate query: ${reason}`);
245+
}
246+
247+
/** Said once per hop whose row cannot be read. */
248+
function warnUnreadableHop(via: string, field: string, object: string | undefined): void {
249+
const path = `${via}.${field}`;
250+
warnLeftOut(
251+
`hop:${path}`,
252+
path,
253+
object
254+
? `no data source here can read ${object} rows.`
255+
: `the record holds "${via}" as an id, and this host did not name the object it points at `
256+
+ `(a record form stamps it with withRecordBeingEdited).`,
257+
);
258+
}
259+
260+
/**
261+
* Said once per path through or to a multi-valued field; answers `undefined`,
262+
* the value such a path resolves to.
263+
*/
264+
function warnMultiValuedPath(path: string, where: string): undefined {
265+
warnLeftOut(
266+
`multi:${path}`,
267+
path,
268+
`the path is multi-valued — ${where} — and a { $record } path names a single value. `
269+
+ 'Point it at a single-valued field.',
238270
);
271+
return undefined;
239272
}
240273

241274
interface HopSource {

0 commit comments

Comments
 (0)