Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .changeset/33911-extension-sibling-currency-field.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@objectstack/objectql': minor
---

fix(objectql): an `object_extension` whose record-mode amount names a sibling `currencyField` the served object does not hold is refused at the save, not at every write

Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: none — `skills/objectstack-data/rules/field-types.md` already says a record-mode amount's code lives in the `text` field `currencyField` names, declaring `valueDomain: 'iso_4217_currency'`, with no carve-out for an extension; the refusal now makes that true for an extension's amount, and its text carries the prescription

<!-- adr-0087: registered record-currency-sibling-extension-judged-as-served -->

**BREAKING** accept-set narrowing, shipped as `minor`. An object body whose record-mode amount names a one-segment `currencyField` it does not declare, or a field holding no ISO 4217 code, is refused `422 INVALID_METADATA` at parse. An extension's amount cannot be judged there, because its pointer may name a field only the owner declares, and the record-currency follow door judged an extension only on the two-segment `MASTER_DETAIL.CODE` path. So `PUT /api/v1/meta/object_extension/showcase_invoice` adding `fee: { type: 'currency', currencyConfig: { currencyMode: 'record', currencyField: 'zz_missing' } }` answered 200 and was served, and every later write of `fee` was refused `ERR_CURRENCY_CODE_REQUIRED`, prescribing a write into `zz_missing`, which does not exist.

- **The verdict.** The follow door (`SchemaRegistry.objectRegistrationRefusal` with `againstRegistry`, and the end of an install, upgrade or reload on a running server) now judges a one-segment pointer against the object AS SERVED: its base, every enabled extension, and the incoming body. A pointer naming no other field, or a field that is not a `text` field declaring `valueDomain: 'iso_4217_currency'`, is refused `422 INVALID_METADATA` (`RecordCurrencyFollowRefusedError`), naming the amount, the pointer and the object's code-field candidates; nothing is stored.
- **Admitted as before.** A pointer naming a code field the owner declares, the same extension adds, or another enabled extension adds.
- **The owner's end.** An owner re-registering without the code field an extension's amount names is refused, naming the package that adds the amount.
- **Runs and single saves.** An install, upgrade or reload that leaves such a pointer as it found it is not refused over it. A published single save and the batch writers judge with the slot left empty: re-publishing an unchanged extension whose pointer a cold boot admitted is refused, and so is a published save of the owner while an extension's amount names no code field. Correct that extension's pointer (or uninstall it), then save the owner again unchanged.
- **Not asked.** The cold boot: packages installed before this release keep loading.

One-line fix: point the amount's `currencyField` at the extended object's ISO 4217 code field (the refusal lists the candidates), or add one in the same extension and name it.
Original file line number Diff line number Diff line change
Expand Up @@ -997,3 +997,66 @@ describe('[#33593] an extension\'s record-mode amount is judged against its obje
expect(fieldOf(ql, 'suite_line', 'inv2')?.reference).toBe('suite_invoice');
});
});

