Skip to content

Commit

Permalink
fix: corrects cases of false positive identification of custom id fie…
Browse files Browse the repository at this point in the history
…lds (#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.
  • Loading branch information
jmikrut authored Nov 16, 2024
1 parent 1393d84 commit 9b00b59
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/payload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,10 +581,13 @@ export class BasePayload {
this.config.collections.forEach((collection) => {
let customIDType = undefined
const findCustomID: TraverseFieldsCallback = ({ field, next }) => {
if (['array', 'blocks'].includes(field.type)) {
next()
return
if (
['array', 'blocks', 'group'].includes(field.type) ||
(field.type === 'tab' && 'name' in field)
) {
return true
}

if (!fieldAffectsData(field)) {
return
}
Expand Down
37 changes: 37 additions & 0 deletions test/database/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,46 @@ export default buildConfigWithDefaults({
],
},
},
{
name: 'title',
type: 'text',
},
],
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',
fields: [
Expand Down
40 changes: 40 additions & 0 deletions test/database/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,46 @@ describe('database', () => {

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', () => {
Expand Down

0 comments on commit 9b00b59

Please sign in to comment.