Skip to content

Commit e9afb36

Browse files
authored
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 #11542 additionally for this case.
1 parent 029cac3 commit e9afb36

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

Diff for: packages/db-mongodb/src/updateVersion.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const updateVersion: UpdateVersion = async function updateVersion(
6767
return null
6868
}
6969

70-
transform({ adapter: this, data: doc, fields, operation: 'write' })
70+
transform({ adapter: this, data: doc, fields, operation: 'read' })
7171

7272
return doc
7373
}

Diff for: test/versions/int.spec.ts

+58-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { Payload } from 'payload';
1+
import type { Payload } from 'payload'
22

33
import { schedulePublishHandler } from '@payloadcms/ui/utilities/schedulePublishHandler'
44
import path from 'path'
5-
import { createLocalReq , ValidationError } from 'payload'
5+
import { createLocalReq, ValidationError } from 'payload'
66
import { wait } from 'payload/shared'
77
import { fileURLToPath } from 'url'
88

@@ -689,6 +689,62 @@ describe('Versions', () => {
689689
{ id: doc.id, message: 'The following field is invalid: Title' },
690690
])
691691
})
692+
693+
it('should update with autosave: true', async () => {
694+
// Save a draft
695+
const { id } = await payload.create({
696+
collection: autosaveCollectionSlug,
697+
draft: true,
698+
data: { title: 'my-title', description: 'some-description', _status: 'draft' },
699+
})
700+
701+
// Autosave the same draft, calls db.updateVersion
702+
const updated1 = await payload.update({
703+
collection: autosaveCollectionSlug,
704+
id,
705+
data: {
706+
title: 'new-title',
707+
},
708+
autosave: true,
709+
draft: true,
710+
})
711+
712+
const versionsCount = await payload.countVersions({
713+
collection: autosaveCollectionSlug,
714+
where: {
715+
parent: {
716+
equals: id,
717+
},
718+
},
719+
})
720+
721+
// This should not create a new version
722+
const updated2 = await payload.update({
723+
collection: autosaveCollectionSlug,
724+
id,
725+
data: {
726+
title: 'new-title-2',
727+
},
728+
autosave: true,
729+
draft: true,
730+
})
731+
732+
const versionsCountAfter = await payload.countVersions({
733+
collection: autosaveCollectionSlug,
734+
where: {
735+
parent: {
736+
equals: id,
737+
},
738+
},
739+
})
740+
741+
expect(versionsCount.totalDocs).toBe(versionsCountAfter.totalDocs)
742+
expect(updated1.id).toBe(id)
743+
expect(updated1.title).toBe('new-title')
744+
745+
expect(updated2.id).toBe(id)
746+
expect(updated2.title).toBe('new-title-2')
747+
})
692748
})
693749

694750
describe('Update Many', () => {

0 commit comments

Comments
 (0)