Skip to content

Commit 09dec43

Browse files
authored
fix(db-mongodb): localized arrays inside blocks with versions (#13804)
Fixes #13745
1 parent 898e937 commit 09dec43

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

packages/db-mongodb/src/utilities/transform.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,12 @@ export const transform = ({
460460
data.globalType = globalSlug
461461
}
462462

463-
const sanitize: TraverseFieldsCallback = ({ field, parentPath, ref: incomingRef }) => {
463+
const sanitize: TraverseFieldsCallback = ({
464+
field,
465+
parentIsLocalized,
466+
parentPath,
467+
ref: incomingRef,
468+
}) => {
464469
if (!incomingRef || typeof incomingRef !== 'object') {
465470
return
466471
}

packages/payload/src/utilities/traverseFields.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ export const traverseFields = ({
412412
if (
413413
fieldShouldBeLocalized({
414414
field,
415-
parentIsLocalized: parentIsLocalized ?? field.localized ?? false,
415+
parentIsLocalized: parentIsLocalized ?? false,
416416
})
417417
) {
418418
if (Array.isArray(currentRef)) {

test/versions/collections/Localized.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@ const LocalizedPosts: CollectionConfig = {
1818
type: 'text',
1919
localized: true,
2020
},
21+
{
22+
name: 'blocks',
23+
type: 'blocks',
24+
blocks: [
25+
{
26+
slug: 'block',
27+
fields: [
28+
{
29+
name: 'array',
30+
type: 'array',
31+
localized: true,
32+
fields: [
33+
{
34+
name: 'relationship',
35+
type: 'relationship',
36+
relationTo: 'posts',
37+
localized: true,
38+
},
39+
],
40+
},
41+
],
42+
},
43+
],
44+
},
2145
],
2246
}
2347

test/versions/int.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,37 @@ describe('Versions', () => {
365365
// When creating new version - updatedAt should match version.updatedAt
366366
expect(fromNonVersionsTable.updatedAt).toBe(latestVersionData.version.updatedAt)
367367
})
368+
369+
it('should allow to create with a localized relationships inside a localized array and a block', async () => {
370+
const post = await payload.create({ collection: 'posts', data: {} })
371+
global.d = true
372+
const res = await payload.create({
373+
collection: 'localized-posts',
374+
draft: true,
375+
depth: 0,
376+
data: {
377+
blocks: [
378+
{
379+
blockType: 'block',
380+
array: [
381+
{
382+
relationship: post.id,
383+
},
384+
],
385+
},
386+
],
387+
},
388+
})
389+
expect(res.blocks[0]?.array[0]?.relationship).toEqual(post.id)
390+
const {
391+
docs: [resFromVersions],
392+
} = await payload.findVersions({
393+
collection: 'localized-posts',
394+
where: { parent: { equals: res.id } },
395+
depth: 0,
396+
})
397+
expect(resFromVersions?.version.blocks[0]?.array[0]?.relationship).toEqual(post.id)
398+
})
368399
})
369400

370401
describe('Restore', () => {

test/versions/payload-types.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,19 @@ export interface LocalizedPost {
360360
id: string;
361361
text?: string | null;
362362
description?: string | null;
363+
blocks?:
364+
| {
365+
array?:
366+
| {
367+
relationship?: (string | null) | Post;
368+
id?: string | null;
369+
}[]
370+
| null;
371+
id?: string | null;
372+
blockName?: string | null;
373+
blockType: 'block';
374+
}[]
375+
| null;
363376
updatedAt: string;
364377
createdAt: string;
365378
_status?: ('draft' | 'published') | null;
@@ -968,6 +981,22 @@ export interface ErrorOnUnpublishSelect<T extends boolean = true> {
968981
export interface LocalizedPostsSelect<T extends boolean = true> {
969982
text?: T;
970983
description?: T;
984+
blocks?:
985+
| T
986+
| {
987+
block?:
988+
| T
989+
| {
990+
array?:
991+
| T
992+
| {
993+
relationship?: T;
994+
id?: T;
995+
};
996+
id?: T;
997+
blockName?: T;
998+
};
999+
};
9711000
updatedAt?: T;
9721001
createdAt?: T;
9731002
_status?: T;

0 commit comments

Comments
 (0)