diff --git a/.changeset/34389-lookup-record-path-multi-valued.md b/.changeset/34389-lookup-record-path-multi-valued.md new file mode 100644 index 00000000000..68fcf63ec2c --- /dev/null +++ b/.changeset/34389-lookup-record-path-multi-valued.md @@ -0,0 +1,14 @@ +--- +'@object-ui/fields': patch +--- + +fix(fields): a lookupFilters `{ $record }` path through or to a multi-valued field is left out of the candidate query, with a development warning + +Clause-②: no +Metadata-protocol: none + +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. + +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. + +Single-valued paths, literal entries and `dependsOn` query exactly as before. diff --git a/ui/packages/fields/src/widgets/LookupField.recordReference-34197.test.tsx b/ui/packages/fields/src/widgets/LookupField.recordReference-34197.test.tsx index f8a28947a23..e83686ce4b7 100644 --- a/ui/packages/fields/src/widgets/LookupField.recordReference-34197.test.tsx +++ b/ui/packages/fields/src/widgets/LookupField.recordReference-34197.test.tsx @@ -302,3 +302,123 @@ describe('lookupFiltersToRecord — an unresolved { $record } never reaches the expect(out[1]).toEqual({ field: 'valid_from', operator: 'lte', value: '2026-11-01' }); }); }); + +/** + * A `{ $record }` path through or to a multi-valued field has no single value + * (the spec's `LookupFilterRecordReferenceSchema` rule). The picker leaves its + * entry out — never sends the list as a scalar comparand, never drops it + * unsaid — and a development build warns once, naming the path. + */ +describe('LookupField — a { $record } path through or to a multi-valued field (#34389)', () => { + const warnings = (spy: { mock: { calls: unknown[][] } }) => spy.mock.calls.map((c) => String(c[0])); + + it('a hop through a multiple: true lookup is left out, with a warning naming the path as multi-valued', async () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + try { + const ds = makeDataSource({ 'account:a1': { id: 'a1', cb_market: 'm1' } }); + const field = formField( + [ + { field: 'is_active', operator: 'eq', value: true }, + { field: 'cb_market', operator: 'eq', value: { $record: 'accounts.cb_market' } }, + ], + { accounts: { type: 'lookup', reference: 'account', multiple: true } }, + ); + render( + , + ); + await openPopover(); + await waitFor(() => expect(candidateFilters(ds)).toHaveLength(1)); + expect(candidateFilters(ds)[0]).toEqual({ is_active: true }); + expect(ds.findOne).not.toHaveBeenCalled(); + const said = warnings(warn).filter((m) => m.includes('"accounts.cb_market"')); + expect(said).toHaveLength(1); + expect(said[0]).toContain('multi-valued'); + expect(said[0]).toContain('"accounts"'); + } finally { + warn.mockRestore(); + } + }); + + it('a plain path to a multi-valued field is left out, not sent as a list, with a warning naming the field', async () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + try { + const ds = makeDataSource(); + render( + , + ); + await openPopover(); + await waitFor(() => expect(candidateFilters(ds)).toHaveLength(1)); + expect(candidateFilters(ds)[0]).toEqual({ is_active: true }); + const said = warnings(warn).filter((m) => m.includes('{ $record: "tags" }')); + expect(said).toHaveLength(1); + expect(said[0]).toContain('multi-valued'); + } finally { + warn.mockRestore(); + } + }); + + it('a hop ending at a multi-valued field of the row is left out, with a warning naming that field', async () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + try { + const ds = makeDataSource({ 'account:a1': { id: 'a1', markets: ['m1', 'm2'] } }); + const field = formField( + [ + { field: 'is_active', operator: 'eq', value: true }, + { field: 'cb_market', operator: 'eq', value: { $record: 'account.markets' } }, + ], + { account: { type: 'lookup', reference: 'account' } }, + ); + render( + , + ); + await openPopover(); + await waitFor(() => expect(candidateFilters(ds)).toHaveLength(1)); + expect(ds.findOne).toHaveBeenCalledWith('account', 'a1', { $select: ['id', 'markets'] }); + expect(candidateFilters(ds)[0]).toEqual({ is_active: true }); + const said = warnings(warn).filter((m) => m.includes('"account.markets"')); + expect(said).toHaveLength(1); + expect(said[0]).toContain('multi-valued'); + expect(said[0]).toContain('"markets"'); + } finally { + warn.mockRestore(); + } + }); + + it('CONTROL: single-valued paths — a field and a one-hop path — still resolve, with no warning', async () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + try { + const ds = makeDataSource({ 'account:a3': { id: 'a3', cb_market: 'm3' } }); + const field = formField( + [ + { field: 'segment', operator: 'eq', value: { $record: 'segment_code' } }, + { field: 'cb_market', operator: 'eq', value: { $record: 'account.cb_market' } }, + ], + { account: { type: 'lookup', reference: 'account' } }, + ); + render( + , + ); + await openPopover(); + await waitFor(() => expect(candidateFilters(ds)).toHaveLength(1)); + expect(candidateFilters(ds)[0]).toEqual({ segment: 'enterprise', cb_market: 'm3' }); + expect(warnings(warn).filter((m) => m.includes('[LookupField]'))).toEqual([]); + } finally { + warn.mockRestore(); + } + }); +}); diff --git a/ui/packages/fields/src/widgets/lookupRecordReference.ts b/ui/packages/fields/src/widgets/lookupRecordReference.ts index 1f4e7535ceb..85d3abbee49 100644 --- a/ui/packages/fields/src/widgets/lookupRecordReference.ts +++ b/ui/packages/fields/src/widgets/lookupRecordReference.ts @@ -25,9 +25,15 @@ * - Only a stamp `withRecordBeingEdited` issued counts. The key written as * data — on a document's form field, an action's params — is no stamp, so * no host is opted in by what it is handed. - * - An entry whose referenced value is empty — null, unset, `''`, an empty - * list, a hop whose lookup is unset — is left out of the query, the rule an - * unset `dependsOn` parent follows. + * - An entry whose referenced value is empty — null, unset, `''`, a hop whose + * lookup is unset — is left out of the query, the rule an unset `dependsOn` + * parent follows. + * - A path through or to a multi-valued field — the record or the hop's row + * holds a list there (`accounts.cb_market` through a `multiple: true` + * lookup, `tags`, `account.markets`) — has no single value to compare, the + * spec's rule (`LookupFilterRecordReferenceSchema`): its entry is left out, + * never sent with the list as a scalar comparand, and a development build + * says so once per path. * - A hop whose row cannot be read is left out the same way: the picker * narrows the choice only, and the save is the server's to judge. * - ⛔ No `{ $record }` value ever reaches the wire: `lookupFiltersToRecord` @@ -170,10 +176,11 @@ export function readRecordBeingEdited(field: unknown): RecordBeingEdited | undef } /** - * The value a `{ $record }` path names, or `undefined` when it is empty or - * cannot be read. `hopRows` holds the rows read for each hop's lookup, by - * `via`; an expanded row on the record itself is read first, so a host that - * already holds the related record costs no request. + * The value a `{ $record }` path names, or `undefined` when it is empty, + * 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 `via`; an + * expanded row on the record itself is read first, so a host that already + * holds the related record costs no request. */ function readRecordPath( path: string, @@ -182,22 +189,28 @@ function readRecordPath( ): unknown { const dot = path.indexOf('.'); if (dot < 0) { - const v = collapseReference(record[path]); + const own = record[path]; + if (Array.isArray(own)) return warnMultiValuedPath(path, `"${path}" holds a list`); + const v = collapseReference(own); return isEmptyValue(v) ? undefined : v; } const via = path.slice(0, dot); const field = path.slice(dot + 1); const held = record[via]; + if (Array.isArray(held)) return warnMultiValuedPath(path, `the lookup "${via}" holds a list of records`); if (isEmptyValue(held)) return undefined; const row = isRow(held) && Object.prototype.hasOwnProperty.call(held, field) ? held : hopRows[via]; if (!row) return undefined; - const v = collapseReference(row[field]); + const end = row[field]; + if (Array.isArray(end)) return warnMultiValuedPath(path, `"${field}" on the "${via}" row holds a list`); + const v = collapseReference(end); return isEmptyValue(v) ? undefined : v; } /** * `filters` with every `{ $record }` value replaced by the value it names on - * `record`, and every entry whose value is empty or unreadable left out. + * `record`, and every entry whose value is empty, unreadable or multi-valued + * left out. * Literal entries pass through untouched, in order. */ export function resolveLookupFilters( @@ -218,24 +231,45 @@ export function resolveLookupFilters( return out; } -const warnedHops = new Set(); +const warned = new Set(); /** - * Said once per hop: the entry is left out, so the picker offers more than the - * author wrote — visible here rather than only as a wider list. + * The development-build warning for an entry left out of the candidate query, + * said once per `key`: the picker then offers more than the author wrote — + * visible here rather than only as a wider list. */ -function warnUnreadableHop(via: string, field: string, object: string | undefined): void { +function warnLeftOut(key: string, path: string, reason: string): void { if ((globalThis as { process?: { env?: { NODE_ENV?: string } } }).process?.env?.NODE_ENV === 'production') return; - const key = `${via}.${field}`; - if (warnedHops.has(key)) return; - warnedHops.add(key); - console.warn( - `[LookupField] lookupFilters { $record: "${key}" } is left out of the candidate query: ` - + (object - ? `no data source here can read ${object} rows.` - : `the record holds "${via}" as an id, and this host did not name the object it points at ` - + `(a record form stamps it with withRecordBeingEdited).`), + if (warned.has(key)) return; + warned.add(key); + console.warn(`[LookupField] lookupFilters { $record: "${path}" } is left out of the candidate query: ${reason}`); +} + +/** Said once per hop whose row cannot be read. */ +function warnUnreadableHop(via: string, field: string, object: string | undefined): void { + const path = `${via}.${field}`; + warnLeftOut( + `hop:${path}`, + path, + object + ? `no data source here can read ${object} rows.` + : `the record holds "${via}" as an id, and this host did not name the object it points at ` + + `(a record form stamps it with withRecordBeingEdited).`, + ); +} + +/** + * Said once per path through or to a multi-valued field; answers `undefined`, + * the value such a path resolves to. + */ +function warnMultiValuedPath(path: string, where: string): undefined { + warnLeftOut( + `multi:${path}`, + path, + `the path is multi-valued — ${where} — and a { $record } path names a single value. ` + + 'Point it at a single-valued field.', ); + return undefined; } interface HopSource {