From deba174f4a4ed54ec33659401f1716b1ceadaec0 Mon Sep 17 00:00:00 2001 From: Alexey Klykov Date: Wed, 18 Nov 2026 17:34:38 +0300 Subject: [PATCH] fix: pass i18n to entityToJSONSchema for locale-keyed descriptions --- src/server/spec/buildDocument.ts | 2 +- src/server/spec/entitySchemas.ts | 13 ++++++++----- tests/unit/index.spec.ts | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/server/spec/buildDocument.ts b/src/server/spec/buildDocument.ts index 4bb9c98..568cea5 100644 --- a/src/server/spec/buildDocument.ts +++ b/src/server/spec/buildDocument.ts @@ -119,7 +119,7 @@ export const buildDocument = async (input: BuildInput): Promise => { for (const block of config.blocks ?? []) { try { - schemas[blockSchemaName(block.slug)] = buildBlockSchema(block, config) + schemas[blockSchemaName(block.slug)] = buildBlockSchema(block, config, ctx.i18n) } catch (error) { logger.warn(`${PLUGIN_NAME}: skipped block "${block.slug}": ${(error as Error).message}`) } diff --git a/src/server/spec/entitySchemas.ts b/src/server/spec/entitySchemas.ts index bb38bb1..5535771 100644 --- a/src/server/spec/entitySchemas.ts +++ b/src/server/spec/entitySchemas.ts @@ -1,4 +1,5 @@ import { getTranslation } from '@payloadcms/translations' +import type { I18n } from '@payloadcms/translations' import { entityToJSONSchema } from 'payload' import type { Block, Field, SanitizedConfig, SchemaVariant } from 'payload' @@ -20,12 +21,14 @@ const toComponentSchema = ({ config, source, idType, + i18n, defs = new Map(), variant, }: { config: SanitizedConfig source: Entity | Block idType: IDType + i18n?: I18n defs?: Map variant?: SchemaVariant }): Schema => { @@ -36,7 +39,7 @@ const toComponentSchema = ({ idType, new Set(), undefined, - undefined, + i18n, undefined, variant, ) @@ -205,8 +208,8 @@ export const buildEntitySchemas = async ({ const source = stripInterfaceName(entity) const defs = new Map() const idType = ctx.defaultIDType - const read = toComponentSchema({ config, source, idType, defs }) - const write = toComponentSchema({ config, source, idType, defs, variant: 'input' }) + const read = toComponentSchema({ config, source, idType, i18n: ctx.i18n, defs }) + const write = toComponentSchema({ config, source, idType, i18n: ctx.i18n, defs, variant: 'input' }) const isAuth = 'auth' in entity && Boolean(entity.auth) for (const schema of [read, write]) { @@ -237,8 +240,8 @@ export const buildEntitySchemas = async ({ return { read, create, update } } -export const buildBlockSchema = (block: Block, config: SanitizedConfig): Schema => { - const schema = toComponentSchema({ config, source: stripInterfaceName(block), idType: 'text' }) +export const buildBlockSchema = (block: Block, config: SanitizedConfig, i18n?: I18n): Schema => { + const schema = toComponentSchema({ config, source: stripInterfaceName(block), idType: 'text', i18n }) const blockType: Schema = { type: 'string', enum: [block.slug] } const properties = { ...schema.properties, blockType } const required = [...new Set(['blockType', ...(schema.required ?? [])])] diff --git a/tests/unit/index.spec.ts b/tests/unit/index.spec.ts index 7f584ce..3d1df7c 100644 --- a/tests/unit/index.spec.ts +++ b/tests/unit/index.spec.ts @@ -1356,6 +1356,20 @@ describe('spec/entitySchemas', () => { expect(update.properties).not.toHaveProperty('lockedLater') expect(update.properties).toHaveProperty('adminOnly') }) + + it('resolves a locale-keyed field description without an `en` key', async () => { + const { buildEntitySchemas } = await import('@/server/spec/entitySchemas.js') + const fields = [{ name: 'title', type: 'text', admin: { description: { de: 'Titel' } } }] as Field[] + const entity = { + slug: 'posts', + fields, + flattenedFields: flattenAllFields({ fields }), + } as unknown as SanitizedCollectionConfig + const config = { collections: [entity], blocks: [] } as unknown as SanitizedConfig + const deCtx = { ...ctx, i18n: { ...ctx.i18n, language: 'de' } as I18n } + const { read } = await buildEntitySchemas({ entity, config, ctx: deCtx }) + expect(read.properties?.title).toMatchObject({ description: 'Titel' }) + }) }) describe('spec/params', () => {