From 3c18a3788a71a8089cd18a952521b1ca09aed84d Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 15:46:56 +0000 Subject: [PATCH 1/2] fix(studio): show, keep and author the optional controlled-by-parent master MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ADR-0055 §1 as amended lets a controlled_by_parent object's only master_detail be declared required: false. Studio now names that master in the OWD overview (via the spec's controlledByParentMasterOf), keeps an explicit required: false on save, and lets the field inspector clear Required on the only master_detail, with help text on the null-master owner rule. Claude-Session: https://claude.ai/code/session_01FEkQ5mJLhZasj7G83LSvsv Co-Authored-By: Claude --- .../30974-studio-optional-cbp-master.md | 16 ++++ ...Inspector.cbpRequiredMaster-12326.test.tsx | 53 +++++++++--- .../inspectors/ObjectFieldInspector.tsx | 30 +++++-- .../studio-design/ObjectSettingsPanel.tsx | 8 +- ...viewPanel.cbpRequiredMaster-12326.test.tsx | 61 +++++++++----- .../studio-design/PackageOwdOverviewPanel.tsx | 25 +++--- .../views/studio-design/owd-sharing.test.ts | 63 ++++++++++----- .../src/views/studio-design/owd-sharing.ts | 81 ++++++++++++------- ui/packages/i18n/src/namespaces/studio/en.ts | 2 + ui/packages/i18n/src/namespaces/studio/zh.ts | 2 + 10 files changed, 239 insertions(+), 102 deletions(-) create mode 100644 .changeset/30974-studio-optional-cbp-master.md diff --git a/.changeset/30974-studio-optional-cbp-master.md b/.changeset/30974-studio-optional-cbp-master.md new file mode 100644 index 00000000000..ee0902e74cc --- /dev/null +++ b/.changeset/30974-studio-optional-cbp-master.md @@ -0,0 +1,16 @@ +--- +'@object-ui/app-shell': patch +'@object-ui/i18n': patch +--- + +fix(studio): Studio shows, keeps and authors the optional master of a controlled-by-parent object (#30974) + +Clause-②: no +Metadata-protocol: none + +Since ADR-0055 §1 as amended (#30905, ruling A), a `controlled_by_parent` object's ONLY `master_detail` may be declared `required: false`: an optional master, whose records may name no master and are then visible only to their owner and to View All Records on the object. Studio still treated every master as required. It now follows the spec: + +- **Display.** The package OWD overview names the master the spec's `controlledByParentMasterOf` names, so an optional master is shown as the master instead of "no master". An omitted `required` on the object's only `master_detail` is shown as the master too, the way the builder and stored data read it. +- **Save.** Switching an object to Controlled by parent (in the OWD overview or the object's Settings tab) still sets `required: true` on each `master_detail` that writes no `required`, and now keeps an explicit `required: false`. A Studio save no longer turns an optional master required. +- **Authoring.** On the object's only `master_detail`, the field inspector's Required box is no longer locked on: clearing it writes `required: false`, and the help text under it explains the optional master and who can see a record that names none. Beside another `master_detail` the lock stands, since the spec admits no optional master there. +- **New string.** `@object-ui/i18n`'s studio namespace gains `designer.field.requiredCbpOptionalMaster` (en, zh). diff --git a/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.cbpRequiredMaster-12326.test.tsx b/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.cbpRequiredMaster-12326.test.tsx index df1498f05ad..91c5ad4252a 100644 --- a/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.cbpRequiredMaster-12326.test.tsx +++ b/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.cbpRequiredMaster-12326.test.tsx @@ -8,11 +8,12 @@ * Since `@objectstack/spec` 18 (objectstack#23100, ADR-0055 §1) such an object * derives its master from exactly one `master_detail` declared * `required: true`. Since ADR-0055 §1 as amended (objectstack#30905 ruling A) - * the spec also reads an object's only `master_detail` that is not required as - * an OPTIONAL master, whose rows may name none; the designer keeps writing - * `required: true`, so the master it shows is one every row names. The - * installed spec's `ObjectSchema` is the oracle for the object each edit - * leaves behind. + * an object's ONLY `master_detail` may be declared `required: false`, an + * OPTIONAL master whose rows may name none. On that field the box stays free, + * clearing it writes `required: false`, and the help text explains the + * null-master owner rule (objectstack#30974). Beside another `master_detail` + * the lock stands. The installed spec's `ObjectSchema` is the oracle for the + * object each edit leaves behind. */ import '@testing-library/jest-dom/vitest'; @@ -89,23 +90,51 @@ function mount(object: Row, selected: string) { const requiredBox = () => screen.getByRole('checkbox', { name: t('designer.field.required', 'en-US') }); const REASON = t('designer.field.requiredCbpMaster', 'en-US'); +const OPTIONAL_HELP = t('designer.field.requiredCbpOptionalMaster', 'en-US'); describe('ObjectFieldInspector — a controlled_by_parent master is required, locked (objectui#12326)', () => { - it('the Required box of the master is on and locked, with the reason', () => { - mount( + it('beside another master-detail, the Required box of the master is on and locked, with the reason', () => { + const { object } = mount( { name: 'crm_case', sharingModel: 'controlled_by_parent', - fields: { account: { type: 'master_detail', reference: 'crm_account', required: true } }, + fields: { + account: { type: 'master_detail', reference: 'crm_account', required: true }, + contact: { type: 'master_detail', reference: 'crm_contact', required: false }, + }, }, 'account', ); + expect(ObjectSchema.safeParse(object()).success).toBe(true); expect(requiredBox()).toBeChecked(); expect(requiredBox()).toBeDisabled(); expect(screen.getByTestId('field-required-cbp-master')).toHaveTextContent(REASON); }); - it('a stored master without required can be ticked, and then no longer cleared', () => { + it('the only master-detail can be cleared to an optional master, written required: false (objectstack#30974)', () => { + const { object, field } = mount( + { + name: 'crm_case', + sharingModel: 'controlled_by_parent', + fields: { account: { type: 'master_detail', reference: 'crm_account', required: true } }, + }, + 'account', + ); + expect(requiredBox()).toBeChecked(); + expect(requiredBox()).toBeEnabled(); + expect(screen.getByTestId('field-required-cbp-master')).toHaveTextContent(OPTIONAL_HELP); + fireEvent.click(requiredBox()); + expect(field().required).toBe(false); + expect(requiredBox()).not.toBeChecked(); + const parsed = ObjectSchema.safeParse(object()); + expect(parsed.success).toBe(true); + expect((parsed.data?.fields as Record).account.required).toBe(false); + // And back: ticking it makes the master required again. + fireEvent.click(requiredBox()); + expect(field().required).toBe(true); + }); + + it('a stored master without required can be ticked, and cleared again to an optional master', () => { const { object, field } = mount( { name: 'crm_case', @@ -123,10 +152,12 @@ describe('ObjectFieldInspector — a controlled_by_parent master is required, lo expect(requiredBox()).toBeEnabled(); fireEvent.click(requiredBox()); expect(field().required).toBe(true); - expect(requiredBox()).toBeDisabled(); + expect(requiredBox()).toBeEnabled(); const after = ObjectSchema.safeParse(object()); expect(after.success).toBe(true); expect((after.data?.fields as Record).account.required).toBe(true); + fireEvent.click(requiredBox()); + expect(field().required).toBe(false); }); it('outside controlled_by_parent the box stays free and no reason is shown', () => { @@ -175,6 +206,6 @@ describe('ObjectFieldInspector — a controlled_by_parent master is required, lo await userEvent.click(within(list).getByRole('option', { name: 'Master-Detail' })); expect(field()).toEqual({ type: 'master_detail', label: 'Account', reference: 'crm_account', required: true }); expect(ObjectSchema.safeParse(object()).success).toBe(true); - expect(requiredBox()).toBeDisabled(); + expect(requiredBox()).toBeChecked(); }); }); diff --git a/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.tsx b/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.tsx index aa672857f2f..b0b8b474c4e 100644 --- a/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.tsx +++ b/ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.tsx @@ -1087,12 +1087,23 @@ export function ObjectFieldInspector({ /** * objectui#12326 — on a `controlled_by_parent` object a `master_detail` field - * is `required: true` and its Required box is locked on: the object derives - * its master from exactly one required `master_detail` (ADR-0055 §1), and the - * save door refuses any other value. `ObjectSettingsPanel` sets it when the - * sharing model is chosen; this inspector keeps it from being cleared. + * is `required: true` and its Required box is locked on once ticked: the + * object derives its master from exactly one required `master_detail` + * (ADR-0055 §1). `ObjectSettingsPanel` sets it when the sharing model is + * chosen; this inspector keeps it from being cleared. + * + * The one exception is the object's ONLY `master_detail` (ADR-0055 §1 as + * amended, objectstack#30905 ruling A): it may be an optional master, + * declared `required: false`, whose rows may name no master and are then + * readable only by their owner and by View All on the object. There the box + * stays free, clearing it writes `required: false` (an omitted `required` + * would be defaulted back to required), and the help text says what an + * optional master means. Beside another `master_detail` the spec admits no + * optional master, so the lock stands. */ const cbpMasterDetail = draft.sharingModel === 'controlled_by_parent' && type === 'master_detail'; + const cbpOptionalMasterAllowed = + cbpMasterDetail && view.entries.filter((e) => e.def.type === 'master_detail').length === 1; /** * A retype keeps a picklist binding only on a type the spec lets name one. @@ -1470,10 +1481,13 @@ export function ObjectFieldInspector({ patchDef({ required: v || undefined })} + // An optional master is written as `required: false`, never by + // dropping the key (objectstack#30905 ruling A). + onCommit={(v) => patchDef({ required: cbpOptionalMasterAllowed ? v : v || undefined })} // Locked once it is on: the box can still be ticked on a stored - // master that lacks it, never cleared (objectui#12326). - disabled={locked || (cbpMasterDetail && def.required === true)} + // master that lacks it, never cleared (objectui#12326), except + // on the object's only `master_detail`, which may be optional. + disabled={locked || (cbpMasterDetail && !cbpOptionalMasterAllowed && def.required === true)} /> {cbpMasterDetail && (

