Skip to content

Commit 5eb28cd

Browse files
committed
fix(objectql): an extension whose extend folds to a legacy key is filed under that key, and the spelling is named once (#28858)
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 45efba2 commit 5eb28cd

3 files changed

Lines changed: 197 additions & 1 deletion

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#28858, ADR-0146 D10] An `extend` that `getObject` folds to a legacy
5+
* `<ns>__<name>` key never opens a second object entry.
6+
*
7+
* A vendor package still ships its Case under the legacy key `crm__case`
8+
* (the published `ObjectSchema` accepts the name), so `getObject('case')`
9+
* folds to it. A second package extends it as `extend: 'case'`. The
10+
* extension is filed under the key the object is registered under, its field
11+
* lands on the vendor's object, no owner-less `case` entry opens beside it,
12+
* and the spelling is named once at `warn`. Never a boot refusal (AGENTS.md
13+
* *Startup registry reads*). The `object_extension` item door still refuses
14+
* the folded spelling at save (#28853, pinned in `@objectstack/runtime`).
15+
*/
16+
17+
import { describe, it, expect, vi, afterEach } from 'vitest';
18+
import { SchemaRegistry } from './registry.js';
19+
import { ObjectQL } from './engine.js';
20+
21+
const VENDOR = 'com.vendor.crm';
22+
const EXT = 'com.acme.case-region';
23+
const LEGACY_KEY = 'crm__case';
24+
const SHORT = 'case';
25+
26+
const vendorCase = () => ({
27+
name: LEGACY_KEY,
28+
label: 'Case',
29+
fields: { subject: { name: 'subject', type: 'text', label: 'Subject' } },
30+
});
31+
32+
const region = { region: { name: 'region', type: 'text', label: 'Region' } };
33+
34+
function registry(): SchemaRegistry {
35+
const reg = new SchemaRegistry({ multiTenant: false, searchCompanion: false } as never);
36+
reg.logLevel = 'silent';
37+
return reg;
38+
}
39+
40+
const spellingLines = (warn: { mock: { calls: unknown[][] } }): string[] =>
41+
warn.mock.calls.map((c) => String(c[0])).filter((l) => l.includes(`'${LEGACY_KEY}'`) && l.includes(EXT));
42+
43+
afterEach(() => vi.restoreAllMocks());
44+
45+
describe('#28858 — an extension whose `extend` folds to a legacy key lands on the object it folds to', () => {
46+
it('a manifest objectExtensions entry: the field is on the vendor\'s object, no second entry opens, named once', () => {
47+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
48+
const ql = new ObjectQL();
49+
(ql.registry as SchemaRegistry).logLevel = 'silent';
50+
ql.registerApp({ id: VENDOR, name: 'CRM', version: '1.0.0', objects: [vendorCase()] });
51+
ql.registerApp({
52+
id: EXT, name: 'Case region', version: '1.0.0',
53+
objectExtensions: [{ extend: SHORT, fields: region }],
54+
});
55+
56+
const reg = ql.registry as SchemaRegistry;
57+
expect(Object.keys((reg.getObject(LEGACY_KEY) as any).fields)).toEqual(expect.arrayContaining(['subject', 'region']));
58+
expect(reg.getObjectContributors(SHORT)).toEqual([]);
59+
expect(reg.getObjectContributors(LEGACY_KEY).map((c) => [c.packageId, c.ownership]))
60+
.toEqual([[VENDOR, 'own'], [EXT, 'extend']]);
61+
expect(reg.getAllObjects().map((o) => o.name).filter((n) => n === SHORT || n === LEGACY_KEY)).toEqual([LEGACY_KEY]);
62+
// The short name still reads the one object, with the field.
63+
expect(Object.keys((reg.getObject(SHORT) as any).fields)).toContain('region');
64+
65+
// Re-reading and re-registering name it no more than once.
66+
reg.invalidateAll();
67+
reg.getObject(LEGACY_KEY);
68+
ql.registerApp({ id: EXT, name: 'Case region', version: '1.0.0', objectExtensions: [{ extend: SHORT, fields: region }] });
69+
const lines = spellingLines(warn);
70+
expect(lines).toHaveLength(1);
71+
expect(lines[0]).toContain(`'${SHORT}'`);
72+
});
73+
74+
it('registerObjectExtension (the stored row\'s verb) files under the resolved key and answers it', () => {
75+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
76+
const reg = registry();
77+
reg.registerObject(vendorCase() as any, VENDOR, undefined, 'own');
78+
79+
const filed = reg.registerObjectExtension({ extend: SHORT, fields: region, _provenance: 'org' }, EXT);
80+
81+
expect(filed).toBe(LEGACY_KEY);
82+
expect(reg.getObjectContributors(SHORT)).toEqual([]);
83+
expect(Object.keys((reg.getObject(LEGACY_KEY) as any).fields)).toContain('region');
84+
expect(spellingLines(warn)).toHaveLength(1);
85+
// The withdrawal the row delete makes reaches the same entry.
86+
expect(reg.withdrawObjectExtension(SHORT, EXT)).toBe(true);
87+
expect(Object.keys((reg.getObject(LEGACY_KEY) as any).fields)).not.toContain('region');
88+
});
89+
90+
it('control: an extension spelled with the registered key registers as before, with no warn', () => {
91+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
92+
const reg = registry();
93+
reg.registerObject(vendorCase() as any, VENDOR, undefined, 'own');
94+
95+
expect(reg.registerObjectExtension({ extend: LEGACY_KEY, fields: region }, EXT)).toBe(LEGACY_KEY);
96+
expect(Object.keys((reg.getObject(LEGACY_KEY) as any).fields)).toContain('region');
97+
expect(reg.getObjectContributors(SHORT)).toEqual([]);
98+
expect(spellingLines(warn)).toEqual([]);
99+
});
100+
101+
it('control: an object registered under the exact name keeps its extensions, even beside a legacy key that shares the short name', () => {
102+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
103+
const reg = registry();
104+
reg.registerObject(vendorCase() as any, VENDOR, undefined, 'own');
105+
reg.registerObject({ name: SHORT, label: 'Support case', fields: { title: { name: 'title', type: 'text', label: 'Title' } } } as any, 'com.other.support', undefined, 'own');
106+
107+
expect(reg.registerObjectExtension({ extend: SHORT, fields: region }, EXT)).toBe(SHORT);
108+
expect(reg.getObjectContributors(SHORT).map((c) => c.ownership)).toEqual(['own', 'extend']);
109+
expect(Object.keys((reg.getObject(LEGACY_KEY) as any).fields)).not.toContain('region');
110+
expect(spellingLines(warn)).toEqual([]);
111+
});
112+
});

‎packages/objectql/src/registry.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4285,6 +4285,12 @@ export class SchemaRegistry {
42854285
if (ownership === 'extend') {
42864286
const extendRefusal = extendContributionRefusal(schema, packageId);
42874287
if (extendRefusal) throw extendRefusal;
4288+
// [#28858] …filed under the key the object it extends is registered
4289+
// under — see {@link extensionTargetKey}. Every entry that registers an
4290+
// extension reaches this line: the manifest `objectExtensions` loop and
4291+
// a stored `object_extension` row ({@link registerObjectExtension}).
4292+
const target = this.extensionTargetKey(String(schema.name ?? ''), packageId);
4293+
if (target !== schema.name) schema = { ...schema, name: target };
42884294
}
42894295
// [ADR-0144 D6] A `summary` roll-up that reads a `record`-mode amount —
42904296
// `sum` / `avg` / `min` / `max` of a child field whose rows each carry
@@ -6183,6 +6189,46 @@ export class SchemaRegistry {
61836189
);
61846190
}
61856191

6192+
/**
6193+
* [#28858] Spellings of an `extend` already named, keyed
6194+
* `spelled|resolved|package`. Per registry, for the reason
6195+
* {@link shadowedExtensionQueueWarned} gives.
6196+
*/
6197+
private extensionSpellingWarned = new Set<string>();
6198+
6199+
/**
6200+
* [#28858, ADR-0146 D10] The key an extension of `name` is filed under: the
6201+
* key {@link getObject} reads `name` as. That is `name` itself whenever an
6202+
* entry is registered under it, or nothing is registered that `name` folds
6203+
* to. It differs only for a short name `getObject` folds to a legacy
6204+
* `<ns>__<name>` key ({@link resolveObjectKey}); filing such an extension
6205+
* under the short name would open a second, owner-less entry beside the
6206+
* object it meant, which nothing serves and nothing checks.
6207+
*
6208+
* The folded spelling is named once at `warn`, with the key to write. ⛔
6209+
* Never a refusal: the manifest loop and the boot hydration run while
6210+
* packages are still registering (AGENTS.md *Startup registry reads*). The
6211+
* `object_extension` item door refuses the same spelling at save (#28834),
6212+
* where the author is present to fix it. A legacy owner that registers
6213+
* AFTER the extension is not yet there to fold to, so that extension is
6214+
* filed under its own spelling, as before.
6215+
*/
6216+
private extensionTargetKey(name: string, packageId: string | undefined): string {
6217+
if (name === '' || this.objectContributors.has(name)) return name;
6218+
const resolved = this.resolveObjectKey(name);
6219+
if (resolved === undefined || resolved === name) return name;
6220+
const key = `${name}|${resolved}|${packageId ?? ''}`;
6221+
if (!this.extensionSpellingWarned.has(key)) {
6222+
this.extensionSpellingWarned.add(key);
6223+
console.warn(
6224+
`[Registry] An extension${packageId ? ` from package "${packageId}"` : ''} names its object '${name}', ` +
6225+
`which is registered as '${resolved}': the extension is filed under '${resolved}', the object it ` +
6226+
`reads as. Write \`extend: '${resolved}'\` to name the object by the key it is registered under.`,
6227+
);
6228+
}
6229+
return resolved;
6230+
}
6231+
61866232
/**
61876233
* [#28574] Withdraw ONE package's `'extend'` contribution to an object — the
61886234
* delete of that package's `object_extension` item. The owner, the overlay

‎packages/runtime/src/domains/meta-object-extension-item-door-fit.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* sentence and that no row was left.
3030
*/
3131

32-
import { describe, it, expect, beforeAll, afterAll, afterEach } from 'vitest';
32+
import { describe, it, expect, beforeAll, afterAll, afterEach, vi } from 'vitest';
3333
import { mkdtempSync, rmSync } from 'node:fs';
3434
import { tmpdir } from 'node:os';
3535
import { join } from 'node:path';
@@ -199,3 +199,41 @@ describe('an object_extension item names its target by the key it is registered
199199
expect(engine.registry.getObjectContributors(LEGACY_SHORT)).toEqual([]);
200200
});
201201
});
202+
203+
describe('a stored object_extension row whose extend folds to a legacy key lands on that object at the boot hydration (#28858)', () => {
204+
it('the field is on the legacy-keyed object, no second entry opens, and the spelling is named once', async () => {
205+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
206+
try {
207+
const { engine, p } = await boot();
208+
// A row stored under the short spelling: the item door refuses
209+
// that spelling now (#28834), so it is a row written before it.
210+
// Stored as the registered key, then respelled at rest.
211+
await p.saveMetaItem({ type: 'object_extension', name: LEGACY_KEY, packageId: PAIR, item: region(LEGACY_KEY), mode: 'draft' });
212+
await p.publishMetaItem({ type: 'object_extension', name: LEGACY_KEY, packageId: PAIR });
213+
const stored: any[] = ((await engine.find('sys_metadata', { where: { type: 'object_extension' }, context: { isSystem: true } })) ?? [])
214+
.filter((r: any) => r.package_id === PAIR);
215+
expect(stored).toHaveLength(1);
216+
const body = typeof stored[0].metadata === 'string' ? JSON.parse(stored[0].metadata) : stored[0].metadata;
217+
await engine.update('sys_metadata', {
218+
id: stored[0].id,
219+
name: LEGACY_SHORT,
220+
metadata: JSON.stringify({ ...body, name: LEGACY_SHORT, extend: LEGACY_SHORT }),
221+
}, { context: { isSystem: true } });
222+
// A fresh process: the registry holds no contribution of the pair.
223+
engine.registry.withdrawObjectExtension(LEGACY_KEY, PAIR);
224+
expect(engine.registry.getObjectContributors(LEGACY_KEY).map((c: any) => c.packageId)).toEqual([BASE]);
225+
226+
const result = await p.loadMetaFromDb();
227+
expect(result.errors).toBe(0);
228+
expect(engine.registry.getObjectContributors(LEGACY_SHORT)).toEqual([]);
229+
expect(engine.registry.getObjectContributors(LEGACY_KEY).map((c: any) => [c.packageId, c.ownership]))
230+
.toEqual([[BASE, 'own'], [PAIR, 'extend']]);
231+
expect(Object.keys((engine.registry.getObject(LEGACY_KEY) as any)?.fields ?? {})).toEqual(expect.arrayContaining(['region__c', 'subject']));
232+
const lines = warn.mock.calls.map((c) => String(c[0]))
233+
.filter((l) => l.includes(`'${LEGACY_SHORT}'`) && l.includes(`'${LEGACY_KEY}'`) && l.includes(PAIR));
234+
expect(lines).toHaveLength(1);
235+
} finally {
236+
warn.mockRestore();
237+
}
238+
});
239+
});

0 commit comments

Comments
 (0)