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
13 changes: 13 additions & 0 deletions .changeset/33800-extender-added-fields-translate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@objectstack/objectql': patch
'@objectstack/spec': patch
---

fix(objectql): a field a dependent package adds through `objectExtensions` is served translated on `/api/v1/meta/object`

`SchemaRegistry.getPackagedObjectBase` (the packaged base that `/meta` translates an object against, and that the save door's translated-echo restore reads) now also carries each field a CODE `extend` contributor adds under a name the owner does not declare, as that extender ships it. A later extender's declaration of the same name replaces an earlier one, as in the fold, and a disabled package's extension adds nothing, as the fold withholds it. Before this change such a field counted as authored after the fact. Every non-default-locale read (`GET /api/v1/meta/object`, `GET /api/v1/meta/object/:name`, the list read, the form routes) then served its `label`, help text and option labels in the source language, even when the extender's own translation pack carried them. These reads now serve the pack's translation for those fields. A Studio save of such a read stores the source text again. Default-locale reads are unchanged.

Unchanged: the object scalars, every field the owner declares (including one an extender redeclares), and an extender's `fieldPatches` all stay the owner's. A patched label or description still keeps its text in every language. Fields added by a tenant overlay or by an extension of tenant provenance (`_provenance: 'org'`), the `.custom` pair's included, still count as authored after the fact. `@objectstack/spec` changes only in doc comments (`TranslateDocumentOptions.packagedBase` and `translateObject`).

Clause-②: no
Metadata-protocol: none
86 changes: 86 additions & 0 deletions packages/objectql/src/protocol-meta-effective-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,89 @@ describe('[#29673] a zh-CN read echoed back is stored as its source, and publish
expect(host.registry.getObject('crm_lead')).toBeDefined();
});
});

/**
* [#29673 door · a code dependent's added field] The save door translates its
* source against the same packaged base the read uses
* (`getPackagedObjectBase`), which now carries the fields a CODE extender adds.
* So a zh-CN read that serves such a field translated, echoed back by Studio's
* PUT, is stored as the extender's source text — with a stored row (its
* source) and without one (the packaged base is the source). Were the read and
* the door to disagree about the base, the translation would be stored as the
* tenant's own label, and served in English to every reader after it.
*/
describe('a zh-CN read of a field a code dependent adds, echoed back, is stored as its source', () => {
const MARKET = {
name: 'cb_market',
type: 'select',
label: 'Market',
options: [
{ value: 'domestic', label: 'Domestic' },
{ value: 'export', label: 'Export' },
],
};
const bundle = {
'zh-CN': {
objects: {
crm_account: {
fields: {
name: { label: '名称' },
cb_market: { label: '市场', options: { domestic: '内销', export: '出口' } },
},
},
},
},
};

/** The code layers: a base package's owner and a dependent's `objectExtensions` field. */
function codeLayers(): SchemaRegistry {
const reg = new SchemaRegistry({ multiTenant: false });
reg.logLevel = 'silent';
reg.installPackage({ id: 'com.example.crm', name: 'crm', version: '1.0.0' } as any, 'code');
reg.registerObject(
{ name: 'crm_account', label: 'Account', sharingModel: 'private', fields: { name: { name: 'name', type: 'text', label: 'Name' } } } as any,
'com.example.crm',
);
reg.installPackage({ id: 'com.example.carbon', name: 'carbon', version: '1.0.0' } as any, 'code');
reg.registerObject({ name: 'crm_account', fields: { cb_market: MARKET } } as any, 'com.example.carbon', undefined, 'extend', 200);
return reg;
}

const storedOf = (host: ReturnType<typeof makeHost>) =>
JSON.parse(Array.from(host.rows.values()).find((r) => r.name === 'crm_account' && r.state === 'active')!.metadata);

for (const withStoredRow of [true, false]) {
it(`stores 'Market' and its option labels, ${withStoredRow ? 'over a stored row' : 'with no stored row'}`, async () => {
const layers = codeLayers();
const packagedBase = () => layers.getPackagedObjectBase('crm_account');
const served: any = { ...structuredClone(layers.getObject('crm_account') as any), sharingModel: 'private' };
for (const k of Object.keys(served)) if (k.startsWith('_')) delete served[k];
const echo: any = translateMetadataDocument('object', structuredClone(served), bundle as any, { locale: 'zh-CN', packagedBase: packagedBase() });
// The precondition: the read serves the dependent's field translated.
expect(echo.fields.cb_market.label).toBe('市场');
expect(echo.fields.cb_market.options.map((o: any) => o.label)).toEqual(['内销', '出口']);

const host = makeHost(false);
if (withStoredRow) {
await host.engine.insert('sys_metadata', {
type: 'object', name: 'crm_account', organization_id: null, package_id: null, state: 'active',
metadata: JSON.stringify({ name: 'crm_account', label: 'Account', sharingModel: 'private', fields: { name: served.fields.name, cb_market: MARKET } }),
});
}
const protocol: any = new ObjectStackProtocolImplementation(host.engine, () => new Map<string, any>([
['i18n', { getLocales: () => Object.keys(bundle), getTranslations: (locale: string) => (bundle as any)[locale] }],
]));
protocol.getPackagedObjectBase = (name: string) => (name === 'crm_account' ? packagedBase() : undefined);
const saved: any = await protocol.saveMetaItem({ type: 'object', name: 'crm_account', item: echo, mode: 'publish' } as any);

const stored = storedOf(host);
expect(stored.fields.cb_market.label).toBe('Market');
expect(stored.fields.cb_market.options.map((o: any) => o.label)).toEqual(['Domestic', 'Export']);
expect(stored.fields.name.label).toBe('Name');
expect((saved.advisories ?? []).map((a: any) => a.path)).toEqual(expect.arrayContaining([
'objects.crm_account.fields.cb_market.label',
'objects.crm_account.fields.cb_market.options[domestic].label',
]));
});
}
});
168 changes: 166 additions & 2 deletions packages/objectql/src/protocol-packaged-object-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,22 @@ describe('#27559 getPackagedObjectBase — the packaged RULE set is the owner pl
expect((s.registry.getObject(OBJ) as any).label).toBe(EXTENSION_LABEL);
});

