fix(db-mongodb): localized arrays inside blocks with versions (#13804)

Fixes https://github.com/payloadcms/payload/issues/13745
This commit is contained in:
Sasha
2025-09-15 21:31:25 +03:00
committed by GitHub
parent 898e937c63
commit 09dec43c15
5 changed files with 91 additions and 2 deletions

View File

@@ -460,7 +460,12 @@ export const transform = ({
data.globalType = globalSlug
}
const sanitize: TraverseFieldsCallback = ({ field, parentPath, ref: incomingRef }) => {
const sanitize: TraverseFieldsCallback = ({
field,
parentIsLocalized,
parentPath,
ref: incomingRef,
}) => {
if (!incomingRef || typeof incomingRef !== 'object') {
return
}

View File

@@ -412,7 +412,7 @@ export const traverseFields = ({
if (
fieldShouldBeLocalized({
field,
parentIsLocalized: parentIsLocalized ?? field.localized ?? false,
parentIsLocalized: parentIsLocalized ?? false,
})
) {
if (Array.isArray(currentRef)) {

View File

@@ -18,6 +18,30 @@ const LocalizedPosts: CollectionConfig = {
type: 'text',
localized: true,
},
{
name: 'blocks',
type: 'blocks',
blocks: [
{
slug: 'block',
fields: [
{
name: 'array',
type: 'array',
localized: true,
fields: [
{
name: 'relationship',
type: 'relationship',
relationTo: 'posts',
localized: true,
},
],
},
],
},
],
},
],
}

View File

@@ -365,6 +365,37 @@ describe('Versions', () => {
// When creating new version - updatedAt should match version.updatedAt
expect(fromNonVersionsTable.updatedAt).toBe(latestVersionData.version.updatedAt)
})
it('should allow to create with a localized relationships inside a localized array and a block', async () => {
const post = await payload.create({ collection: 'posts', data: {} })
global.d = true
const res = await payload.create({
collection: 'localized-posts',
draft: true,
depth: 0,
data: {
blocks: [
{
blockType: 'block',
array: [
{
relationship: post.id,
},
],
},
],
},
})
expect(res.blocks[0]?.array[0]?.relationship).toEqual(post.id)
const {
docs: [resFromVersions],
} = await payload.findVersions({
collection: 'localized-posts',
where: { parent: { equals: res.id } },
depth: 0,
})
expect(resFromVersions?.version.blocks[0]?.array[0]?.relationship).toEqual(post.id)
})
})
describe('Restore', () => {

View File

@@ -360,6 +360,19 @@ export interface LocalizedPost {
id: string;
text?: string | null;
description?: string | null;
blocks?:
| {
array?:
| {
relationship?: (string | null) | Post;
id?: string | null;
}[]
| null;
id?: string | null;
blockName?: string | null;
blockType: 'block';
}[]
| null;
updatedAt: string;
createdAt: string;
_status?: ('draft' | 'published') | null;
@@ -968,6 +981,22 @@ export interface ErrorOnUnpublishSelect<T extends boolean = true> {
export interface LocalizedPostsSelect<T extends boolean = true> {
text?: T;
description?: T;
blocks?:
| T
| {
block?:
| T
| {
array?:
| T
| {
relationship?: T;
id?: T;
};
id?: T;
blockName?: T;
};
};
updatedAt?: T;
createdAt?: T;
_status?: T;