Skip to content

Commit c31e8bc

Browse files
committed
fix(objectql,rest): serve an object an extension renames without the old plural
Claude-Session: https://claude.ai/code/session_01Utp6CYaDMYnWxbm95qon7G Co-authored-by: Claude <noreply@anthropic.com>
1 parent beb000b commit c31e8bc

2 files changed

Lines changed: 109 additions & 6 deletions

File tree

‎packages/objectql/src/registry.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,9 @@ function isLeftOutExtension(contributor: ObjectContributor, options: ObjectExten
763763
* the tenant's `message` and `active`, never the tenant's condition.
764764
* - The three {@link OBJECT_FOLD_SCALAR_KEYS} (`label`, `pluralLabel`,
765765
* `description`) are overridden last-writer-wins, subject to the
766-
* `tenantAuthored` yield rule below.
766+
* `tenantAuthored` yield rule below. [#30391] A `label` that renames the
767+
* object without a `pluralLabel` beside it also takes the running
768+
* `pluralLabel` off: the plural named the old label.
767769
* - `fieldPatches` [ADR-0146 D10, second rule] is APPLIED, never carried:
768770
* after the merges above, each patch writes its `BASE_FIELD_DISPLAY_WHITELIST`
769771
* keys onto the named field and its option labels onto the options whose
@@ -843,7 +845,17 @@ function mergeObjectDefinitions(
843845
// Override scalar props (last writer wins) — [ADR-0029 D9.2a] unless the base has been
844846
// authored by the tenant, in which case the extender's packaged default yields.
845847
const yields = (key: ObjectFoldScalarKey): boolean => tenantAuthored?.has(key) === true;
846-
if (extension.label !== undefined && !yields('label')) merged.label = extension.label;
848+
if (extension.label !== undefined && !yields('label')) {
849+
// [#30391] `pluralLabel` is the plural OF `label`: an extension that renames
850+
// the object and declares no plural leaves the running plural naming the
851+
// old label, so the object is served as one that declares no
852+
// `pluralLabel`. A plural the tenant authored stays (the D9.2a yield), and
853+
// an extension that repeats the label it extends renames nothing.
854+
if (extension.pluralLabel === undefined && extension.label !== merged.label && !yields('pluralLabel')) {
855+
delete merged.pluralLabel;
856+
}
857+
merged.label = extension.label;
858+
}
847859
if (extension.pluralLabel !== undefined && !yields('pluralLabel')) {
848860
merged.pluralLabel = extension.pluralLabel;
849861
}

‎packages/rest/src/meta-item-read-gate.ts‎

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,7 +3034,23 @@ export async function pairTranslationBundleOf(
30343034
} catch {
30353035
return undefined;
30363036
}
3037-
if (typeof base !== 'string' || base === '') return undefined;
3037+
return pairTranslationBundleOfBase(p, base, memo);
3038+
}
3039+
3040+
/**
3041+
* `getPairTranslationBundle(base)` on `protocol`, read once per base through
3042+
* `memo` — {@link pairTranslationBundleOf}'s second question, also asked by
3043+
* {@link withoutStaleObjectPlural} for the base an object's owner ships in.
3044+
* `undefined` on every uncertainty, {@link pairTranslationBundleOf}'s rule.
3045+
*/
3046+
function pairTranslationBundleOfBase(
3047+
protocol: unknown,
3048+
base: unknown,
3049+
memo?: Map<string, Promise<unknown>>,
3050+
): Promise<unknown> {
3051+
const p: any = protocol;
3052+
if (typeof base !== 'string' || base === '') return Promise.resolve(undefined);
3053+
if (!p || typeof p.getPairTranslationBundle !== 'function') return Promise.resolve(undefined);
30383054
let pending = memo?.get(base);
30393055
if (pending === undefined) {
30403056
pending = Promise.resolve()
@@ -3045,6 +3061,66 @@ export async function pairTranslationBundleOf(
30453061
return pending;
30463062
}
30473063

3064+
/** A non-empty string, or `undefined`. */
3065+
function nonEmptyText(value: unknown): string | undefined {
3066+
return typeof value === 'string' && value.length > 0 ? value : undefined;
3067+
}
3068+
3069+
/**
3070+
* [#30391] `translated` — what the object translator answered for the served
3071+
* object `input` — without a plural the catalog made up for a renamed object.
3072+
*
3073+
* An object extension that renames `label` and declares no `pluralLabel` is
3074+
* served without one (the fold takes the plural of the old label off,
3075+
* `mergeObjectDefinitions` in `@objectstack/objectql`). The translator answers
3076+
* a missing `pluralLabel` with the catalog's `objects.NAME.pluralLabel`, and
3077+
* that entry is the base package's translation of the plural the extension
3078+
* left behind: it names the old label. So where `input` carries no plural, the
3079+
* packaged base declares one, and `input`'s `label` diverged from the packaged
3080+
* one, the catalog's plural is not served. In its place, the ruled order
3081+
* (ruling record 6631250894: a translation in the pair, then the override's
3082+
* own text, then the base's translation): the base's pair's own translation of
3083+
* the plural when it ships one, read through the same translator over the
3084+
* pair's bundle alone; otherwise what `input` carries, which is no plural —
3085+
* what an object that declares no `pluralLabel` is served.
3086+
*
3087+
* An object served WITH a plural (an extension that sets one, a tenant rename
3088+
* that kept one) and an object whose label is the packaged one are served
3089+
* unchanged, as is every object with no packaged base. Returns `translated`
3090+
* itself when nothing changes; the pair is read only when the rule applies.
3091+
*/
3092+
async function withoutStaleObjectPlural(
3093+
protocol: unknown,
3094+
input: Record<string, unknown>,
3095+
translated: Record<string, unknown>,
3096+
packagedBase: unknown,
3097+
translateOptions: Record<string, unknown>,
3098+
memo?: Map<string, Promise<unknown>>,
3099+
): Promise<Record<string, unknown>> {
3100+
if (nonEmptyText(input.pluralLabel) !== undefined) return translated;
3101+
const packaged = packagedBase && typeof packagedBase === 'object' ? packagedBase as Record<string, unknown> : undefined;
3102+
if (!packaged || nonEmptyText(packaged.pluralLabel) === undefined) return translated;
3103+
const label = nonEmptyText(input.label);
3104+
if (label === undefined || label === packaged.label) return translated;
3105+
const pairBundle = await pairTranslationBundleOfBase(protocol, packaged._packageId, memo);
3106+
let pairPlural: string | undefined;
3107+
if (pairBundle !== undefined && typeof input.name === 'string') {
3108+
const { translateMetadataDocument } = await import('@objectstack/spec/system');
3109+
const read = translateMetadataDocument('object', { name: input.name } as any, pairBundle as any, translateOptions as any);
3110+
pairPlural = nonEmptyText((read as Record<string, unknown> | undefined)?.pluralLabel);
3111+
}
3112+
if (pairPlural !== undefined) {
3113+
return translated.pluralLabel === pairPlural ? translated : { ...translated, pluralLabel: pairPlural };
3114+
}
3115+
if (!('pluralLabel' in input)) {
3116+
if (!('pluralLabel' in translated)) return translated;
3117+
const served: Record<string, unknown> = { ...translated };
3118+
delete served.pluralLabel;
3119+
return served;
3120+
}
3121+
return translated.pluralLabel === input.pluralLabel ? translated : { ...translated, pluralLabel: input.pluralLabel };
3122+
}
3123+
30483124
/** How a transport reaches what the list translation reads. */
30493125
export interface MetaListTranslationSources {
30503126
/** This request's i18n service, or `undefined` when the deployment has none. */
@@ -3110,12 +3186,19 @@ export async function translateMetaList(
31103186
// OUTER `{ type, items }`), so every element translates directly (#5563).
31113187
const translated = await Promise.all(arr.map(async (item) => {
31123188
const pairBundle = p === undefined ? undefined : await pairTranslationBundleOf(p, metaType, item, pairBundles);
3189+
const packagedBase = packagedObjectBaseOf(p, metaType, item?.name);
31133190
const out = translateMetadataDocument(metaType, item, bundle, {
31143191
...metaTranslateOptions(i18n, locale),
3115-
packagedBase: packagedObjectBaseOf(p, metaType, item?.name),
3192+
packagedBase,
31163193
...(pairBundle !== undefined ? { pairBundle: pairBundle as any } : {}),
31173194
});
3118-
return metaType === 'view' && item && typeof item === 'object' ? withoutInventedViewLabel(item, out) : out;
3195+
if (!item || typeof item !== 'object') return out;
3196+
if (metaType === 'view') return withoutInventedViewLabel(item, out);
3197+
// [#30391] A renamed object's plural, read once per base for the page.
3198+
if (metaType === 'object') {
3199+
return withoutStaleObjectPlural(p, item, out, packagedBase, metaTranslateOptions(i18n, locale), pairBundles);
3200+
}
3201+
return out;
31193202
}));
31203203
return Array.isArray(raw) ? translated : { ...raw, items: translated };
31213204
}
@@ -3183,7 +3266,15 @@ export async function translateMetaDocument(
31833266
packagedBase,
31843267
...(pairBundle !== undefined ? { pairBundle: pairBundle as any } : {}),
31853268
});
3186-
return metaType === 'view' ? withoutInventedViewLabel(document as Record<string, unknown>, translated) : translated;
3269+
if (metaType === 'view') return withoutInventedViewLabel(document as Record<string, unknown>, translated);
3270+
if (metaType === 'object' && translated && typeof translated === 'object') {
3271+
// [#30391] A renamed object's plural.
3272+
return withoutStaleObjectPlural(
3273+
p, document as Record<string, unknown>, translated as Record<string, unknown>, packagedBase,
3274+
metaTranslateOptions(i18n, locale),
3275+
);
3276+
}
3277+
return translated;
31873278
}
31883279

31893280
/**

0 commit comments

Comments
 (0)