fix: corrects cases of false positive identification of custom id fields (#9245)

This PR fixes cases where you may have a field called `id` within a
group or a named tab, which would have incorrectly been treated as a
custom ID field for the collection.

However, custom IDs need to be defined at the root level - and now
Payload only respects custom IDs defined at the root level.
This commit is contained in:
James Mikrut
2024-11-16 11:29:48 -05:00
committed by GitHub
parent 1393d84bca
commit 9b00b59df0
3 changed files with 83 additions and 3 deletions

View File

@@ -581,10 +581,13 @@ export class BasePayload {
this.config.collections.forEach((collection) => { this.config.collections.forEach((collection) => {
let customIDType = undefined let customIDType = undefined
const findCustomID: TraverseFieldsCallback = ({ field, next }) => { const findCustomID: TraverseFieldsCallback = ({ field, next }) => {
if (['array', 'blocks'].includes(field.type)) { if (
next() ['array', 'blocks', 'group'].includes(field.type) ||
return (field.type === 'tab' && 'name' in field)
) {
return true
} }
if (!fieldAffectsData(field)) { if (!fieldAffectsData(field)) {
return return
} }

View File

@@ -383,9 +383,46 @@ export default buildConfigWithDefaults({
], ],
}, },
}, },
{
name: 'title',
type: 'text',
},
], ],
versions: { drafts: true }, versions: { drafts: true },
}, },
{
slug: 'fake-custom-ids',
fields: [
{
name: 'title',
type: 'text',
},
{
name: 'group',
type: 'group',
fields: [
{
name: 'id',
type: 'text',
},
],
},
{
type: 'tabs',
tabs: [
{
name: 'myTab',
fields: [
{
name: 'id',
type: 'text',
},
],
},
],
},
],
},
{ {
slug: 'relationships-migration', slug: 'relationships-migration',
fields: [ fields: [

View File

@@ -90,6 +90,46 @@ describe('database', () => {
expect(doc.id).toBeDefined() expect(doc.id).toBeDefined()
}) })
it('should not create duplicate versions with custom id type', async () => {
const doc = await payload.create({
collection: 'custom-ids',
data: {
title: 'hey',
},
})
await payload.update({
collection: 'custom-ids',
id: doc.id,
data: {},
})
await payload.update({
collection: 'custom-ids',
id: doc.id,
data: {},
})
const versionsQuery = await payload.db.findVersions({
collection: 'custom-ids',
req: {} as PayloadRequest,
where: {
'version.title': {
equals: 'hey',
},
latest: {
equals: true,
},
},
})
expect(versionsQuery.totalDocs).toStrictEqual(1)
})
it('should not accidentally treat nested id fields as custom id', () => {
expect(payload.collections['fake-custom-ids'].customIDType).toBeUndefined()
})
}) })
describe('timestamps', () => { describe('timestamps', () => {