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
24 changes: 24 additions & 0 deletions .changeset/29673-translated-echo-save-door.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@objectstack/metadata-protocol': patch
'@objectstack/objectql': patch
'@objectstack/spec': patch
---

fix(metadata-protocol)!: an object saved back from a translated `/meta` read stores its source labels, and an object the registry would refuse is refused at save and at publish, never reported published (#29673)

Clause-②: no (narrowing) — the object save door (`saveMetaItem`, draft and live) refuses `422 INVALID_METADATA` an object declaring its own `queue` field on a user-owned object (ADR-0142 D3), which it stored before and the registry then refused to register; the publish promote refuses such a draft the same way. A label equal to what the `/meta` read serves for its source in some locale is stored as the source text. Nothing is widened.
Metadata-protocol: none
Skills-docs: none — `skills/objectstack-data/SKILL.md` (Ownership queues) already teaches that an object declaring its own `queue` field is refused, which stays true and is now said at save; no file under `skills/**` teaches saving a translated read back.

<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped. The refusal at save is the registry's own, moved forward to the door that stores the body; a row stored before this rule already never registered. A stored row carrying a translated injected column is repaired by `os migrate meta --stored`, which re-saves it through the same door: no ledger entry and no tombstone. -->

**BREAKING** accept-set narrowing at the object save door (`@objectstack/metadata-protocol`), served by `PUT /api/v1/meta/object/:name` (draft and live) and `POST /api/v1/packages/:id/publish-drafts`.

- **Before.** A `/meta` read in a non-English locale serves each injected system column with a translated label (`queue` → `队列`, `owner_id` → `负责人`). Studio saves the whole body it read, so the save stored those columns back as the author's own fields. For `queue` the registry then refused the object at publish, the refusal went to a log line, and `publish-drafts` answered `outcome: 'published'` while the object's data door answered 404. For `owner_id` and the other columns, every other locale was served the Chinese label.
- **Now.**
- The save door puts a slot back to its source text when the incoming value differs from the source and equals what the read's translator makes of that source in some locale, translating with the read's own options (a packaged object's overlay slot that diverged from the packaged text is served untranslated, so it is no translation's source). The source is the stored row the save overwrites, then the platform's own definition of an injected column. The strip that follows then removes the platform's columns as it always did. Slots: an object's `label`, `pluralLabel` and `description`; a field's `label`, `description`, `inlineHelpText` and `inlineTitle`; an option's `label`.
- The save door asks the registry whether it would register the object, judging the body alone, and refuses what it would not, in both modes. The publish promote asks again, before anything is promoted, so a draft stored before this rule is refused and not reported published. The only refusal this makes new at the door is an author's own `queue`; a field `type` the registry refuses was already refused at save by the object schema. The record-currency `summary` door is left to the registration, since it judges the body against the other objects as they stand and a sibling in the same batch can change that answer either way.
- `os migrate meta --stored` reports a stored object row carrying a translated label as `translated-label-echo` and, with `--apply`, re-saves it through the save door.
- **Unchanged.** A label that is not a translation of its source is the author's edit and is kept. A field the source does not declare is not compared. An English body is stored as before.
- **The edge.** An author who types exactly the translation of a label's source in some locale gets the source back; a reader in that locale is served the translation anyway.
- **New export.** `BUILTIN_SYSTEM_FIELD_LABEL_LOCALES` (`@objectstack/spec/system`): the locales the built-in system-field label table answers in, with no bundle. `SchemaRegistry.objectRegistrationRefusal` (`@objectstack/objectql`) answers the refusal `registerObject` would throw judging the body alone (every door but the record-currency `summary` one), without registering anything.
134 changes: 134 additions & 0 deletions packages/metadata-protocol/src/protocol-translated-echo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* [#29673] `restoreTranslatedEchoes` — the save door's inverse of the `/meta`
* read's translation, as a pure function. The echo every case hands it is
* built with the spec translator the read exits call, so a case can only pass
* when the inverse matches what a read really serves.
*
* The door end to end (the real registry, the real protocol, the publish and
* the stored-row repair) is pinned in `@objectstack/objectql`'s
* `protocol-meta-effective-schema.test.ts`.
*/

import { describe, it, expect } from 'vitest';
import { translateObject, type TranslationBundle } from '@objectstack/spec/system';
import { QUEUE_FIELD_DEF, OWNER_FIELD_DEF } from '@objectstack/spec/data';
import { restoreTranslatedEchoes, translatedEchoLocales } from './protocol-translated-echo.js';

const bundle: TranslationBundle = {
'zh-CN': {
objects: {
crm_invoice: {
label: '发票',
fields: {
amount: { label: '金额' },
status: { label: '状态', options: { open: '打开', closed: '关闭' } },
},
},
},
},
} as unknown as TranslationBundle;

/** The source a stored row carries. */
const stored = () => ({
name: 'crm_invoice',
label: 'Invoice',
sharingModel: 'private',
fields: {
amount: { type: 'currency', label: 'Amount' },
status: { type: 'select', label: 'Status', options: [{ value: 'open', label: 'Open' }, { value: 'closed', label: 'Closed' }] },
},
});

/** What a read serves in `locale`: the stored row plus the injected columns, translated. */
const read = (locale: string, b: TranslationBundle | undefined = bundle): any => {
const doc: any = stored();
doc.fields = { ...doc.fields, queue: { ...QUEUE_FIELD_DEF }, owner_id: { ...OWNER_FIELD_DEF } };
return translateObject(doc, b, { locale });
};

describe('[#29673] restoreTranslatedEchoes', () => {
it('a zh-CN echo is put back to the source on every slot the read translated', () => {
const echo = read('zh-CN');
// The precondition: the read really translated each slot.
expect([echo.label, echo.fields.amount.label, echo.fields.status.options[0].label, echo.fields.queue.label, echo.fields.owner_id.label])
.toEqual(['发票', '金额', '打开', '队列', '负责人']);
const { item, restored } = restoreTranslatedEchoes(echo, { source: stored(), bundle });
expect(item.label).toBe('Invoice');
expect(item.fields.amount.label).toBe('Amount');
expect(item.fields.status.label).toBe('Status');
expect(item.fields.status.options.map((o: any) => o.label)).toEqual(['Open', 'Closed']);
// The injected columns are the platform's own again, byte for byte: the strip that follows removes them.
expect(item.fields.queue).toEqual(QUEUE_FIELD_DEF);
expect(item.fields.owner_id).toEqual(OWNER_FIELD_DEF);
expect(restored.map((r) => r.path).sort()).toEqual([
'fields.amount.label', 'fields.owner_id.label', 'fields.queue.label',
'fields.status.label', 'fields.status.options[closed].label', 'fields.status.options[open].label', 'label',
]);
});

it('a built-in locale with no bundle at all is recognised (the read translates injected columns without one)', () => {
expect(translatedEchoLocales(undefined)).toEqual(expect.arrayContaining(['zh-CN', 'ja-JP', 'es-ES']));
const echo = read('ja-JP', undefined);
expect(echo.fields.queue.label).toBe('キュー');
const { item } = restoreTranslatedEchoes(echo, { source: stored(), bundle: undefined });
expect(item.fields.queue).toEqual(QUEUE_FIELD_DEF);
});

it('the author\'s own text is kept: an edit that is not a translation, a new field, an own owner_id label', () => {
const echo = read('zh-CN');
echo.fields.amount.label = 'Total';
echo.fields.memo = { type: 'text', label: '备注' };
echo.fields.owner_id = { ...OWNER_FIELD_DEF, label: 'Account Owner' };
const source: any = stored();
source.fields.owner_id = { ...OWNER_FIELD_DEF, label: 'Account Owner' };
const { item } = restoreTranslatedEchoes(echo, { source, bundle });
expect(item.fields.amount.label).toBe('Total');
expect(item.fields.memo.label).toBe('备注');
expect(item.fields.owner_id.label).toBe('Account Owner');
});

it('a row that already stored the echo is repaired against the platform\'s definition', () => {
const echo = read('zh-CN');
// The stored row IS the echo (a zh-CN session saved it before this door existed).
const { item } = restoreTranslatedEchoes(echo, { source: echo, bundle });
expect(item.fields.queue).toEqual(QUEUE_FIELD_DEF);
expect(item.fields.owner_id).toEqual(OWNER_FIELD_DEF);
});

it('the array `fields` shape is restored in place, and an untranslated body comes back as the same reference', () => {
const echo = read('zh-CN');
const asArray = { ...echo, fields: Object.entries(echo.fields).map(([name, f]: [string, any]) => ({ name, ...f })) };
const { item } = restoreTranslatedEchoes(asArray, { source: stored(), bundle });
expect(item.fields.map((f: any) => f.name)).toEqual(asArray.fields.map((f: any) => f.name));
expect(item.fields.find((f: any) => f.name === 'amount').label).toBe('Amount');

const untouched = read('en');
const same = restoreTranslatedEchoes(untouched, { source: stored(), bundle });
expect(same.item).toBe(untouched);
expect(same.restored).toEqual([]);
});

it('a packaged object\'s overlay slot that diverged from the packaged text is no translation\'s source: the catalog string an author types is kept', () => {
// The packaged base ships `amount: Amount`; the catalog translates that
// packaged text. The tenant's overlay renamed it `Total`, which the read
// serves untranslated in every locale (ADR-0029 D9.2a).
const packagedBase = stored();
const overlay: any = stored();
overlay.fields.amount = { ...overlay.fields.amount, label: 'Total' };
// The precondition: the read, with the packaged base, serves `Total` in zh-CN.
expect((translateObject(overlay, bundle, { locale: 'zh-CN', packagedBase }) as any).fields.amount.label).toBe('Total');

const typed: any = stored();
typed.fields.amount = { ...typed.fields.amount, label: '金额' };
const kept = restoreTranslatedEchoes(typed, { source: overlay, bundle, packagedBase });
expect(kept.item.fields.amount.label).toBe('金额');
expect(kept.restored).toEqual([]);

// Control: the same typed string over a slot that still equals the
// packaged text IS the read's translation, and is put back.
const echoed = restoreTranslatedEchoes(typed, { source: packagedBase, bundle, packagedBase });
expect(echoed.item.fields.amount.label).toBe('Amount');
});
});
Loading
Loading