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
20 changes: 20 additions & 0 deletions .changeset/36166-unmanaged-rename-stray-and-declared-namespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@objectstack/metadata-protocol': patch
---

fix(metadata-protocol)!: an unmanaged install renamed into a `targetNamespace` refuses a stray-object package and a declared namespace its object names do not carry, instead of copying them under a second prefix

Clause-②: no (narrowing)
Metadata-protocol: none
Ruling-ref: #36166 comment 7146137652

<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export changes shape. What moves is the answer the unmanaged install gives one request: a package body renamed into a `targetNamespace` whose object names contradict its namespace, declared or derived. No stored row, registry key or authored value changes, so there is nothing for a migration to rewrite and no tombstone. -->

**BREAKING** for a caller of the unmanaged install (`importUnmanagedPackage`, and `POST /api/v1/marketplace/install-local` with `installMode: 'unmanaged'`) that sends a `targetNamespace`. Two more package bodies are refused before the base exists, `400 INVALID_REQUEST` with one `details.refused` entry per object, and nothing is installed:

- **A package that declares no `namespace`, with a stray object.** The refusal for object names sharing one first word other than the namespace derived from the id now applies to every group of two or more such names, not only when every object shares it. `demoasset_asset`, `demoasset_site` and `site` used to copy into `trialdemoasset` as `trialdemoasset_demoasset_asset`, `trialdemoasset_demoasset_site` and `trialdemoasset_site`; now `demoasset_asset` and `demoasset_site` are refused, each entry naming the derived namespace and the shared prefix. Two objects sharing a short first word are refused with a stray as without one (`purchase_order`, `purchase_item` beside `vendor`). One object carrying the derived prefix still clears it.
- **A package that declares a `namespace` its object names do not carry.** Each object the package's own prefix rule refuses (the rule `defineStack` and the save door already hold every authored package to) is refused, naming the declared namespace. `namespace: 'other'` with `demoasset_asset` used to copy as `trialdemoasset_demoasset_asset`, and a package declaring `crm` with an object named `ticket` copied it as `trialdemoasset_ticket`; both are refused now. Without a `targetNamespace` the save door already refused both (`422 NAMESPACE_PREFIX`).

Unchanged: a single object with no namespace declared keeps the rename, foreign prefix or not (`demoasset_asset` alone copies as `trialdemoasset_demoasset_asset`), because one name cannot tell a two-word short name from a foreign prefix. So do object names that share no first word (`purchase_order` beside `asset_owner`), and a declared namespace the names carry.

The fix is in the package: declare `namespace` on it and name every object under that namespace, then install again.
13 changes: 8 additions & 5 deletions content/docs/deployment/install-modes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,11 @@ beside the package, or beside another copy of it, under names of its own. Send

The copy is renamed by the same rules as duplicating a package:

- **Every object takes the new namespace.** `crm_lead` copies as `acme_lead`,
and an object whose name lacks the package's prefix takes the new one in
front (`ticket` copies as `acme_ticket`).
- **Every object takes the new namespace.** `crm_lead` copies as `acme_lead`.
In a package that declares no `namespace`, an object whose name lacks the
prefix derived from the package id takes the new one in front (`ticket`
copies as `acme_ticket`). In a package that declares one, such an object is
refused (see the table below).
- **What names an object follows it.** A lookup's `reference`, a view's
`object`, a permission set's `objects` keys and the object part of its
`fields` keys, a translation's object keys, a form action's view target.
Expand All @@ -266,8 +268,9 @@ refusal is answered before the base exists, so nothing is installed:
| The rename | The answer |
|:---|:---|
| A `targetNamespace` the namespace rule refuses, or a shared one | `400 INVALID_REQUEST`, naming `targetNamespace` |
| An object whose copy name another object's copy takes (`ticket` beside `crm_ticket`) | `400 INVALID_REQUEST`, naming the object and the rename that clears it |
| A package that declares no `namespace`, with two or more objects whose names all begin with the same first word, not the namespace derived from the package id (`demoasset_asset` and `demoasset_site` in `com.acme.asset-lifecycle`, which derives `asset_lifecycle`; `purchase_order` and `purchase_item` too) | `400 INVALID_REQUEST`, naming each object, the derived namespace and the common prefix; declaring `namespace` on the package clears it |
| An object whose copy name another object's copy takes (`ticket` beside `crm_ticket`, in a package that declares no `namespace` and whose id derives `crm`) | `400 INVALID_REQUEST`, naming the object and the rename that clears it |
| A package that declares no `namespace`, where no object carries the namespace derived from the package id and two or more object names begin with the same first word, whatever other objects stand beside them (`demoasset_asset` and `demoasset_site`, with or without `site`, in `com.acme.asset-lifecycle`, which derives `asset_lifecycle`; `purchase_order` and `purchase_item` too) | `400 INVALID_REQUEST`, naming each object of the group, the derived namespace and the shared prefix; declaring `namespace` on the package and naming every object under it clears it. A package with a single object keeps the rename. |
| A package that declares a `namespace` some of its object names do not carry (`ticket` or `demoasset_asset` in a package that declares `crm`) | `400 INVALID_REQUEST`, naming each such object and the declared namespace; the install without `targetNamespace` refuses the same names (`422 NAMESPACE_PREFIX`) |
| A copy name that is not a legal item name, longer than an object's table name may be (63 characters), or longer than an item name may be (255) | `400 INVALID_REQUEST`, naming each item |
| A renamed key that meets a key the item already holds (a permission set granting both `crm_lead` and `acme_lead`) | `400 INVALID_REQUEST`, naming the item and both keys |
| A capability whose copy name another capability of the package declares (`crm.approve` beside `acme.approve`) | `400 INVALID_REQUEST`, naming the capability |
Expand Down
98 changes: 92 additions & 6 deletions packages/metadata-protocol/src/package-unmanaged-install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,10 @@ describe('planUnmanagedImport — `targetNamespace`, the rename-on-install (#307

it('refuses an object whose copy name another object\'s copy claims — INVALID_REQUEST / 400, naming it', () => {
// `lead` lacks the prefix, so its copy is `acme_lead`, which `crm_lead`'s copy already takes.
// Namespace-less (`crm` derived from the id): under a declared `crm` the bare
// name is refused first, as the declared prefix rule refuses it.
const body = renamableBody();
delete body.namespace;
(body.objects as unknown[]).push({ name: 'lead', label: 'Bare', sharingModel: 'private', fields: {} });
const err = (() => { try { plan(body, { targetNamespace: 'acme' }); } catch (e) { return e as any; } })();
const envelope = resolveThrownHttpError(err);
Expand Down Expand Up @@ -1217,26 +1220,67 @@ describe('a namespace derived from the id, contradicted by the object names\' co
]);
});