// ── [ADR-0144 D2] An extension's ONE-SEGMENT pointer, judged as its object is served, at the end of the run ──
//
// Pair (b) of the follow door: a record-mode amount an extension adds whose
// `currencyField` is a sibling name is judged against the served object. A run
// that adds a dangling one is refused; a run that leaves one as it found it
// (one a cold boot admitted unasked) is not (R7); a run that breaks one is
// refused naming the extension's package (R6).
describe('an extension\'s sibling record-currency pointer is judged against its object as served, at the end of the run', () => {
/** The child, holding an ISO 4217 code of its own under `currency_code` (none when `withCode` is false). */
const coded = (withCode = true) => object('suite_line', {
fields: {
title: { type: 'text', label: 'Title' },
invoice: { type: 'master_detail', label: 'Invoice', reference: 'suite_invoice' },
...(withCode ? { currency_code: CODE } : {}),
},
});

it('an extension-only install naming no field of the served object is refused 422, naming the candidates, and leaves nothing', async () => {
const { ql, manifest } = await boot();
await manifest.register(ledger('currency_code'));
await manifest.register(billing([coded()]));

const err = await refusal(() => manifest.register(extender({ ext_amount: follows('zz_missing') }), { source: 'marketplace' }));

expect(err).toMatchObject({ ...FOLLOW_REFUSED, objectName: 'suite_line', fieldName: 'ext_amount' });
expect(String(err?.message)).toContain("'zz_missing'");
expect(String(err?.message)).toContain("candidates: 'currency_code'");
expect(extraOf(ql)).toBeUndefined();
expect(ql.registry.getPackage(EXTENDER)).toBeUndefined();
});

it('CONTROL: the extension naming the code field the owner declares installs', async () => {
const { ql, manifest } = await boot();
await manifest.register(ledger('currency_code'));
await manifest.register(billing([coded()]));

await manifest.register(extender({ ext_amount: follows('currency_code') }), { source: 'marketplace' });

expect(extraOf(ql)?.currencyConfig?.currencyField).toBe('currency_code');
});

it('R7: a no-change upgrade of the owner, and of the extension, installs over a dangling pointer a cold boot admitted', async () => {
const { ql, manifest } = await bootWith([ledger('currency_code'), billing([coded()]), extender({ ext_amount: follows('zz_missing') })]);
expect(extraOf(ql)?.currencyConfig?.currencyField).toBe('zz_missing');

await manifest.register(billing([coded()], '1.0.1'));
await manifest.register(extender({ ext_amount: follows('zz_missing') }, '1.0.1'));

expect(ql.registry.getPackage(BILLING)?.manifest?.version).toBe('1.0.1');
expect(ql.registry.getPackage(EXTENDER)?.manifest?.version).toBe('1.0.1');
});

it('R6: an owner upgrade that removes the code field an extension\'s amount names is refused, naming the extension\'s package', async () => {
const { ql, manifest } = await bootWith([ledger('currency_code'), billing([coded()]), extender({ ext_amount: follows('currency_code') })]);

const err = await refusal(() => manifest.register(billing([coded(false)], '1.0.1')));

expect(err).toMatchObject({ ...FOLLOW_REFUSED, objectName: 'suite_line', fieldName: 'ext_amount' });
expect(String(err?.message)).toContain(`'${EXTENDER}'`);
expect(ql.registry.getPackage(BILLING)?.manifest?.version).toBe('1.0.0');
});
});
110 changes: 110 additions & 0 deletions packages/objectql/src/protocol-batch-object-registration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,3 +715,113 @@ describe('[#33713] a metadata-API save of a `dynamic` roll-up over a child field
expect(activeRow(h, 'object_extension', 'homeroll_project', P)).toBeUndefined();
});
});

