Skip to content

Commit 3f3f5db

Browse files
authored
fix: ensure restoreAsDraft only updates the published doc when draft is false (#14658)
### What? Updates the `restoreVersion` logic to ensure restoring as a draft does not overwrite the currently published data. ### Why? If you have a published doc, then save a draft, you expect to be able to access the published data by `draft: false` and the draft by `draft:true`. When you restore a version as a draft, the same behavior is expected. However, in the `restoreVersion` operation, we are updating the main doc even if you are restoring as a draft. This overwrites the published data, so now you will see draft data returned regardless of querying with `draft: true/false`. ### How? When restoring a version as a draft, we only need to save a new version. The main doc should only be updated when restoring as published. This change checks `(!draftArg)` before running the `updateOne` on the main doc. #### Reported by client.
1 parent a7cf30d commit 3f3f5db

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

packages/payload/src/collections/operations/restoreVersion.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,15 @@ export const restoreVersionOperation = async <
262262
result.updatedAt = new Date().toISOString()
263263
// Ensure status respects restoreAsDraft arg
264264
result._status = draftArg ? 'draft' : result._status
265-
result = await req.payload.db.updateOne({
266-
id: parentDocID,
267-
collection: collectionConfig.slug,
268-
data: result,
269-
req,
270-
select,
271-
})
265+
if (!draftArg) {
266+
result = await req.payload.db.updateOne({
267+
id: parentDocID,
268+
collection: collectionConfig.slug,
269+
data: result,
270+
req,
271+
select,
272+
})
273+
}
272274

273275
// /////////////////////////////////////
274276
// Save restored doc as a new version

test/versions/e2e.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ describe('Versions', () => {
306306

307307
await expect(page.locator('#field-title')).toHaveValue('v2')
308308
await page.goto(`${savedDocURL}/api`)
309+
await page.locator('#field-draft').check()
309310
const values = page.locator('.query-inspector__value')
310311
const count = await values.count()
311312

0 commit comments

Comments
 (0)