Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions packages/payload/src/collections/operations/restoreVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const restoreVersionOperation = async <
const { docs: versionDocs } = await req.payload.db.findVersions({
collection: collectionConfig.slug,
limit: 1,
locale: locale!,
locale: 'all',
Copy link
Copy Markdown
Contributor Author

@GermanJablo GermanJablo Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

restore logic needs full localized data, not just the caller locale.
Without this, localized required fields can appear missing simply because the active locale has no value.

pagination: false,
req,
where: { id: { equals: id } },
Expand Down Expand Up @@ -109,7 +109,7 @@ export const restoreVersionOperation = async <

const findOneArgs: FindOneArgs = {
collection: collectionConfig.slug,
locale: locale!,
locale: 'all',
req,
where: combineQueries({ id: { equals: parentDocID } }, accessResults),
}
Expand Down Expand Up @@ -143,6 +143,10 @@ export const restoreVersionOperation = async <
})

// originalDoc with hoisted localized data
const validationLocale = payload.config.localization
? payload.config.localization.defaultLocale
: locale!

const originalDoc = await afterRead({
collection: collectionConfig,
context: req.context,
Expand All @@ -151,13 +155,13 @@ export const restoreVersionOperation = async <
draft: draftArg,
fallbackLocale: null,
global: null,
locale: locale!,
locale: validationLocale,
overrideAccess: true,
req,
showHiddenFields: true,
})

// version data with hoisted localized data
// Use locale-hoisted version data for validation while preserving all locales in docWithLocales.
const prevVersionDoc = await afterRead({
collection: collectionConfig,
context: req.context,
Expand All @@ -166,7 +170,7 @@ export const restoreVersionOperation = async <
draft: draftArg,
fallbackLocale: null,
global: null,
locale: locale!,
locale: validationLocale,
overrideAccess: true,
req,
showHiddenFields: true,
Expand All @@ -176,6 +180,11 @@ export const restoreVersionOperation = async <
// beforeValidate - Fields
// /////////////////////////////////////

const reqWithValidationLocale = Object.assign(Object.create(req), req, {
fallbackLocale: null,
locale: validationLocale,
})

let data = await beforeValidate({
id: parentDocID,
collection: collectionConfig,
Expand All @@ -185,7 +194,7 @@ export const restoreVersionOperation = async <
global: null,
operation: 'update',
overrideAccess,
req,
req: reqWithValidationLocale,
})

// /////////////////////////////////////
Expand All @@ -201,7 +210,7 @@ export const restoreVersionOperation = async <
data,
operation: 'update',
originalDoc,
req,
req: reqWithValidationLocale,
})) || data
}
}
Expand All @@ -219,7 +228,7 @@ export const restoreVersionOperation = async <
data,
operation: 'update',
originalDoc,
req,
req: reqWithValidationLocale,
})) || data
}
}
Expand All @@ -238,7 +247,7 @@ export const restoreVersionOperation = async <
global: null,
operation: 'update',
overrideAccess,
req,
req: reqWithValidationLocale,
skipValidation: draftArg && !hasDraftValidationEnabled(collectionConfig),
})

Expand All @@ -261,7 +270,7 @@ export const restoreVersionOperation = async <
id: parentDocID,
collection: collectionConfig.slug,
data: result,
req,
req: reqWithValidationLocale,
select,
})
}
Expand All @@ -278,7 +287,7 @@ export const restoreVersionOperation = async <
draft: draftArg,
operation: 'restoreVersion',
payload,
req,
req: reqWithValidationLocale,
select,
})

Expand Down
42 changes: 42 additions & 0 deletions test/versions/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,48 @@ describe('Versions', () => {
expect(restoredVersion.title).toStrictEqual('v1')
})

it('should restore a published version when required localized fields are empty in a non-default locale', async () => {
const originalPost = await payload.create({
collection: draftCollectionSlug,
data: {
_status: 'published',
description: 'description v1',
title: 'title v1 en',
},
})

await payload.update({
id: originalPost.id,
collection: draftCollectionSlug,
data: {
_status: 'published',
description: 'description v2',
title: 'title v2 en',
},
draft: true,
})

const versions = await payload.findVersions({
collection: draftCollectionSlug,
where: {
parent: {
equals: originalPost.id,
},
},
})

const oldestVersion = versions.docs[versions.docs.length - 1]

const restoredVersion = await payload.restoreVersion({
id: oldestVersion!.id,
collection: draftCollectionSlug,
fallbackLocale: false,
locale: 'de',
})

expect(restoredVersion.id).toStrictEqual(originalPost.id)
})

it('findVersions - pagination should work correctly', async () => {
const post = await payload.create({
collection: draftCollectionSlug,
Expand Down