// ── [ADR-0144 D2, amended] An extension's ONE-SEGMENT record-currency pointer, judged as its object is served ──
//
// `ObjectSchema` refuses a record-mode amount whose sibling `currencyField`
// names no code field of the object; `ObjectExtensionSchema` cannot (an
// extension's pointer may name a field only the owner declares). The follow
// door judges the pointer against the object AS SERVED — the owner, every
// enabled extension, and this body in its slot — so a pointer into nothing is
// refused at the save, never at every later write of the amount.
describe('an object_extension\'s record-mode amount naming a sibling code field is judged against its object as served', () => {
const P = 'com.qa.sib';
const X = 'com.qa.siblingext';
const fee = (pointer: string) => ({ name: 'fee', type: 'currency', label: 'Fee', currencyConfig: { currencyMode: 'record', currencyField: pointer } });
const caught = async (run: () => Promise<unknown>): Promise<any> => {
try {
await run();
} catch (e) {
return e;
}
return undefined;
};
async function bootWithInvoice(owner: Record<string, unknown> = invoice('sib')): Promise<Harness> {
const h = await boot();
vi.spyOn(console, 'warn').mockImplementation(() => {});
await h.protocol.installPackage({ manifest: { id: P, name: 'Sibling', type: 'app' } });
await h.protocol.saveMetaItem({ type: 'object', name: String(owner.name), packageId: P, item: owner, writeFace: 'external-import' });
return h;
}
const saveExtension = (h: Harness, fields: Record<string, unknown>) => h.protocol.saveMetaItem({
type: 'object_extension', name: 'sib_invoice', packageId: P, mode: 'publish', writeFace: 'meta-envelope',
item: { extend: 'sib_invoice', fields },
});

it('refuses 422 INVALID_METADATA a pointer naming no field of the served object, naming the field and the code-field candidates, and stores nothing', async () => {
const h = await bootWithInvoice();
const err = await caught(() => saveExtension(h, { fee: fee('zz_missing') }));
expect(err).toMatchObject({ status: 422, code: 'INVALID_METADATA', name: 'RecordCurrencyFollowRefusedError', objectName: 'sib_invoice', fieldName: 'fee' });
expect(err.message).toContain("'zz_missing'");
expect(err.message).toContain("candidates: 'currency_code'");
expect(registered(h, 'sib_invoice')?.fields?.fee).toBeUndefined();
expect(activeRow(h, 'object_extension', 'sib_invoice', P)).toBeUndefined();
});

it('refuses a pointer naming a served field that holds no ISO 4217 code', async () => {
const h = await bootWithInvoice();
const err = await caught(() => saveExtension(h, { fee: fee('title') }));
expect(err).toMatchObject({ status: 422, code: 'INVALID_METADATA', objectName: 'sib_invoice', fieldName: 'fee' });
expect(err.message).toContain("a 'text' field that does not hold an ISO 4217 code");
expect(err.message).toContain("candidates: 'currency_code'");
expect(activeRow(h, 'object_extension', 'sib_invoice', P)).toBeUndefined();
});

it('says the object has no code field when the served object holds none', async () => {
const { currency_code: _code, ...withoutCode } = invoice('sib').fields;
const h = await bootWithInvoice({ ...invoice('sib'), fields: withoutCode });
const err = await caught(() => saveExtension(h, { fee: fee('currency_code') }));
expect(err).toMatchObject({ status: 422, code: 'INVALID_METADATA', fieldName: 'fee' });
expect(err.message).toContain("it has none; declare one");
});

it('CONTROL: a pointer naming the code field the owner declares is saved and served', async () => {
const h = await bootWithInvoice();
await saveExtension(h, { fee: fee('currency_code') });
expect(registered(h, 'sib_invoice')?.fields?.fee?.currencyConfig?.currencyField).toBe('currency_code');
expect(activeRow(h, 'object_extension', 'sib_invoice', P)).toBeDefined();
});

it('CONTROL: a pointer naming a code field the same body, or another registered extension, declares is saved', async () => {
const { currency_code: _code, ...withoutCode } = invoice('sib').fields;
const h = await bootWithInvoice({ ...invoice('sib'), fields: withoutCode });
await saveExtension(h, { own_code: { name: 'own_code', type: 'text', label: 'Code', ...CODE }, fee: fee('own_code') });
expect(registered(h, 'sib_invoice')?.fields?.fee?.currencyConfig?.currencyField).toBe('own_code');

const other = await bootWithInvoice({ ...invoice('sib'), fields: withoutCode });
other.engine.registry.registerObject({ name: 'sib_invoice', fields: { ext_code: { name: 'ext_code', type: 'text', label: 'Code', ...CODE } } } as never, X, undefined, 'extend');
await saveExtension(other, { fee: fee('ext_code') });
expect(registered(other, 'sib_invoice')?.fields?.fee?.currencyConfig?.currencyField).toBe('ext_code');
});

it('the owner re-saved without the code field a registered extension\'s amount names is refused, naming the package that adds the amount', async () => {
const h = await bootWithInvoice();
h.engine.registry.registerObject({ name: 'sib_invoice', fields: { fee: fee('currency_code') } } as never, X, undefined, 'extend');
const { currency_code: _code, ...withoutCode } = invoice('sib').fields;
const refusal = h.engine.registry.objectRegistrationRefusal({ ...invoice('sib'), fields: withoutCode } as never, P, 'own', { againstRegistry: true });
expect(refusal).toMatchObject({ name: 'RecordCurrencyFollowRefusedError', status: 422, code: 'INVALID_METADATA', objectName: 'sib_invoice', fieldName: 'fee' });
expect(refusal?.message).toContain(`package '${X}'`);
});

// A single save asks with its slot left empty (`before: null`), so it does
// not get the run's R7 comparison: a pointer already dangling when the save
// arrives (registered unasked, as a cold boot registers it) refuses both an
// unchanged re-publish of that extension and an unchanged owner save.
it('a published single save is refused over a sibling pointer that already dangles: the extension\'s unchanged re-publish, and the owner\'s unchanged save', async () => {
const h = await bootWithInvoice();
h.engine.registry.registerObject({ name: 'sib_invoice', fields: { fee: fee('zz_missing') } } as never, P, undefined, 'extend');
expect(registered(h, 'sib_invoice')?.fields?.fee?.currencyConfig?.currencyField).toBe('zz_missing');

const republish = await caught(() => saveExtension(h, { fee: fee('zz_missing') }));
expect(republish).toMatchObject({ status: 422, code: 'INVALID_METADATA', name: 'RecordCurrencyFollowRefusedError', objectName: 'sib_invoice', fieldName: 'fee' });
expect(activeRow(h, 'object_extension', 'sib_invoice', P)).toBeUndefined();

const other = await bootWithInvoice();
other.engine.registry.registerObject({ name: 'sib_invoice', fields: { fee: fee('zz_missing') } } as never, X, undefined, 'extend');
// Asked as the single save asks it (a protocol re-save of the owner first meets the destructive-change
// door, which reads the extension's column as one the body would drop).
const ownerSave = other.engine.registry.objectRegistrationRefusal(invoice('sib') as never, P, 'own', { againstRegistry: true });
expect(ownerSave).toMatchObject({ status: 422, code: 'INVALID_METADATA', name: 'RecordCurrencyFollowRefusedError', objectName: 'sib_invoice', fieldName: 'fee' });
expect(String(ownerSave?.message)).toContain(`package '${X}'`);
});
});
Loading
Loading