diff --git a/.changeset/36166-unmanaged-rename-stray-and-declared-namespace.md b/.changeset/36166-unmanaged-rename-stray-and-declared-namespace.md new file mode 100644 index 00000000000..98c74f6a91d --- /dev/null +++ b/.changeset/36166-unmanaged-rename-stray-and-declared-namespace.md @@ -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 + + + +**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. diff --git a/content/docs/deployment/install-modes.mdx b/content/docs/deployment/install-modes.mdx index b0ba3a3fc96..bc2f518a320 100644 --- a/content/docs/deployment/install-modes.mdx +++ b/content/docs/deployment/install-modes.mdx @@ -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. @@ -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 | diff --git a/packages/metadata-protocol/src/package-unmanaged-install.test.ts b/packages/metadata-protocol/src/package-unmanaged-install.test.ts index cec7ab420f1..cd4c9305188 100644 --- a/packages/metadata-protocol/src/package-unmanaged-install.test.ts +++ b/packages/metadata-protocol/src/package-unmanaged-install.test.ts @@ -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); @@ -1217,10 +1220,55 @@ 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', @@ -1228,15 +1276,11 @@ describe('a namespace derived from the id, contradicted by the object names\' co ]); }); - 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', () => { @@ -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) => + (() => { 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( diff --git a/packages/metadata-protocol/src/package-unmanaged-install.ts b/packages/metadata-protocol/src/package-unmanaged-install.ts index 0944eee5c9f..1ff9a296713 100644 --- a/packages/metadata-protocol/src/package-unmanaged-install.ts +++ b/packages/metadata-protocol/src/package-unmanaged-install.ts @@ -112,6 +112,7 @@ import { UNMANAGED_INSTALL_KEY_DISPOSITIONS, unmanagedInstallValueCarries, MetadataProtectionFields, + validateObjectNamespacePrefix, } from '@objectstack/spec/kernel'; import type { ImportUnmanagedPackageResponse } from '@objectstack/spec/api'; import { METADATA_ITEM_NAME_PATTERN, PLURAL_TO_SINGULAR, applyProtection } from '@objectstack/spec/shared'; @@ -619,15 +620,11 @@ export function planUnmanagedImport( items: items.map((item) => ({ type: item.type, name: item.name, body: item.body })), retryVerb: 'install', }); - if (declaredNamespace === undefined) { - const contradicted = derivedNamespaceContradiction( - sourcePackageId, - sourceNamespace, - namespace, - items.filter((item) => item.type === 'object').map((item) => item.name), - ); - if (contradicted !== undefined) throw unmanagedInstallRenameRefusal(sourcePackageId, namespace, contradicted); - } + const objectNames = items.filter((item) => item.type === 'object').map((item) => item.name); + const contradicted = declaredNamespace === undefined + ? derivedNamespaceContradiction(sourcePackageId, sourceNamespace, namespace, objectNames) + : declaredNamespaceContradiction(sourcePackageId, declaredNamespace, namespace, objectNames); + if (contradicted !== undefined) throw unmanagedInstallRenameRefusal(sourcePackageId, namespace, contradicted); const refused: UnmanagedImportRenameRefusal[] = []; for (const item of items) { const reason = (item.type === 'object' ? planned.objectRefusal(item.name) : undefined) @@ -680,25 +677,38 @@ export function planUnmanagedImport( * (`deriveNamespaceFromPackageId`), and the rename engine trusts it: an object * name without that prefix takes the target prefix in front of its whole name * (ADR-0048 §3.5 as amended). When the producer's real namespace was dropped - * from the manifest, every object still carries it, and that rule turns the - * copy into `__` beside items the swap left alone, one app - * in three spellings. + * from the manifest, its objects still carry it, and that rule turns the copy + * into `__` beside items the swap left alone, one app in + * three spellings. * - * The signal is the object names' COMMON prefix, never one name's shape: a + * The signal is a prefix the object names SHARE, never one name's shape: a * single name cannot tell a two-word short name (`purchase_order`) from a - * foreign prefix (`demoasset_site`). So only when there are at least two - * objects, every object name begins with the same first segment, that segment - * is not the derived namespace, and no object carries the derived prefix, is - * the copy refused: every object at its own name, naming the derived - * namespace and the common prefix, and asking for the missing key. Otherwise - * the ADR rename applies, unchanged. Object names only: ADR-0129 D2 puts the - * namespace on object names, while a package's flows, datasets or reports may - * be named without it, and its translation items are named by this planner. - * Without a rename the save door's own prefix rule refuses the same names - * (`NAMESPACE_PREFIX`), so this is asked only on the rename path. + * foreign prefix (`demoasset_site`). So when no object carries the derived + * prefix, every group of two or more object names beginning with the same + * first segment is refused: each object of the group at its own name, naming + * the derived namespace and the shared prefix, and asking for the missing key. + * A stray object beside the group (`site` beside `demoasset_asset` and + * `demoasset_site`) neither joins the refusal nor lifts it; one object + * carrying the derived prefix confirms the guess, and nothing is refused. + * + * Decided to stay as the ADR rename (a test pins each): a package whose object + * names share no first segment (`purchase_order` beside `asset_owner`, bare + * `asset` beside `site`), and a single foreign-prefixed name — one name + * carries no signal, so `demoasset_asset` alone copies as + * `_demoasset_asset`. Refused as one family with the group above: a + * two-word short name its own package repeats (`purchase_order` beside + * `purchase_item`), stray or not — `namespace` declared on the package, with + * every object named under it, lifts it. * - * @returns one refusal per object, or `undefined` when the names do not - * contradict the derived namespace. + * Object names only: ADR-0129 D2 puts the namespace on object names, while a + * package's flows, datasets or reports may be named without it, and its + * translation items are named by this planner. Without a rename the save + * door's own prefix rule refuses the same names (`NAMESPACE_PREFIX`), and the + * stray beside a refused group with them, so this is asked only on the rename + * path. + * + * @returns one refusal per object of a shared prefix, or `undefined` when the + * names do not contradict the derived namespace. */ function derivedNamespaceContradiction( sourcePackageId: string, @@ -706,24 +716,89 @@ function derivedNamespaceContradiction( targetNamespace: string, objectNames: readonly string[], ): UnmanagedImportRenameRefusal[] | undefined { - if (objectNames.length < 2) return undefined; if (objectNames.some((name) => name.startsWith(`${derivedNamespace}_`))) return undefined; - const firstSegment = (name: string): string | undefined => { + // By first segment. A name whose first segment is the derived namespace + // carries the derived prefix, so it returned above. + const bySegment = new Map(); + for (const name of objectNames) { const separator = name.indexOf('_'); - return separator > 0 ? name.slice(0, separator) : undefined; - }; - const common = firstSegment(objectNames[0]); - if (common === undefined || common === derivedNamespace) return undefined; - if (!objectNames.every((name) => firstSegment(name) === common)) return undefined; - return objectNames.map((name) => ({ - type: 'object', - name, - reason: `Object '${name}' is not copied: package '${sourcePackageId}' declares no \`namespace\`, so its ` - + `namespace was derived from its id as '${derivedNamespace}', but every one of its ${objectNames.length} ` - + `object names begins '${common}_' instead. Renamed into namespace '${targetNamespace}' it would be ` - + `'${targetNamespace}_${name}', keeping a prefix the package never declared. Declare \`namespace\` on ` - + 'the package, the prefix its object names carry, then install again.', - })); + if (separator <= 0) continue; + const segment = name.slice(0, separator); + bySegment.set(segment, [...(bySegment.get(segment) ?? []), name]); + } + const refused: UnmanagedImportRenameRefusal[] = []; + for (const [segment, names] of bySegment) { + if (names.length < 2) continue; + const whole = names.length === objectNames.length; + const sharing = whole + ? `every one of its ${objectNames.length} object names begins '${segment}_'` + : `${names.length} of its ${objectNames.length} object names ` + + `(${names.map((n) => `'${n}'`).join(', ')}) begin '${segment}_'`; + // A declared namespace is held to every object name + // ({@link declaredNamespaceContradiction}), so where the group is not the + // whole package the remedy names the other objects too: declaring the + // group's prefix alone would be refused on the next attempt. + const remedy = whole + ? 'Declare `namespace` on the package, the prefix its object names carry, then install again.' + : 'Declare `namespace` on the package, the prefix its object names carry, and name every other ' + + 'object under that namespace too, then install again.'; + for (const name of names) { + refused.push({ + type: 'object', + name, + reason: `Object '${name}' is not copied: package '${sourcePackageId}' declares no \`namespace\`, so its ` + + `namespace was derived from its id as '${derivedNamespace}', but ${sharing} instead. Renamed ` + + `into namespace '${targetNamespace}' it would be '${targetNamespace}_${name}', keeping a prefix ` + + `the package never declared. ${remedy}`, + }); + } + } + if (refused.length === 0) return undefined; + const order = new Map(objectNames.map((name, i) => [name, i])); + return refused.sort((a, b) => (order.get(a.name) ?? 0) - (order.get(b.name) ?? 0)); +} + +/** + * ⛔ A DECLARED NAMESPACE THE OBJECT NAMES CONTRADICT IS REFUSED, NEVER RENAMED. + * A package that declares `namespace` states the prefix every object it + * defines carries (ADR-0129 D2), and every authoring door holds it to that: + * `defineStack` (`STACK_NAMESPACE_PREFIX_INVALID`), the publish pre-flight and + * the save door (`NAMESPACE_PREFIX`) ask one judge, + * `validateObjectNamespacePrefix`, which this asks too. An object name the + * judge refuses under the declared namespace is no spelling the package could + * author, so it is not the "name that lacks the package's prefix" ADR-0048 + * §3.5's rename puts the target in front of: renamed, `demoasset_asset` under + * a declared `other` would copy as `_demoasset_asset`, a prefix the + * package never declared, and a bare `lead` under a declared `crm` as + * `_lead`, a name no door would have stored under the source. Each + * such object is refused at its own name, naming + * the declared namespace and the judge's sentence. A namespace-less package is + * the legacy shape the judge skips, so its unprefixed names keep the ADR + * rename ({@link derivedNamespaceContradiction}). Without a rename the save + * door refuses the same names, so this is asked only on the rename path. + * + * @returns one refusal per object the judge refuses, or `undefined` when + * every object name carries the declared prefix. + */ +function declaredNamespaceContradiction( + sourcePackageId: string, + declaredNamespace: string, + targetNamespace: string, + objectNames: readonly string[], +): UnmanagedImportRenameRefusal[] | undefined { + const refused = objectNames.flatMap((name) => { + const violation = validateObjectNamespacePrefix(name, declaredNamespace); + return violation === null ? [] : [{ + type: 'object', + name, + reason: `Object '${name}' is not copied: package '${sourcePackageId}' declares \`namespace\` ` + + `'${declaredNamespace}', and its prefix rule refuses the name: ${violation} Renamed into namespace ` + + `'${targetNamespace}' it would be '${targetNamespace}_${name}': the rename puts the target in place ` + + 'of the declared prefix, and this name carries none to replace. Declare the `namespace` the object ' + + 'names carry, or name each object under the declared one, then install again.', + }]; + }); + return refused.length > 0 ? refused : undefined; } /** [#30794] One item the rename into a `targetNamespace` cannot copy, and why. */