fix: localized fields within block references were not handled properly if any parent is localized (#11207)

The `localized` properly was not stripped out of referenced block fields, if any parent was localized. For normal fields, this is done in sanitizeConfig. As the same referenced block config can be used in both a localized and non-localized config, we are not able to strip it out inside sanitizeConfig by modifying the block config.

Instead, this PR had to bring back tedious logic to handle it everywhere the `field.localized` property is accessed. For backwards-compatibility, we need to keep the existing sanitizeConfig logic. In 4.0, we should remove it to benefit from better test coverage of runtime field.localized handling - for now, this is done for our test suite using the `PAYLOAD_DO_NOT_SANITIZE_LOCALIZED_PROPERTY` flag.
This commit is contained in:
Alessio Gravili
2025-02-17 12:50:32 -07:00
committed by GitHub
parent 749962a1db
commit e6fea1d132
105 changed files with 2036 additions and 1347 deletions

View File

@@ -83,7 +83,7 @@ export interface Config {
'payload-migrations': PayloadMigrationsSelect<false> | PayloadMigrationsSelect<true>;
};
db: {
defaultIDType: number;
defaultIDType: string;
};
globals: {
menu: Menu;
@@ -123,7 +123,7 @@ export interface UserAuthOperations {
* via the `definition` "posts".
*/
export interface Post {
id: number;
id: string;
title?: string | null;
content?: {
root: {
@@ -148,7 +148,7 @@ export interface Post {
* via the `definition` "media".
*/
export interface Media {
id: number;
id: string;
updatedAt: string;
createdAt: string;
url?: string | null;
@@ -192,7 +192,7 @@ export interface Media {
* via the `definition` "users".
*/
export interface User {
id: number;
id: string;
updatedAt: string;
createdAt: string;
email: string;
@@ -209,24 +209,24 @@ export interface User {
* via the `definition` "payload-locked-documents".
*/
export interface PayloadLockedDocument {
id: number;
id: string;
document?:
| ({
relationTo: 'posts';
value: number | Post;
value: string | Post;
} | null)
| ({
relationTo: 'media';
value: number | Media;
value: string | Media;
} | null)
| ({
relationTo: 'users';
value: number | User;
value: string | User;
} | null);
globalSlug?: string | null;
user: {
relationTo: 'users';
value: number | User;
value: string | User;
};
updatedAt: string;
createdAt: string;
@@ -236,10 +236,10 @@ export interface PayloadLockedDocument {
* via the `definition` "payload-preferences".
*/
export interface PayloadPreference {
id: number;
id: string;
user: {
relationTo: 'users';
value: number | User;
value: string | User;
};
key?: string | null;
value?:
@@ -259,7 +259,7 @@ export interface PayloadPreference {
* via the `definition` "payload-migrations".
*/
export interface PayloadMigration {
id: number;
id: string;
name?: string | null;
batch?: number | null;
updatedAt: string;
@@ -378,7 +378,7 @@ export interface PayloadMigrationsSelect<T extends boolean = true> {
* via the `definition` "menu".
*/
export interface Menu {
id: number;
id: string;
globalText?: string | null;
updatedAt?: string | null;
createdAt?: string | null;

View File

@@ -16,6 +16,9 @@ import { runInit } from './runInit.js'
import { child } from './safelyRunScript.js'
import { createTestHooks } from './testHooks.js'
// @todo remove in 4.0 - will behave like this by default in 4.0
process.env.PAYLOAD_DO_NOT_SANITIZE_LOCALIZED_PROPERTY = 'true'
const prod = process.argv.includes('--prod')
if (prod) {
process.argv = process.argv.filter((arg) => arg !== '--prod')

View File

@@ -8,7 +8,7 @@ import playwright from 'eslint-plugin-playwright'
export const testEslintConfig = [
...rootEslintConfig,
{
ignores: [...defaultESLintIgnores, '**/payload-types.ts'],
ignores: [...defaultESLintIgnores, '**/payload-types.ts', 'jest.setup.js'],
},
{
languageOptions: {

View File

@@ -126,6 +126,26 @@ export const baseConfig: Partial<Config> = {
},
],
},
{
slug: 'localizedTextReference',
fields: [
{
name: 'text',
type: 'text',
localized: true,
},
],
},
{
slug: 'localizedTextReference2',
fields: [
{
name: 'text',
type: 'text',
localized: true,
},
],
},
],
custom: {
client: {

View File

@@ -409,6 +409,21 @@ const BlockFields: CollectionConfig = {
blockReferences: ['ConfigBlockTest'],
blocks: [],
},
{
name: 'localizedReferencesLocalizedBlock',
type: 'blocks',
blockReferences: ['localizedTextReference'],
blocks: [],
localized: true,
},
{
name: 'localizedReferences',
type: 'blocks',
// Needs to be a separate block - otherwise this will break in postgres. This is unrelated to block references
// and an issue with all blocks.
blockReferences: ['localizedTextReference2'],
blocks: [],
},
],
}

View File

@@ -47,4 +47,16 @@ export const blocksDoc: Partial<BlockField> = {
blockType: 'blockWithMinRows',
},
],
localizedReferencesLocalizedBlock: [
{
blockType: 'localizedTextReference',
text: 'localized text',
},
],
localizedReferences: [
{
blockType: 'localizedTextReference2',
text: 'localized text',
},
],
}

View File

@@ -2,11 +2,11 @@ import type { MongooseAdapter } from '@payloadcms/db-mongodb'
import type { IndexDirection, IndexOptions } from 'mongoose'
import path from 'path'
import { type PaginatedDocs, type Payload, reload, ValidationError } from 'payload'
import { type PaginatedDocs, type Payload, reload } from 'payload'
import { fileURLToPath } from 'url'
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
import type { GroupField, RichTextField } from './payload-types.js'
import type { BlockField, GroupField, RichTextField } from './payload-types.js'
import { devUser } from '../credentials.js'
import { initPayloadInt } from '../helpers/initPayloadInt.js'
@@ -2562,6 +2562,32 @@ describe('Fields', () => {
expect(result.blocksWithLocalizedArray[0].array[0].text).toEqual('localized')
})
it('ensure localized field within block reference is saved correctly', async () => {
const blockFields = await payload.find({
collection: 'block-fields',
locale: 'all',
})
const doc: BlockField = blockFields.docs[0] as BlockField
expect(doc?.localizedReferences?.[0]?.blockType).toEqual('localizedTextReference2')
expect(doc?.localizedReferences?.[0]?.text).toEqual({ en: 'localized text' })
})
it('ensure localized property is stripped from localized field within localized block reference', async () => {
const blockFields = await payload.find({
collection: 'block-fields',
locale: 'all',
})
const doc: any = blockFields.docs[0]
expect(doc?.localizedReferencesLocalizedBlock?.en?.[0]?.blockType).toEqual(
'localizedTextReference',
)
expect(doc?.localizedReferencesLocalizedBlock?.en?.[0]?.text).toEqual('localized text')
})
})
describe('collapsible', () => {

File diff suppressed because it is too large Load Diff

View File

@@ -16,6 +16,7 @@ export const autoDedupeBlocksPlugin =
traverseFields({
config,
leavesFirst: true,
parentIsLocalized: false,
isTopLevel: true,
fields: [
...(config.collections?.length

View File

@@ -16,6 +16,8 @@ process.env.PAYLOAD_PUBLIC_CLOUD_STORAGE_ADAPTER = 's3'
process.env.NODE_OPTIONS = '--no-deprecation'
process.env.PAYLOAD_CI_DEPENDENCY_CHECKER = 'true'
// @todo remove in 4.0 - will behave like this by default in 4.0
process.env.PAYLOAD_DO_NOT_SANITIZE_LOCALIZED_PROPERTY = 'true'
// Mock createTestAccount to prevent calling external services
jest.spyOn(nodemailer, 'createTestAccount').mockImplementation(() => {

View File

@@ -213,6 +213,7 @@ describe('Joins Field', () => {
})
expect(categoryWithPosts.arrayPosts.docs).toBeDefined()
expect(categoryWithPosts.arrayPosts.docs).toHaveLength(10)
})
it('should populate joins with localized array relationships', async () => {

View File

@@ -276,7 +276,7 @@ describe('Localization', () => {
expect(localized.title.es).toEqual(spanishTitle)
})
it('REST all locales with all', async () => {
it('rest all locales with all', async () => {
const response = await restClient.GET(`/${collection}/${localizedPost.id}`, {
query: {
locale: 'all',
@@ -290,7 +290,7 @@ describe('Localization', () => {
expect(localized.title.es).toEqual(spanishTitle)
})
it('REST all locales with asterisk', async () => {
it('rest all locales with asterisk', async () => {
const response = await restClient.GET(`/${collection}/${localizedPost.id}`, {
query: {
locale: '*',
@@ -1869,14 +1869,16 @@ describe('Localization', () => {
})
})
// Nested localized fields do no longer have their localized property stripped in
// this monorepo, as this is handled at runtime.
describe('nested localized field sanitization', () => {
it('should sanitize nested localized fields', () => {
it('ensure nested localized fields keep localized property in monorepo', () => {
const collection = payload.collections['localized-within-localized'].config
expect(collection.fields[0].tabs[0].fields[0].localized).toBeUndefined()
expect(collection.fields[1].fields[0].localized).toBeUndefined()
expect(collection.fields[2].blocks[0].fields[0].localized).toBeUndefined()
expect(collection.fields[3].fields[0].localized).toBeUndefined()
expect(collection.fields[0].tabs[0].fields[0].localized).toBeDefined()
expect(collection.fields[1].fields[0].localized).toBeDefined()
expect(collection.fields[2].blocks[0].fields[0].localized).toBeDefined()
expect(collection.fields[3].fields[0].localized).toBeDefined()
})
})

View File

@@ -27,6 +27,8 @@ export default withBundleAnalyzer(
env: {
PAYLOAD_CORE_DEV: 'true',
ROOT_DIR: path.resolve(dirname),
// @todo remove in 4.0 - will behave like this by default in 4.0
PAYLOAD_DO_NOT_SANITIZE_LOCALIZED_PROPERTY: 'true',
},
async redirects() {
return [

View File

@@ -9,6 +9,9 @@ import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(__filename)
// @todo remove in 4.0 - will behave like this by default in 4.0
process.env.PAYLOAD_DO_NOT_SANITIZE_LOCALIZED_PROPERTY = 'true'
shelljs.env.DISABLE_LOGGING = 'true'
const prod = process.argv.includes('--prod')