- {tr('designer.field.requiredCbpMaster')} + {tr(cbpOptionalMasterAllowed ? 'designer.field.requiredCbpOptionalMaster' : 'designer.field.requiredCbpMaster')}

)} { if (!v) { onPatch({ sharingModel: undefined }); diff --git a/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.cbpRequiredMaster-12326.test.tsx b/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.cbpRequiredMaster-12326.test.tsx index 4fff4c8fb9b..ca2d3034a48 100644 --- a/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.cbpRequiredMaster-12326.test.tsx +++ b/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.cbpRequiredMaster-12326.test.tsx @@ -6,18 +6,13 @@ * * Since `@objectstack/spec` 18 (objectstack#23100, ADR-0055 §1) the master is * the ONE `master_detail` declared `required: true`; zero or two or more derive - * none. The row shows that master, or says there is none. Switching a row to - * `controlled_by_parent` sets `required: true` on its `master_detail` fields in - * the saved draft, as the per-object Settings tab does. The installed spec's + * none. ADR-0055 §1 as amended (objectstack#30905 ruling A) adds an OPTIONAL + * master: an object's only `master_detail` declared `required: false`. The row + * shows the master, required or optional, or says there is none. Switching a + * row to `controlled_by_parent` sets `required: true` on each `master_detail` + * that writes no `required`, as the per-object Settings tab does, and keeps an + * optional master as written (objectstack#30974). The installed spec's * `ObjectSchema` is the oracle for the saved body. - * - * ADR-0055 §1 as amended (objectstack#30905 ruling A) adds an OPTIONAL master: - * an object's only `master_detail` declared `required: false`. The parse cannot - * tell an omitted `required` from `false`, so it admits the omitted shape too. - * This overview still names none for an omitted `required`, as the runtime - * does for an object that reaches it unparsed (it reads `required: false` - * literally, never its absence), and that row records the parse's answer - * apart from the master shown. */ import '@testing-library/jest-dom/vitest'; @@ -62,15 +57,14 @@ afterEach(cleanup); const cbp = (fields: Body): Body => ({ name: 'crm_case', label: 'Case', sharingModel: 'controlled_by_parent', fields }); describe('PackageOwdOverviewPanel — the master shown is the one the runtime derives (objectui#12326)', () => { - // [label, fields, the master shown, whether the spec parse accepts it when - // that differs from "a master is shown"] - const SHAPES: Array<[string, Body, string | undefined, boolean?]> = [ + // [label, fields, the master shown] + const SHAPES: Array<[string, Body, string | undefined]> = [ ['one required master', { account: { type: 'master_detail', reference: 'crm_account', required: true } }, 'crm_account'], + ['one master without required', { account: { type: 'master_detail', reference: 'crm_account' } }, 'crm_account'], [ - 'one master without required (the parse reads it as an optional master; no master is shown)', - { account: { type: 'master_detail', reference: 'crm_account' } }, - undefined, - true, + 'one optional master (required: false)', + { account: { type: 'master_detail', reference: 'crm_account', required: false } }, + 'crm_account', ], [ 'two required masters', @@ -82,10 +76,9 @@ describe('PackageOwdOverviewPanel — the master shown is the one the runtime de ], ]; - it.each(SHAPES)('%s', async (_label, fields, master, parses) => { - // The oracle: the spec accepts the shapes a master is shown for, and the - // one row that says otherwise. - expect(ObjectSchema.safeParse(cbp(fields)).success).toBe(parses ?? master !== undefined); + it.each(SHAPES)('%s', async (_label, fields, master) => { + // The oracle: the spec accepts exactly the shapes a master is shown for. + expect(ObjectSchema.safeParse(cbp(fields)).success).toBe(master !== undefined); renderPanel({ crm_case: cbp(fields) }); const cell = await screen.findByTestId('owd-master-crm_case'); expect(cell).toHaveTextContent( @@ -127,6 +120,30 @@ describe('PackageOwdOverviewPanel — switching a row to controlled_by_parent (o expect((parsed.data?.fields as Record).account.required).toBe(true); }); + it('keeps an optional master as written: the save never turns required: false required (objectstack#30974)', async () => { + const body: Body = { + name: 'crm_case', + label: 'Case', + sharingModel: 'private', + fields: { account: { type: 'master_detail', reference: 'crm_account', required: false } }, + }; + const { saved } = renderPanel({ crm_case: body }); + await screen.findByTestId('owd-row-crm_case'); + fireEvent.keyDown(screen.getByTestId('owd-internal-crm_case'), { key: 'ArrowDown' }); + fireEvent.click(within(await screen.findByRole('listbox')).getByRole('option', { name: CBP_LABEL })); + expect(screen.getByTestId('owd-master-crm_case')).toHaveTextContent( + tFormat('engine.studio.owd.byParent', 'en-US', { master: 'crm_account' }), + ); + + fireEvent.click(screen.getByTestId('owd-save')); + await waitFor(() => expect(saved.length).toBe(1)); + expect(saved[0].body.sharingModel).toBe('controlled_by_parent'); + expect((saved[0].body.fields as Record).account.required).toBe(false); + const parsed = ObjectSchema.safeParse(saved[0].body); + expect(parsed.success).toBe(true); + expect((parsed.data?.fields as Record).account.required).toBe(false); + }); + it('a row with two master-detail fields shows no master before the save', async () => { renderPanel({ crm_case: { diff --git a/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.tsx b/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.tsx index 6ef3c055e88..bd277f86ed7 100644 --- a/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.tsx +++ b/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.tsx @@ -54,7 +54,7 @@ import { import { errorCodeIs } from '@object-ui/types'; import { readFields } from '../metadata-admin/previews/object-fields-io.js'; import { t, tFormat, type SupportedLocale } from '../metadata-admin/i18n.js'; -import { deriveMasterObject, requireCbpMasterDetails } from './owd-sharing.js'; +import { cbpMasterDetailDefaultsRequired, deriveMasterObject, requireCbpMasterDetails } from './owd-sharing.js'; import { toast } from 'sonner'; @@ -69,13 +69,14 @@ interface OwdRow { internal: string; /** * The master (parent) object the row derives under `controlled_by_parent`: - * the reference of its one required `master_detail` (objectui#12326). For a - * row loaded as `controlled_by_parent` it is read from the body as stored, - * which is what the runtime derives. For any other row it is the master the - * row would derive once switched and saved here, since that save sets - * `required: true` on its `master_detail` fields. Undefined = no master: zero - * `master_detail` fields, or two or more, an authoring error the save door - * refuses. + * the reference of its one master `master_detail`, required or optional + * (objectui#12326, objectstack#30905 ruling A). For a row loaded as + * `controlled_by_parent` it is read from the body as stored, which is what + * the runtime derives. For any other row it is the master the row would + * derive once switched and saved here, since that save sets `required: true` + * on each `master_detail` that writes no `required`. Undefined = no master: + * zero `master_detail` fields, or two or more with no one master, an + * authoring error the save door refuses. */ master?: string; /** True when this object already has an unpublished draft. */ @@ -138,7 +139,7 @@ function owdLabel(value: string, locale: SupportedLocale): string { */ function cbpPrerequisite(fields: unknown): string[] { return readFields(fields) - .entries.filter((e) => e.def.type === 'master_detail' && e.def.required !== true) + .entries.filter((e) => cbpMasterDetailDefaultsRequired(e.def)) .map((e) => `${e.name} → ${typeof e.def.reference === 'string' ? e.def.reference : '?'}`); } @@ -399,8 +400,10 @@ export function PackageOwdOverviewPanel({ if (s.edit.internal) next.sharingModel = s.edit.internal; else delete next.sharingModel; // objectui#12326 — switching to `controlled_by_parent` sets - // `required: true` on every `master_detail` field, as the per-object - // Settings tab does: the save door refuses an omitted one there. + // `required: true` on each `master_detail` field that writes no + // `required`, as the per-object Settings tab does: the save door would + // read the omission as an optional master. An explicit + // `required: false` is an optional master and is kept. let relations: string[] = []; if (s.edit.internal === 'controlled_by_parent' && next.fields !== undefined) { relations = cbpPrerequisite(next.fields); diff --git a/ui/packages/app-shell/src/views/studio-design/owd-sharing.test.ts b/ui/packages/app-shell/src/views/studio-design/owd-sharing.test.ts index 97d09097411..24f0d8374fb 100644 --- a/ui/packages/app-shell/src/views/studio-design/owd-sharing.test.ts +++ b/ui/packages/app-shell/src/views/studio-design/owd-sharing.test.ts @@ -42,32 +42,46 @@ describe('owd-sharing — deriveMasterObject', () => { /** * objectui#12326 — the display reader agrees with the runtime (ADR-0055 §1, - * objectstack#23100): the ONE `master_detail` declared `required: true` names - * the master, and any other shape names none. The installed spec's - * `ObjectSchema` is the oracle for which shapes the platform accepts: every - * shape this reader names a master for parses as a `controlled_by_parent` - * object, and every shape it names none for is refused, with one exception. + * objectstack#23100): the master is the one the spec's + * `controlledByParentMasterOf` names, and any other shape names none. The + * installed spec's `ObjectSchema` is the oracle for which shapes the platform + * accepts: every shape this reader names a master for parses as a + * `controlled_by_parent` object, and every shape it names none for is refused. * * ADR-0055 §1 as amended (objectstack#30905 ruling A) adds an OPTIONAL master: - * an object's only `master_detail` declared `required: false`. The parse cannot - * tell an omitted `required` from `false`, so it admits the omitted shape too, - * while this reader names none for it, as the runtime does for an object that - * reaches it unparsed (it reads `required: false` literally, never its - * absence). That row records the parse's answer apart from the master named. + * an object's only `master_detail` declared `required: false`, which this + * reader names (objectstack#30974). An omitted `required` on that only + * `master_detail` is the master too: the parse admits it, and Studio, the + * builder and stored data read it as required. */ describe('owd-sharing — deriveMasterObject agrees with the runtime (objectui#12326)', () => { const asCbp = (fields: Record) => ObjectSchema.safeParse({ name: 'crm_case', sharingModel: 'controlled_by_parent', fields }).success; - // [label, fields, the master named, whether the spec parse accepts it when - // that differs from "a master is named"] - const SHAPES: Array<[string, Record, string | undefined, boolean?]> = [ + // [label, fields, the master named] + const SHAPES: Array<[string, Record, string | undefined]> = [ ['one required master', { account: { type: 'master_detail', reference: 'crm_account', required: true } }, 'crm_account'], + ['one master without required', { account: { type: 'master_detail', reference: 'crm_account' } }, 'crm_account'], [ - 'one master without required (the parse reads it as an optional master; this reader names none)', - { account: { type: 'master_detail', reference: 'crm_account' } }, + 'one optional master (required: false)', + { account: { type: 'master_detail', reference: 'crm_account', required: false } }, + 'crm_account', + ], + [ + 'a master-detail declared required: false beside the required one', + { + contact: { type: 'master_detail', reference: 'crm_contact', required: false }, + account: { type: 'master_detail', reference: 'crm_account', required: true }, + }, + 'crm_account', + ], + [ + 'two master-detail fields, none required', + { + account: { type: 'master_detail', reference: 'crm_account', required: false }, + contact: { type: 'master_detail', reference: 'crm_contact', required: false }, + }, undefined, - true, ], [ 'two required masters', @@ -89,9 +103,9 @@ describe('owd-sharing — deriveMasterObject agrees with the runtime (objectui#1 ['no relationship field', { subject: { type: 'text' } }, undefined], ]; - it.each(SHAPES)('%s', (_label, fields, master, parses) => { + it.each(SHAPES)('%s', (_label, fields, master) => { expect(deriveMasterObject(fields)).toBe(master); - expect(asCbp(fields)).toBe(parses ?? master !== undefined); + expect(asCbp(fields)).toBe(master !== undefined); }); }); @@ -116,6 +130,19 @@ describe('owd-sharing — requireCbpMasterDetails (objectui#12326)', () => { expect(next).toEqual([{ name: 'parent', type: 'master_detail', reference: 'crm_case', required: true }]); }); + it('keeps an optional master: an explicit required: false is never turned required (objectstack#30974)', () => { + const fields = { account: { type: 'master_detail', reference: 'crm_account', required: false } }; + expect(requireCbpMasterDetails(fields)).toBe(fields); + const arr = [{ name: 'parent', type: 'master_detail', reference: 'crm_case', required: false }]; + expect(requireCbpMasterDetails(arr)).toBe(arr); + const mixed = requireCbpMasterDetails({ + account: { type: 'master_detail', reference: 'crm_account', required: false }, + contact: { type: 'master_detail', reference: 'crm_contact' }, + }) as Record>; + expect(mixed.account.required).toBe(false); + expect(mixed.contact.required).toBe(true); + }); + it('returns the input itself when every master-detail field is already required', () => { const fields = { account: { type: 'master_detail', reference: 'crm_account', required: true } }; expect(requireCbpMasterDetails(fields)).toBe(fields); diff --git a/ui/packages/app-shell/src/views/studio-design/owd-sharing.ts b/ui/packages/app-shell/src/views/studio-design/owd-sharing.ts index 92afdda1f9a..75b43ddacdf 100644 --- a/ui/packages/app-shell/src/views/studio-design/owd-sharing.ts +++ b/ui/packages/app-shell/src/views/studio-design/owd-sharing.ts @@ -11,7 +11,7 @@ * DOM-free, so both surfaces can share one validated implementation. */ -import { ObjectSchema } from '@objectstack/spec/data'; +import { ObjectSchema, controlledByParentMasterOf } from '@objectstack/spec/data'; import { readFields, writeFields } from '../metadata-admin/previews/object-fields-io.js'; /** @@ -32,39 +32,64 @@ const MASTER_DETAIL = 'master_detail'; /** * The master (parent) object a `controlled_by_parent` child inherits its - * baseline from, read the way the runtime derives it (ADR-0055 §1, enforced - * since `@objectstack/spec` 18, objectstack#23100): the `reference` of the ONE - * `master_detail` field declared `required: true`. + * baseline from, read the way the runtime derives it from a stored object + * (ADR-0055 §1 as amended by objectstack#30905 ruling A). Which field is the + * master is the spec's answer, `controlledByParentMasterOf`: the ONE + * `master_detail` declared `required: true`, or, with none, the object's only + * `master_detail` declared `required: false` (an optional master, whose rows + * may name none). * - * Returns undefined when the object declares zero such fields, or two or more: - * the runtime derives no master from either shape (every read is denied, every - * write refused), and it never picks one by field order. It used to return the - * FIRST `master_detail` by order, required or not, which showed a master for - * an object the runtime derives none from (objectui#12326). A non-required - * `master_detail` is not a master. Undefined is the authoring error, and the - * overview says so rather than naming a parent. + * An omitted `required` on the object's only `master_detail` reads as + * `required: true`, as the spec's stored-row conversion + * `cbp-master-detail-required-defaulted` reads it: Studio defaults it to + * required, as the builder and stored data do. It names the same master either + * way. + * + * Returns undefined when the spec names no master (two required masters, two + * `master_detail` fields none of them required, none at all): the runtime + * derives none from those shapes and never picks one by field order. Undefined + * is the authoring error, and the overview says so rather than naming a parent. */ export function deriveMasterObject(fields: unknown): string | undefined { - const masters = readFields(fields).entries.filter( - (e) => e.def.type === MASTER_DETAIL && e.def.required === true, - ); - if (masters.length !== 1) return undefined; - const ref = masters[0].def.reference; + const entries = readFields(fields).entries; + const masterDetails = entries.filter((e) => e.def.type === MASTER_DETAIL); + const byName: Record> = {}; + for (const e of entries) { + byName[e.name] = + masterDetails.length === 1 && e === masterDetails[0] && e.def.required === undefined + ? { ...e.def, required: true } + : e.def; + } + const master = controlledByParentMasterOf(byName); + if (!master) return undefined; + const ref = byName[master.field].reference; return typeof ref === 'string' && ref ? ref : undefined; } +/** + * Does Studio's write default this field's `required` to `true` on a + * `controlled_by_parent` object? A `master_detail` that writes no `required` + * at all. An explicit `required: false` is the author's choice, an optional + * master (ADR-0055 §1 as amended, objectstack#30905 ruling A), and is kept. + */ +export function cbpMasterDetailDefaultsRequired(def: Record): boolean { + return def.type === MASTER_DETAIL && def.required === undefined; +} + /** * The designer's write side of the same rule (objectui#12326): on a - * `controlled_by_parent` object every `master_detail` field is `required: true`, - * because the protocol admits no other value there. Returns the fields with - * that set on each `master_detail` that does not carry it, in the shape they - * came in, or the input itself when nothing changes, so a caller can skip a - * write that would change nothing. + * `controlled_by_parent` object a `master_detail` field that writes no + * `required` is made `required: true`, because the save door would otherwise + * read the omission as an optional master. Returns the fields with that set, + * in the shape they came in, or the input itself when nothing changes, so a + * caller can skip a write that would change nothing. * - * It never picks a master. With two or more `master_detail` fields every one - * of them is made required, and the spec refuses the object for declaring more - * than one ({@link cbpMasterRefusal} shows that refusal). The author then - * chooses which one stays the master, which is the spec's own prescription. + * It keeps an explicit `required: false`: that is an optional master the + * author chose, and a save never turns it required. It never picks a master. + * With two or more `master_detail` fields each omitted one is made required, + * and the spec refuses the object when that leaves more than one master + * ({@link cbpMasterRefusal} shows that refusal). The author then chooses + * which one stays the master, which is the spec's own prescription. * * `@objectstack/spec`'s `ObjectSchema.create()` applies the same force to an * omitted `required` and does not export it, so this is the designer's copy of @@ -72,15 +97,13 @@ export function deriveMasterObject(fields: unknown): string | undefined { */ export function requireCbpMasterDetails(fields: unknown): unknown { const view = readFields(fields); - if (!view.entries.some((e) => e.def.type === MASTER_DETAIL && e.def.required !== true)) { + if (!view.entries.some((e) => cbpMasterDetailDefaultsRequired(e.def))) { return fields; } return writeFields({ shape: view.shape, entries: view.entries.map((e) => - e.def.type === MASTER_DETAIL && e.def.required !== true - ? { name: e.name, def: { ...e.def, required: true } } - : e, + cbpMasterDetailDefaultsRequired(e.def) ? { name: e.name, def: { ...e.def, required: true } } : e, ), }); } diff --git a/ui/packages/i18n/src/namespaces/studio/en.ts b/ui/packages/i18n/src/namespaces/studio/en.ts index b026048bdbe..24ff525f28d 100644 --- a/ui/packages/i18n/src/namespaces/studio/en.ts +++ b/ui/packages/i18n/src/namespaces/studio/en.ts @@ -368,6 +368,8 @@ const STUDIO_EN: Record = { 'designer.field.required': 'Required', 'designer.field.requiredCbpMaster': 'Required is locked on: this object is controlled by parent, and it derives its master from this master-detail field, which must be required.', + 'designer.field.requiredCbpOptionalMaster': + 'This object is controlled by parent, and this master-detail field is its master. Clear Required to make it an optional master: a record that names no master is visible only to its owner and to users with View All Records on this object.', 'designer.field.unique': 'Unique', 'designer.field.description': 'Description', 'designer.field.readonly': 'Read-only', diff --git a/ui/packages/i18n/src/namespaces/studio/zh.ts b/ui/packages/i18n/src/namespaces/studio/zh.ts index 210c932216b..cfff4df26ac 100644 --- a/ui/packages/i18n/src/namespaces/studio/zh.ts +++ b/ui/packages/i18n/src/namespaces/studio/zh.ts @@ -296,6 +296,8 @@ const STUDIO_ZH: Record = { 'designer.field.required': '必填', 'designer.field.requiredCbpMaster': '必填已锁定:该对象受父级控制,其主记录由这个主从字段推导,该字段必须必填。', + 'designer.field.requiredCbpOptionalMaster': + '该对象受父级控制,这个主从字段是它的主字段。取消必填即成为可选主字段:未指定主记录的记录仅其所有者及对该对象拥有“查看所有记录”权限的用户可见。', 'designer.field.unique': '唯一', 'designer.field.description': '描述', 'designer.field.readonly': '只读', From f7f98072faba226b37b93efee7d08f8c561c2731 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 16:53:33 +0000 Subject: [PATCH 2/2] =?UTF-8?q?chore(changeset):=20declare=20the=20measure?= =?UTF-8?q?d=20Clause-=E2=91=A1=20arm=20for=20the=20new=20i18n=20key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The diff adds one key to @object-ui/i18n's published studio catalogue, which widens it. Claude-Session: https://claude.ai/code/session_01FEkQ5mJLhZasj7G83LSvsv Co-Authored-By: Claude --- .changeset/30974-studio-optional-cbp-master.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/30974-studio-optional-cbp-master.md b/.changeset/30974-studio-optional-cbp-master.md index ee0902e74cc..f0333d29ad5 100644 --- a/.changeset/30974-studio-optional-cbp-master.md +++ b/.changeset/30974-studio-optional-cbp-master.md @@ -5,7 +5,7 @@ fix(studio): Studio shows, keeps and authors the optional master of a controlled-by-parent object (#30974) -Clause-②: no +Clause-②: yes (widening) Metadata-protocol: none Since ADR-0055 §1 as amended (#30905, ruling A), a `controlled_by_parent` object's ONLY `master_detail` may be declared `required: false`: an optional master, whose records may name no master and are then visible only to their owner and to View All Records on the object. Studio still treated every master as required. It now follows the spec: