Skip to content

Commit 59faf74

Browse files
fix(metadata-protocol): copy an object_extension row without a name key (#38763)
<!-- claude-projects-attribution: {"github_login":"os-steve"} --> _Requested by **Steve** · [project thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9RgRdwG1S43a4gfYWB4Ytyp)_ Fixes #38709 Fixes #38762 Clause-②: no Metadata-protocol: none ## 维护者速读 - 搭建者复制一个含「对象扩展」的应用包后,扩展会随副本一起复制成功,并扩展副本自己的对象;此前每条扩展都被拒并报一个作者从没写过的键。 - 属于应用包复制与导入的修复,替复制含对象扩展的应用包的搭建者解决复制失败,同时关掉扩展误指源对象的问题。 - 若出错,复制出的扩展会失败或指向源包的对象,其他类型不受影响;本改动可直接撤回。 - 无 ## What changes `planNamespaceRename(...).rewrite` (the one rename engine `duplicatePackage` and `importUnmanagedPackage` with `targetNamespace` both call) set `name = copyName(type, name)` on every rewritten body. An `object_extension` body is keyed by `extend`; `ObjectExtensionSchema` is a strict object and `name` is only a retired alias, so every copied extension row landed in `failed[]` with "Unrecognized key(s) on this object extension: `name`". Two changes, both in `package-namespace-rename.ts`: - The rewrite skips `name` for `object_extension` only; every other type still takes its copy name. - A copied extension follows the copied object. `extend` is read off the source body and mapped whole through the copy's object renames (`objectRenames`), so an extension of the package's own bare-named object (`ticket`, copied as `acme_ticket`) extends the copy, not the source's object. A prefixed name (`crm_lead` to `acme_lead`) keeps its result and is not renamed twice, because the mapping reads the source value. An `extend` naming an object the copy does not hold (a platform object, another package's, even one spelled with the source prefix) keeps its name. `copyName` names an `object_extension` row by the same map, so the row name and its `extend` never part. Re-measured gap (on base `2f8383ffc6`, by tightening the control first and running it before the fix): the duplicate's `failed[]` held `object_extension/crm_lead failed spec validation: ... Unrecognized key(s) on this object extension: name`, `copied[]` held no extension row. ## Tests - `packages/objectql/src/protocol-custom-arrangement.test.ts`: the ADR-0146 D10 duplicate CONTROL now asserts the row without arrangement keys lands: no `object_extension` entry in `failed[]`, the row in `copied[]`, stored in the target under its target name, stored body exactly `{ extend }` (no `name`). The `it` title now says it lands in `copied[]` and is stored. - Same file, new top-level `describe` (duplicate level): a source package holding a bare object `ticket` and an extension of it, duplicated into `acme`: nothing in `failed[]`, the extension in `copied[]` as `acme_ticket`, the stored copy is `{ extend: 'acme_ticket' }`, the source row is unchanged. - `packages/metadata-protocol/src/package-namespace-rename-object-extension-38709.test.ts`, under both doors' `retryVerb` (`duplicate`, `install`): bare case `extend: 'acme_ticket'` and row name `acme_ticket`; prefixed case `acme_lead`, followed once; CONTROL: an `extend` naming an object the copy does not hold keeps its name, body and row alike (including `crm_platform`, spelled with the source prefix); full `ObjectExtensionSchema.safeParse` green. - Red-first, before the mapping (engine at the first round's fix): the bare pins red under both verbs (`extend` stayed `ticket`), the control red on the row name (`acme_platform` where `extend` stayed `crm_platform`), and the duplicate-level pin red (`no object named 'ticket' is registered`). - Ablation with `scripts/ablation-replace.mjs`: round 1, the `name` guard made unconditional, turned all three `name` pins red. Round 2, the `extend` mapping disabled, turned the bare pins red under both verbs and the duplicate-level pin red; the prefixed pin stayed green, as expected, because the free-text pass covers it. Each restore matched HEAD. ## Acceptance notes - Import leg: `importUnmanagedPackage` refuses a manifest's `objectExtensions` at its plan (`UNMANAGED_INSTALL_KEY_DISPOSITIONS.objectExtensions: 'uncopyable'`, 422 `UNMANAGED_INSTALL_UNCOPYABLE`, already pinned in `package-unmanaged-install.test.ts`), so no import can carry an `object_extension` to the rewrite and "copied and lands" is not reachable on that leg. The fix is in the shared engine; the import leg is pinned at the engine under `retryVerb: 'install'`. - Bare-named object: fixed here. A copied extension of the package's own bare object now extends the copy's object (`ticket` to `acme_ticket`), with its row named the same. Before, it stayed `ticket` and would have extended the source's object. - Row name of an extension of an object outside the package that carries the source prefix: `copyName` used to swap the prefix (`crm_platform` to `acme_platform`) while `extend` stayed. Both now keep the name. ## Verification Two-minute check: `pnpm --filter @objectstack/objectql exec vitest run --maxWorkers=2 src/protocol-custom-arrangement.test.ts` (after building metadata-protocol) shows 11 passed. Read first: `packages/metadata-protocol/src/package-namespace-rename.ts` (the `name` guard and the `extend` mapping in `rewrite`, then `copyName`), then the two objectql pins. --- 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01ADHkmQJUGAAa5rz9Yi2DvW <!-- signed: repo:objectstack#3 · os-steve · session_01ADHkmQJUGAAa5rz9Yi2DvW --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 95a49ad commit 59faf74

4 files changed

Lines changed: 150 additions & 8 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/metadata-protocol': patch
3+
---
4+
5+
fix(metadata-protocol): a duplicated or namespace-imported package copies its `object_extension` rows without a `name` key, and each copy extends the copied object
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
The namespace rename that `duplicatePackage` and `importUnmanagedPackage` (with `targetNamespace`) run on every copied item set `name` on each rewritten body. An `object_extension` body is keyed by `extend`, and the strict extension schema refuses `name`, so every copied extension landed in `failed[]` with "Unrecognized key(s) on this object extension: `name`", a key the author never wrote. The rewrite now leaves `name` off an `object_extension` body.
11+
12+
A copied extension now follows the copied object: when its `extend` names an object the copy renames, the copy extends the copy's object and its row is named by it, for a bare object name too (`ticket` copied as `acme_ticket`), so it never extends the source package's object. An `extend` naming an object the copy does not hold (a platform object, another package's object) keeps its name. Every other type still takes its copy name.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#38709] The rename engine's rewrite of an `object_extension` row writes only
5+
* keys the strict `ObjectExtensionSchema` admits — the body is keyed by
6+
* `extend`, and the engine gives it no `name` — and the copy follows the
7+
* copied object: an `extend` naming an object this copy renames takes that
8+
* object's copy name, prefixed or bare, and the row is named by it.
9+
* `planNamespaceRename` is the one engine both copy doors call —
10+
* `duplicatePackage` per stored row, and `importUnmanagedPackage` with
11+
* `targetNamespace` per planned item — so the pin runs the engine under each
12+
* door's `retryVerb`. The duplicate door's own landing pin is the CONTROL in
13+
* the objectql `protocol-custom-arrangement.test.ts` (an extension of an object
14+
* outside the package) and the bare-object duplicate reading beside it; the unmanaged
15+
* import refuses a manifest's `objectExtensions` at its plan (`uncopyable`),
16+
* pinned in `package-unmanaged-install.test.ts`.
17+
*/
18+
import { describe, expect, it } from 'vitest';
19+
import { ObjectExtensionSchema } from '@objectstack/spec/data';
20+
import { planNamespaceRename } from './package-namespace-rename.js';
21+
22+
const items = [
23+
{ type: 'object', name: 'crm_lead', body: { name: 'crm_lead', fields: {} } },
24+
{ type: 'object', name: 'ticket', body: { name: 'ticket', fields: {} } },
25+
{ type: 'object_extension', name: 'crm_lead', body: { extend: 'crm_lead' } },
26+
{ type: 'object_extension', name: 'ticket', body: { extend: 'ticket' } },
27+
{ type: 'object_extension', name: 'other_account', body: { extend: 'other_account' } },
28+
{ type: 'object_extension', name: 'crm_platform', body: { extend: 'crm_platform' } },
29+
];
30+
31+
const parses = (item: unknown) => {
32+
const parsed = ObjectExtensionSchema.safeParse(item);
33+
expect(parsed.success, JSON.stringify(parsed.error?.issues)).toBe(true);
34+
};
35+
36+
describe('planNamespaceRename — an `object_extension` copy carries no `name` and follows the copied object (#38709)', () => {
37+
for (const retryVerb of ['duplicate', 'install'] as const) {
38+
const rename = () => planNamespaceRename({
39+
sourcePackageId: 'com.example.crm', sourceNamespace: 'crm', targetNamespace: 'acme', items, retryVerb,
40+
});
41+
42+
it(`${retryVerb}: a bare object the copy renames — \`extend\` and the row name follow it to the copy's object`, () => {
43+
const r = rename();
44+
const outcome = r.rewrite('object_extension', 'ticket', { extend: 'ticket', priority: 300 });
45+
expect(outcome).toEqual({ item: { extend: 'acme_ticket', priority: 300 } });
46+
expect(r.copyName('object_extension', 'ticket')).toBe('acme_ticket');
47+
parses((outcome as { item: unknown }).item);
48+
});
49+
50+
it(`${retryVerb}: a prefixed object the copy renames — followed once, never renamed twice`, () => {
51+
const r = rename();
52+
expect(r.rewrite('object_extension', 'crm_lead', { extend: 'crm_lead' })).toEqual({ item: { extend: 'acme_lead' } });
53+
expect(r.copyName('object_extension', 'crm_lead')).toBe('acme_lead');
54+
});
55+
56+
it(`${retryVerb}: CONTROL — an \`extend\` naming an object the copy does not hold keeps its name, row and body alike`, () => {
57+
const r = rename();
58+
const outcome = r.rewrite('object_extension', 'other_account', { extend: 'other_account', priority: 300 });
59+
expect(outcome).toEqual({ item: { extend: 'other_account', priority: 300 } });
60+
expect(r.copyName('object_extension', 'other_account')).toBe('other_account');
61+
parses((outcome as { item: unknown }).item);
62+
// Spelled with the source prefix, yet not an object the package holds: still not renamed.
63+
expect(r.rewrite('object_extension', 'crm_platform', { extend: 'crm_platform' })).toEqual({ item: { extend: 'crm_platform' } });
64+
expect(r.copyName('object_extension', 'crm_platform')).toBe('crm_platform');
65+
// Every other type still takes its copy name.
66+
expect(r.rewrite('object', 'crm_lead', { name: 'crm_lead', fields: {} }))
67+
.toEqual({ item: { name: 'acme_lead', fields: {} } });
68+
});
69+
}
70+
});

‎packages/metadata-protocol/src/package-namespace-rename.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,11 @@ export function planNamespaceRename(input: NamespaceRenameInput): NamespaceRenam
749749
// `object` followed the copy, so the copy's container sat under a name
750750
// the source's container holds — the shadowing the qualified rule above
751751
// closes for `ticket.board`.
752+
// An `object_extension` row is named by its target (`listSlotNameOf`), so
753+
// its copy name is its target's copy name, read off the same map
754+
// `rewrite` maps `extend` through: the row and its `extend` never part.
752755
const copyName = (type: string, name: string): string =>
753-
type === 'object'
756+
type === 'object' || (PLURAL_TO_SINGULAR[String(type)] ?? String(type)) === 'object_extension'
754757
? (renameMap.get(name) ?? name)
755758
: isCapabilityType(type)
756759
? (capabilityRenames.get(name) ?? name)
@@ -830,7 +833,29 @@ export function planNamespaceRename(input: NamespaceRenameInput): NamespaceRenam
830833
};
831834
}
832835
if (rewritten && typeof rewritten === 'object' && !Array.isArray(rewritten)) {
833-
rewritten.name = copyName(type, name);
836+
// [#38709] An `object_extension` body is keyed by `extend` and
837+
// carries no `name`: `ObjectExtensionSchema` is strict and
838+
// refuses one (`listSlotNameOf` in
839+
// `protocol-object-extension-item.ts` reads the row's identity
840+
// off `extend`). Setting it here refused every copied
841+
// extension at the save door, for a key nobody wrote.
842+
if (singular !== 'object_extension') rewritten.name = copyName(type, name);
843+
// ⭐ A COPIED EXTENSION FOLLOWS THE COPIED OBJECT. `extend` names
844+
// the object the extension merges into; when that object is one
845+
// this copy renames, the copy's extension extends the copy's
846+
// object, never the source's. It is read off the SOURCE body and
847+
// mapped through `renameMap` whole, so a prefixed name the
848+
// free-text pass above already renamed is not renamed twice,
849+
// and a bare one (`ticket` → `acme_ticket`), which that pass
850+
// never reaches, follows too. An object the copy does not hold
851+
// (a platform object, another package's) is not in the map and
852+
// keeps its name. The row's name is this same target
853+
// (`copyName` above reads the same map).
854+
const extend = (item as { extend?: unknown } | null | undefined)?.extend;
855+
if (singular === 'object_extension' && typeof extend === 'string') {
856+
const target = renameMap.get(extend);
857+
if (target !== undefined) rewritten.extend = target;
858+
}
834859
if (singular === 'permission' && rewritten.fields
835860
&& typeof rewritten.fields === 'object' && !Array.isArray(rewritten.fields)) {
836861
const result = rewriteFieldRuleKeys(rewritten.fields);

‎packages/objectql/src/protocol-custom-arrangement.test.ts‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ describe('ADR-0146 D10 — a `.custom` package that is not the object\'s pair is
343343
expect(await outcome(() => saveExtension(protocol, ADDON_PAIR, { fields: { addon_score__c: text('addon_score__c', 'Score') } }))).toEqual({ ok: true });
344344
});
345345

346-
it('a package duplicate refuses the copied arrangement in a plain target and in another App\'s pair, per row; CONTROL: the copy of a row without the keys lands', async () => {
346+
it('a package duplicate refuses the copied arrangement in a plain target and in another App\'s pair, per row; CONTROL: the copy of a row without the keys lands in `copied[]` and is stored', async () => {
347347
const { protocol, stores } = await boot();
348348
// No `__c` field in the row: the copy into a plain package is refused for the arrangement alone.
349349
const OWNERS_ONLY = { fieldGroups: [{ key: 'basic', label: 'Basic', fields: ['name', 'company'] }], relatedLists: ARRANGEMENT.relatedLists };
@@ -370,14 +370,49 @@ describe('ADR-0146 D10 — a `.custom` package that is not the object\'s pair is
370370
}
371371

372372
// CONTROL: the same duplicate of a source whose row writes no arrangement
373-
// key is not refused for an arrangement. (Measured on this tree: the copy
374-
// still fails, on the copy's own `name` the duplicate sets on every item,
375-
// which the strict extension schema refuses; that is outside this rule.)
373+
// key lands. The copied body is keyed by `extend` alone: the rename gives
374+
// an `object_extension` no `name`, a key the strict extension schema
375+
// refuses (objectstack#38709).
376376
stores.get('sys_metadata')!.set('r_residue', { ...pairRow!, id: 'r_residue', package_id: SRC, metadata: JSON.stringify({ extend: OBJ }) });
377377
const res = await protocol.duplicatePackage({ sourcePackageId: SRC, targetPackageId: `${SVC}.custom`, targetNamespace: 'svc_custom' });
378378
const errors = (res.failed ?? []).filter((f: any) => f.type === 'object_extension').map((f: any) => String(f.error));
379379
const copied = (res.copied ?? []).filter((c: any) => c.type === 'object_extension');
380-
expect(errors.length + copied.length, 'precondition: the row was copied or tried').toBe(1);
381-
for (const error of errors) expect(error).not.toContain('ADR-0146 D10');
380+
expect(errors, 'no object_extension row failed').toEqual([]);
381+
expect(copied.map((c: any) => c.name), 'the row is copied').toEqual([OBJ]);
382+
const stored = rowsIn(stores, `${SVC}.custom`);
383+
expect(stored.map((r) => r.name), 'the copy is stored under its target').toEqual([OBJ]);
384+
expect(JSON.parse(String(stored[0]!.metadata)), 'the stored body is keyed by `extend`, with no `name`').toEqual({ extend: OBJ });
385+
});
386+
});
387+
388+
/**
389+
* objectstack#38709 — a duplicate's copied `object_extension` follows the
390+
* copied object. The source package holds a bare-named object (`ticket`, a row
391+
* stored before the namespace-prefix rule) and an extension of it; the copy
392+
* into `acme` renames the object to `acme_ticket`, and the copied extension
393+
* must extend that copy, never the source's object.
394+
*/
395+
describe('objectstack#38709 — a duplicated extension of the package\'s own bare object extends the copy', () => {
396+
it('the copy lands in `copied[]` under the copy\'s object name, its stored `extend` is the copy\'s object, and the source\'s extension is untouched', async () => {
397+
const { protocol, stores } = await boot();
398+
const SRC = 'com.example.tickets';
399+
await protocol.installPackage({ manifest: { id: SRC, name: 'tickets', version: '0.1.0', type: 'app', namespace: 'tix' } });
400+
// A stored row through the real door: the shape the two source rows below copy.
401+
expect(await outcome(() => saveExtension(protocol, PAIR, {}))).toEqual({ ok: true });
402+
const sys = stores.get('sys_metadata')!;
403+
const [template] = Array.from(sys.values()).filter((r) => r.type === 'object_extension' && r.package_id === PAIR);
404+
const row = (id: string, type: string, name: string, body: Record<string, unknown>) =>
405+
sys.set(id, { ...(template ?? {}), id, type, name, package_id: SRC, metadata: JSON.stringify(body) });
406+
if (!template) throw new Error('precondition: a stored metadata row to copy the row shape from');
407+
row('r_ticket', 'object', 'ticket', { name: 'ticket', label: 'Ticket', sharingModel: 'private', fields: { title: text('title', 'Title') } });
408+
row('r_ticket_ext', 'object_extension', 'ticket', { extend: 'ticket' });
409+
410+
const res = await protocol.duplicatePackage({ sourcePackageId: SRC, targetPackageId: 'com.example.acme', targetNamespace: 'acme' });
411+
expect((res.failed ?? []).map((f: any) => `${f.type}/${f.name}: ${f.error}`), 'nothing failed').toEqual([]);
412+
expect((res.copied ?? []).filter((c: any) => c.type === 'object_extension').map((c: any) => c.name), 'the extension is copied, under the copy\'s object name').toEqual(['acme_ticket']);
413+
const copies = Array.from(sys.values()).filter((r) => r.type === 'object_extension' && r.package_id === 'com.example.acme');
414+
expect(copies.map((r) => r.name), 'the copy is named by the copy\'s object').toEqual(['acme_ticket']);
415+
expect(JSON.parse(String(copies[0]!.metadata)), 'the copy extends the copy\'s object').toEqual({ extend: 'acme_ticket' });
416+
expect(JSON.parse(String(sys.get('r_ticket_ext')!.metadata)), 'the source row is unchanged').toEqual({ extend: 'ticket' });
382417
});
383418
});

0 commit comments

Comments
 (0)