it('CONTROL: a single-object package is not refused — one name carries no common prefix', () => {
it('DECIDED: a single-object package keeps the ADR rename, foreign prefix or not — one name carries no signal', () => {
// Decided to stay as ADR-0048 §3.5's rename, recorded on the ADR: a single
// name cannot tell a two-word short name from a foreign prefix.
expect(renamedObjects(namespacelessBody(['demoasset_asset']))).toEqual(['demoasset_asset -> trialdemoasset_demoasset_asset']);
});

it('one stray object beside a shared prefix does not lift the refusal — the group is refused, the stray named nowhere', () => {
for (const stray of ['site', 'purchase_order']) {
const err = refusedPlan(namespacelessBody(['demoasset_asset', 'demoasset_site', stray]));
expect([resolveThrownHttpError(err).status, resolveThrownHttpError(err).code]).toEqual([400, 'INVALID_REQUEST']);
expect(err.details.targetNamespace).toBe('trialdemoasset');
expect(err.details.refused.map((r: any) => `${r.type}/${r.name}`)).toEqual(['object/demoasset_asset', 'object/demoasset_site']);
const [{ reason }] = err.details.refused;
expect(reason).toContain("derived from its id as 'asset_lifecycle_b'");
expect(reason).toContain("begin 'demoasset_'");
}
});

it('two refused groups in one body: every object of both is named, in manifest order', () => {
const err = refusedPlan(namespacelessBody(['beta_x', 'alpha_x', 'beta_y', 'alpha_y']));
expect([resolveThrownHttpError(err).status, resolveThrownHttpError(err).code]).toEqual([400, 'INVALID_REQUEST']);
expect(err.details.refused.map((r: any) => `${r.type}/${r.name}`))
.toEqual(['object/beta_x', 'object/alpha_x', 'object/beta_y', 'object/alpha_y']);
});

it('the verb refuses the stray-object package before anything is written: no base, no row', async () => {
const { protocol, writes, packages, table } = makeProtocol();
const err = await refusalOf(() => protocol.importUnmanagedPackage({
manifest: namespacelessBody(['demoasset_asset', 'demoasset_site', 'site']), targetNamespace: 'trialdemoasset',
}));
expect([resolveThrownHttpError(err).status, resolveThrownHttpError(err).code]).toEqual([400, 'INVALID_REQUEST']);
expect(err.details.refused.map((r: any) => r.name)).toEqual(['demoasset_asset', 'demoasset_site']);
expect(writes).toEqual([]);
expect(packages.size).toBe(0);
expect(table('sys_metadata')).toEqual([]);
});

it('a two-word short name the package repeats is refused with a stray as without one — the same family, `namespace` lifts it', () => {
expect(refusedPlan(namespacelessBody(['purchase_order', 'purchase_item'])).details.refused.map((r: any) => r.name))
.toEqual(['purchase_order', 'purchase_item']);
expect(refusedPlan(namespacelessBody(['purchase_order', 'purchase_item', 'vendor'])).details.refused.map((r: any) => r.name))
.toEqual(['purchase_order', 'purchase_item']);
// Declared, the prefix is the package's own: the names copy under the target in its place.
expect(renamedObjects(namespacelessBody(['purchase_order', 'purchase_item'], { namespace: 'purchase' }))).toEqual([
'purchase_order -> trialdemoasset_order',
'purchase_item -> trialdemoasset_item',
]);
});

it('CONTROL: one object carrying the derived prefix confirms the derived namespace — nothing is refused', () => {
expect(renamedObjects(namespacelessBody(['asset_lifecycle_b_asset', 'demoasset_site']))).toEqual([
'asset_lifecycle_b_asset -> trialdemoasset_asset',
'demoasset_site -> trialdemoasset_demoasset_site',
]);
});

it('CONTROL: a declared `namespace` is judged as before, whatever the names look like', () => {
it('CONTROL: a declared `namespace` the object names carry renames as before — in place of the declared prefix', () => {
expect(renamedObjects(namespacelessBody(['demoasset_asset', 'demoasset_site'], { namespace: 'demoasset' }))).toEqual([
'demoasset_asset -> trialdemoasset_asset',
'demoasset_site -> trialdemoasset_site',
]);
expect(renamedObjects(namespacelessBody(['demoasset_asset', 'demoasset_site'], { namespace: 'other' }))).toEqual([
'demoasset_asset -> trialdemoasset_demoasset_asset',
'demoasset_site -> trialdemoasset_demoasset_site',
]);
});

it('CONTROL: without a rename the plan does not ask — the save door\'s prefix rule answers those names', () => {
Expand All @@ -1258,6 +1302,48 @@ describe('a namespace derived from the id, contradicted by the object names\' co
});
});

describe('a declared namespace the object names contradict — refused on the rename path, never double-prefixed', () => {
const refusedPlan = (body: Record<string, unknown>) =>
(() => { try { plan(body, { targetNamespace: 'trialdemoasset' }); } catch (e) { return e as any; } throw new Error('expected a refusal'); })();

it('every object the declared prefix rule refuses is refused — INVALID_REQUEST / 400, naming each and the declared namespace', () => {
const err = refusedPlan(namespacelessBody(['demoasset_asset', 'demoasset_site'], { namespace: 'other' }));
expect([resolveThrownHttpError(err).status, resolveThrownHttpError(err).code]).toEqual([400, 'INVALID_REQUEST']);
expect(err.details.targetNamespace).toBe('trialdemoasset');
expect(err.details.refused.map((r: any) => `${r.type}/${r.name}`)).toEqual(['object/demoasset_asset', 'object/demoasset_site']);
expect(err.details.refused[0].reason).toContain("declares `namespace` 'other'");
});

it('only the objects that miss the declared prefix are named — one carrying it confirms nothing for the rest', () => {
const err = refusedPlan(namespacelessBody(['other_asset', 'demoasset_site'], { namespace: 'other' }));
expect([resolveThrownHttpError(err).status, resolveThrownHttpError(err).code]).toEqual([400, 'INVALID_REQUEST']);
expect(err.details.refused.map((r: any) => r.name)).toEqual(['demoasset_site']);
// A bare name too: the rename has no declared prefix in it to replace.
expect(refusedPlan(namespacelessBody(['other_asset', 'site'], { namespace: 'other' })).details.refused.map((r: any) => r.name))
.toEqual(['site']);
});

it('the verb refuses before anything is written: no base, no row', async () => {
const { protocol, writes, packages, table } = makeProtocol();
const err = await refusalOf(() => protocol.importUnmanagedPackage({
manifest: namespacelessBody(['demoasset_asset', 'demoasset_site'], { namespace: 'other' }), targetNamespace: 'trialdemoasset',
}));
expect([resolveThrownHttpError(err).status, resolveThrownHttpError(err).code]).toEqual([400, 'INVALID_REQUEST']);
expect(err.details.refused.map((r: any) => r.name)).toEqual(['demoasset_asset', 'demoasset_site']);
expect(writes).toEqual([]);
expect(packages.size).toBe(0);
expect(table('sys_metadata')).toEqual([]);
});

it('CONTROL: without a rename the save door\'s prefix rule refuses the same body — the two doors agree', async () => {
const { protocol } = makeProtocol();
const err = await refusalOf(() => protocol.importUnmanagedPackage({
manifest: namespacelessBody(['demoasset_asset', 'demoasset_site'], { namespace: 'other' }),
}));
expect([resolveThrownHttpError(err).status, resolveThrownHttpError(err).code]).toEqual([422, 'NAMESPACE_PREFIX']);
});
});

describe('the rename parity — one package, one `targetNamespace`, two doors, identical items (#30794)', () => {
/** Every active row of one package: `type/name` → its stored body. */
const itemsOf = (table: (name: string) => Row[], packageId: string) => Object.fromEntries(
Expand Down
Loading
Loading