it('stays the owner declaration BY REFERENCE when no extender ships a rule', () => {
it('stays the owner declaration BY REFERENCE when no extender ships a rule or adds a field', () => {
const s = makeSession();
registerPackaged(s.registry);
registerExtension(s.registry);
// An extension that relabels the object and redeclares one of the
// owner's own fields adds nothing the packaged base carries.
s.registry.registerObject(
{
name: OBJ,
label: EXTENSION_LABEL,
fields: { name: { name: 'name', type: 'text', label: 'Full Name' } },
} as any,
EXT_PKG,
undefined,
'extend',
210,
);
expect(baseOf(s.protocol)).toBe(s.registry.getPackagedObjectOwner(OBJ)?.definition);
});

Expand All @@ -282,3 +294,155 @@ describe('#27559 getPackagedObjectBase — the packaged RULE set is the owner pl
expect(baseOf(s.protocol).validations.map((r: any) => r.message)).toEqual(['Extension says.']);
});
});

describe('getPackagedObjectBase — a field a code extender ADDS is packaged, as the extender ships it', () => {
// A dependent package's `objectExtensions` entry adds fields the owner does
// not declare, and ships the translations of their labels and option
// labels in its own pack. The catalog translates exactly what the code
// layers ship (the #27559 sentence for rules), so the packaged
// counterpart of such a field is the extender's declaration; read off the
// owner alone, the field counted as authored after the fact and `/meta`
// served its source-language strings under every locale.
const DEP_PKG = 'com.example.carbon';
const MARKET = {
name: 'cb_market',
type: 'select',
label: 'Market',
options: [
{ value: 'domestic', label: 'Domestic' },
{ value: 'export', label: 'Export' },
],
};

function registerDependentExtension(registry: SchemaRegistry, fields: Record<string, unknown>, packageId = DEP_PKG) {
registry.installPackage({ id: packageId, name: packageId, version: '1.0.0' } as any, 'code');
registry.registerObject({ name: OBJ, fields } as any, packageId, undefined, 'extend', 200);
}

it('carries a code dependent\'s added field, label and option labels, beside the owner\'s fields', () => {
const s = makeSession();
registerPackaged(s.registry);
registerDependentExtension(s.registry, { cb_market: MARKET });

const base = baseOf(s.protocol);
expect(base.fields.cb_market.label).toBe('Market');
expect(base.fields.cb_market.options.map((o: any) => o.label)).toEqual(['Domestic', 'Export']);
expect(base.fields.name.label).toBe('Name');
// The owner's own declaration is not mutated on the way.
expect((s.registry.getPackagedObjectOwner(OBJ)?.definition as any).fields.cb_market).toBeUndefined();
});

it('keeps the OWNER\'s declaration of a field an extender redeclares, and the owner\'s scalars (#8037)', () => {
const s = makeSession();
registerPackaged(s.registry);
s.registry.installPackage({ id: DEP_PKG, name: DEP_PKG, version: '1.0.0' } as any, 'code');
s.registry.registerObject(
{
name: OBJ,
label: EXTENSION_LABEL,
fields: { name: { name: 'name', type: 'text', label: 'Full Name' }, cb_market: MARKET },
} as any,
DEP_PKG,
undefined,
'extend',
200,
);

const base = baseOf(s.protocol);
expect(base.fields.name.label).toBe('Name');
expect(base.label).toBe(PACKAGED_LABEL);
expect(base.fields.cb_market.label).toBe('Market');
// The served object is the fold's, which the base must not follow.
expect((s.registry.getObject(OBJ) as any).fields.name.label).toBe('Full Name');
});

it('does not carry a field a tenant overlay adds, and does not move under the tenant\'s rename of an extender field', () => {
const s = makeSession();
registerPackaged(s.registry);
registerDependentExtension(s.registry, { cb_market: MARKET });
s.registry.registerObject(
{
name: OBJ,
label: PACKAGED_LABEL,
fields: {
name: { name: 'name', type: 'text', label: 'Name' },
cb_market: { ...MARKET, label: 'Sales Market' },
tenant_note: { name: 'tenant_note', type: 'text', label: 'Note' },
},
_packageId: 'sys_metadata', _provenance: 'org',
} as any,
'sys_metadata',
undefined,
'overlay',
);

const base = baseOf(s.protocol);
expect(base.fields.tenant_note).toBeUndefined();
expect(base.fields.cb_market.label).toBe('Market');
});

it('is no longer the owner BY REFERENCE once a code extender adds a field: the shared fixture\'s `loyalty_tier` is carried', () => {
const s = makeSession();
registerPackaged(s.registry);
registerExtension(s.registry);
const base = baseOf(s.protocol);
expect(base).not.toBe(s.registry.getPackagedObjectOwner(OBJ)?.definition);
expect(base.fields.loyalty_tier.label).toBe('Loyalty Tier');
expect(base.label).toBe(PACKAGED_LABEL);
});

it('carries nothing from a stored (tenant-provenance) extension, a `.custom` pair\'s row included', () => {
const s = makeSession();
registerPackaged(s.registry);
s.registry.registerObject(
{
name: OBJ,
fields: { region__c: { name: 'region__c', type: 'text', label: 'Region' } },
_packageId: 'app.showcase.custom', _provenance: 'org',
} as any,
'app.showcase.custom',
undefined,
'extend',
200,
);
expect((s.registry.getObject(OBJ) as any).fields.region__c).toBeDefined();
const base = baseOf(s.protocol);
expect(base.fields.region__c).toBeUndefined();
expect(base).toBe(s.registry.getPackagedObjectOwner(OBJ)?.definition);
});

it('does not fold a code extender\'s fieldPatches into the base: the owner\'s field text stays the packaged one', () => {
const s = makeSession();
registerPackaged(s.registry);
s.registry.installPackage({ id: DEP_PKG, name: DEP_PKG, version: '1.0.0' } as any, 'code');
s.registry.registerObject(
{ name: OBJ, fields: { cb_market: MARKET }, fieldPatches: { name: { label: 'Company Name', description: 'Patched.' } } } as any,
DEP_PKG,
undefined,
'extend',
200,
);
expect((s.registry.getObject(OBJ) as any).fields.name.label).toBe('Company Name');
const base = baseOf(s.protocol);
expect(base.fields.name.label).toBe('Name');
expect(base.fields.name.description).toBeUndefined();
expect(base.fields.cb_market.label).toBe('Market');
});

it('lets a later code extender\'s declaration of an added field replace an earlier one, as the fold does', () => {
const s = makeSession();
registerPackaged(s.registry);
registerDependentExtension(s.registry, { cb_market: MARKET });
s.registry.installPackage({ id: 'com.example.later', name: 'later', version: '1.0.0' } as any, 'code');
s.registry.registerObject(
{ name: OBJ, fields: { cb_market: { ...MARKET, label: 'Sales Market' } } } as any,
'com.example.later',
undefined,
'extend',
300,
);

expect(baseOf(s.protocol).fields.cb_market.label).toBe('Sales Market');
expect((s.registry.getObject(OBJ) as any).fields.cb_market.label).toBe('Sales Market');
});
});
Loading
Loading