Skip to content

Commit e90f4f4

Browse files
committed
fix(rest): the one-request record page accepts a related list keyed by a user field
The related-list check in record-page-routes.ts kept a private lookup/master_detail list and read `reference` raw, so a related list on sys_user keyed by a `user` field answered 400 invalid_relationship though its row query follows that field (18.3.0 regression, duly#185). It now asks the spec's one arbiter, referenceTargetOf, the judgement the engine's $expand and delete walk read. A field that does not reference the page's object is still a located 400. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzijwvEjcKQArbvuX9p
1 parent 40fa433 commit e90f4f4

3 files changed

Lines changed: 105 additions & 10 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@objectstack/rest': patch
3+
---
4+
5+
fix(rest): the one-request record page accepts a related list keyed by a `user` field again
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
- **Regression in 18.3.0 (#29946, duly#185).** `POST /data/:object/:id/page` (#27713) checked each related list's field against a private `lookup`/`master_detail` list and read `reference` raw, so a related list on `sys_user` keyed by a `user` field (an owner, an assignee) answered `400 VALIDATION_FAILED … is not a lookup or master-detail field that references 'sys_user'`, and the console's full-page record view showed "could not load this record". The related list's own row query (`{ [field]: id }`) follows that field, and 18.2.0 read each related list through that list read, with no type check.
11+
- The check now asks the spec's one arbiter of a field's target, `referenceTargetOf` (`lookup`, `master_detail`, `user`, `tree`; a `user` field's target is fixed to `sys_user` by its type, whether or not its `reference` spells it), the same judgement the engine's `$expand` and delete walk read. A field that does not reference the page's object is still a located `400 invalid_relationship`: a `user` field on any other object's page, a `lookup` to another object, a `tree` on another object, an unreadable `reference` carrier. The message now reads `… is not a lookup, master-detail, user or tree field that references '<object>'.`

‎packages/rest/src/record-page-routes.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,74 @@ describe('[#27627] POST /data/:object/:id/page', () => {
319319
expect(findData).not.toHaveBeenCalled();
320320
});
321321

322+
describe('[#29946] a user field relates its child to sys_user, as the row query follows it', () => {
323+
const SYS_USER = { name: 'sys_user', label: 'User', fields: { id: { name: 'id', type: 'text' } } };
324+
const TASK = {
325+
name: 'task',
326+
label: 'Task',
327+
fields: {
328+
id: { name: 'id', type: 'text' },
329+
// A bare `Field.user` as a raw registration spells it: no carrier.
330+
owner: { name: 'owner', type: 'user' },
331+
// ...and as the spec's parse materializes it.
332+
assignee: { name: 'assignee', type: 'user', reference: 'sys_user' },
333+
reviewers: { name: 'reviewers', type: 'user', multiple: true },
334+
parent: { name: 'parent', type: 'tree', reference: 'task' },
335+
account: { name: 'account', type: 'lookup', reference: 'account' },
336+
},
337+
};
338+
const items = { ...ITEMS, sys_user: SYS_USER, task: TASK };
339+
340+
it('a related list on sys_user keyed by a user field answers 200 with its rows (duly#185)', async () => {
341+
const { call, findData } = boot({ items });
342+
const answer = await call(
343+
{ related: [{ object: 'task', field: 'owner', top: 5 }, { object: 'task', field: 'assignee', top: 5 }, { object: 'task', field: 'reviewers', top: 5 }] },
344+
{},
345+
{ object: 'sys_user', id: 'u9' },
346+
);
347+
expect(answer.status).toBe(200);
348+
const data = RecordPageResponseSchema.parse(answer.body.data);
349+
expect(data.related).toEqual([
350+
expect.objectContaining({ object: 'task', field: 'owner', total: 7, records: [{ id: 'task-1' }] }),
351+
expect.objectContaining({ object: 'task', field: 'assignee', total: 7 }),
352+
expect.objectContaining({ object: 'task', field: 'reviewers', total: 7 }),
353+
]);
354+
expect(findData.mock.calls.map(([req]: any) => req.query.where)).toEqual([
355+
{ owner: 'u9' },
356+
{ assignee: 'u9' },
357+
{ reviewers: { $contains: 'u9' } },
358+
]);
359+
});
360+
361+
it('a user field on a page that is not sys_user is still a located 400, naming the kinds it accepts', async () => {
362+
const { call, getData, findData } = boot({ items });
363+
const answer = await call({ related: [{ object: 'task', field: 'owner', top: 1 }] });
364+
expect(answer.status).toBe(400);
365+
expect(answer.body.error.details.fields).toEqual([{
366+
field: 'related.0.field',
367+
code: 'invalid_relationship',
368+
message: "'owner' on 'task' is not a lookup, master-detail, user or tree field that references 'account'.",
369+
}]);
370+
expect(getData).not.toHaveBeenCalled();
371+
expect(findData).not.toHaveBeenCalled();
372+
});
373+
374+
it('a lookup keeps relating only to its own reference, and a tree to its own object', async () => {
375+
const { call } = boot({ items });
376+
expect((await call({ related: [{ object: 'task', field: 'account', top: 1 }] })).status).toBe(200);
377+
expect((await call({ related: [{ object: 'task', field: 'account', top: 1 }] }, {}, { object: 'sys_user', id: 'u9' })).status).toBe(400);
378+
expect((await call({ related: [{ object: 'task', field: 'parent', top: 1 }] }, {}, { object: 'task', id: 't1' })).status).toBe(200);
379+
expect((await call({ related: [{ object: 'task', field: 'parent', top: 1 }] })).status).toBe(400);
380+
});
381+
382+
it('an unreadable reference carrier is not a relationship: the located 400, not a thrown 500', async () => {
383+
const odd = { ...TASK, fields: { ...TASK.fields, bad: { name: 'bad', type: 'lookup', reference: { object: 'account' } } } };
384+
const answer = await boot({ items: { ...items, task: odd } }).call({ related: [{ object: 'task', field: 'bad', top: 1 }] });
385+
expect(answer.status).toBe(400);
386+
expect(answer.body.error.details.fields).toEqual([expect.objectContaining({ code: 'invalid_relationship' })]);
387+
});
388+
});
389+
322390
it('a declared filter the list read refuses is a located 400 before anything is read', async () => {
323391
const { call, getData, findData } = boot();
324392
const answer = await call({ related: [{ object: 'note', field: 'account', top: 1, filter: { title: { $in: 'x' } } }] });

‎packages/rest/src/record-page-routes.ts‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
type FindDataRequest,
4343
type RecordPageRequest,
4444
} from '@objectstack/spec/api';
45-
import { isMultiValueField, parseFilterAST } from '@objectstack/spec/data';
45+
import { isMultiValueField, parseFilterAST, referenceTargetOf } from '@objectstack/spec/data';
4646
import { measureServerTiming } from '@objectstack/observability';
4747
import { sendError, sendOk } from '@objectstack/types';
4848
import { recordNotFoundError } from '@objectstack/core';
@@ -54,9 +54,6 @@ import { apiAccessDenialFromEnable, type RestServer } from './rest-server.js';
5454
/** The closed query-parameter set of `POST {basePath}/data/:object/:id/page`: EMPTY. */
5555
export const RECORD_PAGE_PARAMS: readonly string[] = [];
5656

57-
/** The relationship field types a related list follows back to its parent. */
58-
const RELATIONSHIP_TYPES = new Set(['lookup', 'master_detail']);
59-
6057
/** [#28470] The objects the record's stream parts read. */
6158
const COMMENT_OBJECT = 'sys_comment';
6259
const COMMENT_REACTION_OBJECT = 'sys_comment_reaction';
@@ -120,10 +117,29 @@ function fieldDefOf(item: any, name: string): any | undefined {
120117
return undefined;
121118
}
122119

123-
/** The target object a relationship field declares (`reference`, the spec's one spelling). */
124-
function referenceOf(def: any): string | undefined {
125-
const ref = def?.reference;
126-
return typeof ref === 'string' && ref ? ref : undefined;
120+
/**
121+
* [#29946] The object a related list's field points back at, by the spec's ONE
122+
* arbiter of a field's target ({@link referenceTargetOf}): the reference value
123+
* types (`lookup`, `master_detail`, `user`, `tree`), with a `user` field's
124+
* target fixed to `sys_user` by its type whatever its carrier spells. It is the
125+
* judgement the engine's own reads ask (`$expand`, the referential delete
126+
* walk), so the related list's row query (`{ [field]: id }`) and this check
127+
* cannot disagree about which fields relate a child to its parent.
128+
*
129+
* 18.3.0 kept a private `lookup`/`master_detail` list here and read `reference`
130+
* raw, so a related list on `sys_user` keyed by a `user` field (an owner, an
131+
* assignee) was a 400 `invalid_relationship` the row query would have answered.
132+
*
133+
* A carrier no reader can read (an object- or array-valued `reference` that
134+
* never went through parse) throws in the arbiter; here it is simply not a
135+
* relationship to this object, the located 400 it was before.
136+
*/
137+
function referenceOf(def: unknown): string | undefined {
138+
try {
139+
return referenceTargetOf(def);
140+
} catch {
141+
return undefined;
142+
}
127143
}
128144

129145
/**
@@ -201,11 +217,11 @@ export function registerRecordPageEndpoints(this: RecordPageRouteHost, basePath:
201217
const item = childItems[index];
202218
if (!item || apiAccessDenialFromEnable(item.enable, entry.object, 'list')) return;
203219
const def = fieldDefOf(item, entry.field);
204-
if (!def || !RELATIONSHIP_TYPES.has(String(def.type)) || referenceOf(def) !== object) {
220+
if (!def || referenceOf(def) !== object) {
205221
malformed.push({
206222
field: `related.${index}.field`,
207223
code: 'invalid_relationship',
208-
message: `'${entry.field}' on '${entry.object}' is not a lookup or master-detail field that references '${object}'.`,
224+
message: `'${entry.field}' on '${entry.object}' is not a lookup, master-detail, user or tree field that references '${object}'.`,
209225
});
210226
}
211227
});

0 commit comments

Comments
 (0)