diff --git a/.changeset/36595-placed-title-save-door.md b/.changeset/36595-placed-title-save-door.md new file mode 100644 index 00000000000..eeae4180056 --- /dev/null +++ b/.changeset/36595-placed-title-save-door.md @@ -0,0 +1,17 @@ +--- +'@objectstack/metadata-protocol': patch +'@objectstack/spec': patch +--- + +fix(metadata-protocol): a tab a code package places, or a field it adds, is saved in its source text from a translated read + +Clause-②: no +Metadata-protocol: none + +The `/meta` read of an object serves a related-list tab that another installed code package places into the object's `relatedLists` (its `place`), and a field such a package adds, translated for the caller's language. When the object has a stored customization row saved before that package was installed, the row carries no copy of either. A save of that translated read in a language other than the source (the whole body sent back through `PUT /meta/object/NAME`) then had nothing to compare them with, so the translation was stored as the tab title or the field label, and every other language read it from then on. + +The save door now compares them with the packaged definition as a last resort: a tab whose `list` the owning package does not name, or a field the owning package does not declare, that the stored row does not declare at all, is compared with what the packaged definition carries, and its translation is stored as that source text. The save answer names each such slot in `advisories` (`translated-label-echo`), as it does for every other restored label. A title or label the stored row does declare is never compared with the packaged definition, so a customer's own rename keeps its text, even when it equals some language's catalog entry. A tab or field the owning package declares and the stored row dropped is left as the body carries it. + +Console and Studio editors already save the stored document rather than the translated read, so they never reached this; a client that saved the translated read back did. + +`TranslateDocumentOptions.packagedBase` now says that an object's packaged definition carries the tabs a code package places, and that a placement is judged on the owning package there but on the customization row by the served object: where only a customization row declares `relatedLists`, the placed tab has no packaged counterpart and the deployment catalog does not translate its title. diff --git a/packages/metadata-protocol/src/protocol-discovery-and-bases.ts b/packages/metadata-protocol/src/protocol-discovery-and-bases.ts index 2c4238cec23..567127ec024 100644 --- a/packages/metadata-protocol/src/protocol-discovery-and-bases.ts +++ b/packages/metadata-protocol/src/protocol-discovery-and-bases.ts @@ -1131,6 +1131,14 @@ export class ProtocolDiscoveryAndBasesFacet { * could have answered in. Each restored slot is answered on the save's * `advisories` ({@link translatedEchoNotices}). * + * The packaged owner (the registry's `getPackagedObjectOwner`) is handed + * in beside the base, so what the base carries beyond it (a field a code + * extender adds, a `relatedLists` entry a code extender's `place` + * splices in) is the last source for a field or entry the stored row does + * not declare: the fold serves those over a stored row saved before the + * extending package was installed, translated against the base, and that + * row carries nothing to compare them with. + * * Every save is asked, the stored-row migration included * (`source: 'migrate-stored'`): it re-saves a row through this door, which * is how a row a zh-CN session already stored is repaired. Any other @@ -1139,7 +1147,7 @@ export class ProtocolDiscoveryAndBasesFacet { * the packaged base is a registry lookup, so a degraded MetadataService * fails nothing here. */ - static async restoreTranslatedObjectEchoes(host: Pick, request: SaveMetaItemRequest, + static async restoreTranslatedObjectEchoes(host: Pick, request: SaveMetaItemRequest, repo: SysMetadataRepository, ref: Parameters[0], mode: 'draft' | 'publish',): Promise<{ item: SaveMetaItemRequest['item']; restored: RestoredTranslatedSlot[] }> { @@ -1174,11 +1182,20 @@ export class ProtocolDiscoveryAndBasesFacet { const own = await host.getPackageTranslationBundle(entry.packageId); if (own !== undefined) patchBundles.push({ patchedBase: entry.base, bundle: own }); } + // The owner alone, so the restore can tell what a code extender + // brings from what the owner declares (see the docblock): the same + // registry read `getPackagedObjectBase` falls back to. No `try`, for + // the reason the stored-row read has none. + const registry = (host.engine as any)?.registry; + const packagedOwner: unknown = packagedBase && typeof registry?.getPackagedObjectOwner === 'function' + ? registry.getPackagedObjectOwner(request.name)?.definition + : undefined; return restoreTranslatedEchoes(request.item, { source, bundle: host.registeredTranslationBundle(), packagedBase: packagedBase ?? undefined, ...(patchBundles.length > 0 ? { patchBundles } : {}), + ...(packagedOwner !== undefined && packagedOwner !== null ? { packagedOwner } : {}), }); } diff --git a/packages/metadata-protocol/src/protocol-translated-echo.test.ts b/packages/metadata-protocol/src/protocol-translated-echo.test.ts index 2fb6e83e2d4..d3e5e58a13b 100644 --- a/packages/metadata-protocol/src/protocol-translated-echo.test.ts +++ b/packages/metadata-protocol/src/protocol-translated-echo.test.ts @@ -15,6 +15,10 @@ 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'; +import { ProtocolDiscoveryAndBasesFacet } from './protocol-discovery-and-bases.js'; +// The save door's module reaches `@objectstack/lint` through dynamic imports; +// its first load is paid here, during collection, never inside a case's clock. +import '@objectstack/lint'; const bundle: TranslationBundle = { 'zh-CN': { @@ -238,3 +242,103 @@ describe('[#34836] a patch entry answers a slot its patch changed, and only wher expect(restored).toEqual([]); }); }); + +/** + * What a CODE extender brings, over a stored row saved before the extending + * package was installed: the fold serves the extender's added field and the + * entry its `place` splices into `relatedLists`, translated against the + * packaged base, and the stored row carries no copy of either. The packaged + * base is the last source for them, told from the owner's own declaration. + */ +describe('a code extender\'s placed title or added field, over a stored row that predates it', () => { + const catalog = { + 'zh-CN': { + objects: { + crm_lead: { + fields: { name: { label: '名称' }, cb_market: { label: '市场' } }, + _relatedLists: { crm_task: { label: '任务' }, acme_audit: { label: '审计' } }, + }, + }, + }, + } as unknown as TranslationBundle; + const owner = () => ({ + name: 'crm_lead', + label: 'Lead', + fields: { name: { type: 'text', label: 'Name' } } as Record, + relatedLists: [{ list: 'crm_task', label: 'Tasks' }, 'crm_note'] as unknown[], + }); + /** The packaged base: the owner, the extender's added field and its placed tab. */ + const packagedBase = () => ({ + ...owner(), + fields: { ...owner().fields, cb_market: { type: 'text', label: 'Market' } }, + relatedLists: [{ list: 'crm_task', label: 'Tasks' }, { list: 'acme_audit', label: 'Audit trail' }, 'crm_note'], + }); + /** The stored overlay row, saved before the extender: it declares `relatedLists` without the placed entry. */ + const storedRow = () => ({ ...owner(), label: 'Prospect' }); + /** The `/meta` read in zh-CN: the fold over the stored row, translated against the packaged base. */ + const zhRead = (): any => { + const served = { ...storedRow(), fields: packagedBase().fields, relatedLists: packagedBase().relatedLists }; + return structuredClone(translateObject(served as any, catalog, { locale: 'zh-CN', packagedBase: packagedBase() } as any)); + }; + const labelOf = (body: any, list: string) => body.relatedLists.find((e: any) => typeof e === 'object' && e.list === list)?.label; + + it('the read does translate both: the case is real, not a no-op', () => { + const echo = zhRead(); + expect(labelOf(echo, 'acme_audit')).toBe('审计'); + expect(echo.fields.cb_market.label).toBe('市场'); + }); + + it('a zh-CN save of the folded read keeps the placed title and the added field\'s label as their source', () => { + const { item, restored } = restoreTranslatedEchoes(zhRead(), { + source: storedRow(), bundle: catalog, packagedBase: packagedBase(), packagedOwner: owner(), + }) as any; + expect(labelOf(item, 'acme_audit')).toBe('Audit trail'); + expect(item.fields.cb_market.label).toBe('Market'); + expect(restored.map((r: any) => r.path).sort()).toEqual([ + 'fields.cb_market.label', 'fields.name.label', 'relatedLists[acme_audit].label', 'relatedLists[crm_task].label', + ]); + // The stored row's own text is what it was: the object label diverged, so the read served it as written. + expect(item.label).toBe('Prospect'); + }); + + it('a placed entry the stored row declares is the stored row\'s to explain: a tenant\'s rename, typed over as the catalog string, is kept', () => { + const source: any = storedRow(); + source.relatedLists = [...source.relatedLists, { list: 'acme_audit', label: 'Audit log' }]; + const body = zhRead(); + expect(labelOf(body, 'acme_audit')).toBe('审计'); + const { item } = restoreTranslatedEchoes(body, { source, bundle: catalog, packagedBase: packagedBase(), packagedOwner: owner() }) as any; + expect(labelOf(item, 'acme_audit')).toBe('审计'); + }); + + it('an entry or field the OWNER declares and the stored row dropped is no extender\'s: the body\'s text is kept', () => { + const source: any = { ...storedRow(), fields: {}, relatedLists: ['crm_note'] }; + const body = zhRead(); + const { item } = restoreTranslatedEchoes(body, { source, bundle: catalog, packagedBase: packagedBase(), packagedOwner: owner() }) as any; + expect(labelOf(item, 'crm_task')).toBe('任务'); + expect(item.fields.name.label).toBe('名称'); + // …while the extender's own still come back as their source. + expect(labelOf(item, 'acme_audit')).toBe('Audit trail'); + expect(item.fields.cb_market.label).toBe('Market'); + }); + + it('the save door hands the restore the packaged owner: a non-source-locale save over the stored row lacking the entry keeps the source title', async () => { + const host = { + engine: { registry: { getPackagedObjectOwner: (name: string) => (name === 'crm_lead' ? { definition: owner() } : undefined) } }, + getPackagedObjectBase: (name: string) => (name === 'crm_lead' ? packagedBase() : undefined), + getPackagedFieldPatchBases: () => [], + getPackageTranslationBundle: async () => undefined, + registeredTranslationBundle: () => catalog, + }; + const repo = { headAt: async () => ({ head: { body: storedRow() }, packageId: null }) }; + const { item, restored } = await ProtocolDiscoveryAndBasesFacet.restoreTranslatedObjectEchoes( + host as any, + { type: 'object', name: 'crm_lead', item: zhRead() } as any, + repo as any, + { type: 'object', name: 'crm_lead', org: 'env' } as any, + 'publish', + ) as any; + expect(labelOf(item, 'acme_audit')).toBe('Audit trail'); + expect(item.fields.cb_market.label).toBe('Market'); + expect(restored.map((r: any) => r.path)).toEqual(expect.arrayContaining(['relatedLists[acme_audit].label', 'fields.cb_market.label'])); + }); +}); diff --git a/packages/metadata-protocol/src/protocol-translated-echo.ts b/packages/metadata-protocol/src/protocol-translated-echo.ts index 98f51ab0c07..120e8d8e95f 100644 --- a/packages/metadata-protocol/src/protocol-translated-echo.ts +++ b/packages/metadata-protocol/src/protocol-translated-echo.ts @@ -47,7 +47,14 @@ * carries the echo itself, so compared with itself nothing differs, and * compared with the platform's `queue` its label is `队列` for `Queue`. That * is what lets the next save of such a row — and the stored-row migration, - * which re-saves through this door — repair it. + * which re-saves through this door — repair it; + * - last, for what a CODE extender brings and the stored row does not + * declare at all, the packaged base: a field the packaged owner does not + * declare, and a `relatedLists` entry whose `list` the owner does not name + * (an extender's `place`). The read serves both from the fold over a stored + * row of any age, and translates them against the packaged base; a row + * saved before the extending package was installed carries no copy, so + * without this source the translation would be stored as the text. * * The locales tried are every one the read could have answered in: the * bundle's, and the built-in system-field table's @@ -60,8 +67,13 @@ * the source in some locale gets the source back — which the reader in that * locale is served anyway, since the catalog translates it. Nothing else is * touched: a label that is not a translation of its source is the author's - * edit and is kept, and a field the source does not declare has nothing to - * be compared with. + * edit and is kept, and a field no source declares (neither the stored row nor, + * for what a code extender brings, the packaged base) has nothing to be + * compared with. The packaged base is never a source for a field or entry the + * stored row declares, nor for one the packaged owner declares: a stored slot + * is the stored row's to explain (a diverged one is served as written), and an + * owner's field or entry the stored row dropped is not served by the fold, so + * a body carrying it again carries the author's text. * * The slots are the ones the read translates: the object's scalars, each * field's text slots and option labels, and the `label` of each object-form @@ -72,7 +84,7 @@ * from the packaged text) is no translation's source. * * Pure: no store, no service. The caller hands in the source row, the - * bundle and the packaged base. + * bundle, the packaged base and the packaged owner. */ import type { RuntimeAuthoringIssue } from '@objectstack/spec/api'; @@ -175,6 +187,17 @@ function relatedListEntry(entries: unknown, list: string): Rec | undefined { return Array.isArray(entries) ? (entries.find((e) => isRec(e) && e.list === list) as Rec | undefined) : undefined; } +/** Every `list` the entries name, as a bare string or an object entry's `list`. */ +function relatedListNames(entries: unknown): Set { + const out = new Set(); + if (!Array.isArray(entries)) return out; + for (const e of entries) { + if (typeof e === 'string') out.add(e); + else if (isRec(e) && typeof e.list === 'string') out.add(e.list); + } + return out; +} + /** That entry's `label`; {@link NOT_DECLARED} when no entry names `list` with a string label. */ function relatedListLabel(entries: unknown, list: string): unknown { const label = relatedListEntry(entries, list)?.label; @@ -194,8 +217,8 @@ export function translatedEchoLocales(bundle: TranslationBundle | undefined): st * only the names the incoming body declares. */ interface TranslatedSource { - /** Which source: the stored row, one patch entry's base, or the platform's columns. */ - kind: 'stored' | 'patch' | 'injected'; + /** Which source: the stored row, one patch entry's base, the platform's columns, or what a code extender brings. */ + kind: 'stored' | 'patch' | 'injected' | 'extender'; doc: Rec; fields: Map; reads: Array<{ doc: Rec; fields: Map }>; @@ -236,12 +259,14 @@ function translatedSource( * Put every translated slot of `item` back to its source text. Returns the * SAME reference when nothing was restored. See the module header. * - * Two sources, asked in order, the first that explains the value wins: the - * stored row's own declaration, then — for an injected column — the - * platform's definition. The second is what heals a row a zh-CN session - * already stored: its `queue` declaration IS the translation, so compared - * with itself nothing differs, while compared with the platform's `queue` the - * label is `队列` for `Queue`. + * The sources, asked in order, the first that explains the value wins: the + * stored row's own declaration, each patch entry's base, then — for an + * injected column — the platform's definition, and last — given + * `packagedOwner` — what a code extender brings that the stored row does not + * declare. The injected one is what heals a row a zh-CN session already + * stored: its `queue` declaration IS the translation, so compared with itself + * nothing differs, while compared with the platform's `queue` the label is + * `队列` for `Queue`. */ export function restoreTranslatedEchoes( item: T, @@ -279,6 +304,19 @@ export function restoreTranslatedEchoes( * package's translation. */ patchBundles?: PatchBundles; + /** + * The packaged OWNER's own declaration (the registry's + * `getPackagedObjectOwner`), the base before any code extender widened + * it. With it and `packagedBase`, what the base carries beyond the + * owner is the last source, for an incoming field or `relatedLists` + * entry the stored row does not declare at all: a field a code + * extender adds (a name the owner does not declare) and an entry a + * code extender's `place` splices in (a `list` the owner does not + * name). Those the read serves from the fold over a stored row that + * predates the extending package, translated against the base, and + * nothing else explains them. Absent, there is no such source. + */ + packagedOwner?: unknown; }, ): { item: T; restored: RestoredTranslatedSlot[] } { const restored: RestoredTranslatedSlot[] = []; @@ -322,7 +360,27 @@ export function restoreTranslatedEchoes( } return doc; }); - const sources = ([['stored', storedDoc], ...patchDocs.map((doc) => ['patch', doc] as const), ['injected', injectedDoc]] as const) + // What a code extender brings, cut to what the stored row does not + // declare: the packaged base's fields the owner does not declare, and its + // `relatedLists` entries whose `list` the owner does not name. A slot the + // stored row declares is the stored row's to explain, so this source can + // never out-explain a tenant's own text; with no stored row the source IS + // the packaged base, and this cut is empty. + const extenderDoc: Rec = { name: item.name, fields: {} }; + if (isRec(args.packagedOwner) && isRec(args.packagedBase) && args.packagedBase !== args.packagedOwner) { + const ownerFields = fieldMap(args.packagedOwner); + for (const name of incoming.keys()) { + const def = packagedFields.get(name); + if (def && !ownerFields.has(name) && !storedFields.has(name)) (extenderDoc.fields as Rec)[name] = { ...def }; + } + const ownerLists = relatedListNames(args.packagedOwner.relatedLists); + const storedLists = relatedListNames(isRec(args.source) ? args.source.relatedLists : undefined); + const placed = Array.isArray(args.packagedBase.relatedLists) + ? args.packagedBase.relatedLists.filter((e) => isRec(e) && typeof e.list === 'string' && !ownerLists.has(e.list) && !storedLists.has(e.list)) + : []; + if (placed.length > 0) extenderDoc.relatedLists = placed; + } + const sources = ([['stored', storedDoc], ...patchDocs.map((doc) => ['patch', doc] as const), ['injected', injectedDoc], ['extender', extenderDoc]] as const) .filter(([, doc]) => Object.keys(doc.fields as Rec).length > 0 || Object.keys(doc).length > 2) .map(([kind, doc]) => translatedSource(kind, doc, locales, args.bundle, args.packagedBase, patchBundles)); if (sources.length === 0) return { item, restored }; diff --git a/packages/spec/src/system/i18n-resolver.ts b/packages/spec/src/system/i18n-resolver.ts index eba7ced87d6..b17df591156 100644 --- a/packages/spec/src/system/i18n-resolver.ts +++ b/packages/spec/src/system/i18n-resolver.ts @@ -403,9 +403,17 @@ export interface TranslateDocumentOptions extends ResolveOptions { * layer shipped that rule name, and its `fields` also carry each field a * code extender ADDS under a name the owner does not declare, as that * extender ships it — the catalog rows for such a field can only translate - * what its extender ships. Its scalars and every field the owner declares + * what its extender ships, and, where the owner declares `relatedLists`, + * those `relatedLists` carry each entry a code extender's list placement + * (`place`) splices in, as the fold splices it, so a placed tab's `label` + * reads the catalog. Its scalars and every field the owner declares * stay the owner's: an extension's `fieldPatches` are never folded in, so a - * patched owner field still reads as diverged. + * patched owner field still reads as diverged. Whether a placement applies + * is judged on the OWNER here and on the served object's base layer (a + * tenant overlay's own `relatedLists`, else the owner's) by the fold: where + * only a tenant overlay declares `relatedLists`, the served placed tab has + * no packaged counterpart, so it reads as authored and the deployment + * catalog does not translate its `label`. * * Supplied by the serving layer, which is the only one that knows it; when * it is absent the translators behave exactly as they did before #8284 (the