fix(db-mongodb): properly sanitize updateVersion read result (#11589)

Previously, `db.updateVersion` had a mistake with using `transform({
operation: 'write' })` instead of `transform({ operation: 'read' })`
which led to improper DB data sanitization (like ObjectID -> string,
Date -> string) when calling `payload.update` with `autosave: true` when
some other autosave draft already exists. This fixes
https://github.com/payloadcms/payload/issues/11542 additionally for this
case.
This commit is contained in:
Sasha
2025-03-07 19:14:02 +02:00
committed by GitHub
parent 029cac3cd3
commit e9afb367b5
2 changed files with 59 additions and 3 deletions

View File

@@ -67,7 +67,7 @@ export const updateVersion: UpdateVersion = async function updateVersion(
return null return null
} }
transform({ adapter: this, data: doc, fields, operation: 'write' }) transform({ adapter: this, data: doc, fields, operation: 'read' })
return doc return doc
} }

View File

@@ -1,4 +1,4 @@
import type { Payload } from 'payload'; import type { Payload } from 'payload'
import { schedulePublishHandler } from '@payloadcms/ui/utilities/schedulePublishHandler' import { schedulePublishHandler } from '@payloadcms/ui/utilities/schedulePublishHandler'
import path from 'path' import path from 'path'
@@ -689,6 +689,62 @@ describe('Versions', () => {
{ id: doc.id, message: 'The following field is invalid: Title' }, { id: doc.id, message: 'The following field is invalid: Title' },
]) ])
}) })
it('should update with autosave: true', async () => {
// Save a draft
const { id } = await payload.create({
collection: autosaveCollectionSlug,
draft: true,
data: { title: 'my-title', description: 'some-description', _status: 'draft' },
})
// Autosave the same draft, calls db.updateVersion
const updated1 = await payload.update({
collection: autosaveCollectionSlug,
id,
data: {
title: 'new-title',
},
autosave: true,
draft: true,
})
const versionsCount = await payload.countVersions({
collection: autosaveCollectionSlug,
where: {
parent: {
equals: id,
},
},
})
// This should not create a new version
const updated2 = await payload.update({
collection: autosaveCollectionSlug,
id,
data: {
title: 'new-title-2',
},
autosave: true,
draft: true,
})
const versionsCountAfter = await payload.countVersions({
collection: autosaveCollectionSlug,
where: {
parent: {
equals: id,
},
},
})
expect(versionsCount.totalDocs).toBe(versionsCountAfter.totalDocs)
expect(updated1.id).toBe(id)
expect(updated1.title).toBe('new-title')
expect(updated2.id).toBe(id)
expect(updated2.title).toBe('new-title-2')
})
}) })
describe('Update Many', () => { describe('Update Many', () => {