fix: deduplicate custom array id fields (#13064)

When adding a custom ID field to an array's config, both the default
field provided by Payload, and the custom ID field, exist in the
resulting config. This can lead to problems when the looking up the
field's config, where either one or the other will be returned.

Fixes #12978
This commit is contained in:
Patrik
2025-07-07 16:06:31 -04:00
committed by GitHub
parent ba660fdea2
commit e6f8ca6fd0
4 changed files with 48 additions and 1 deletions

View File

@@ -200,7 +200,10 @@ export const sanitizeFields = async ({
}
if (field.type === 'array' && field.fields) {
field.fields.push(baseIDField)
const hasCustomID = field.fields.some((f) => 'name' in f && f.name === 'id')
if (!hasCustomID) {
field.fields.push(baseIDField)
}
}
if ((field.type === 'blocks' || field.type === 'array') && field.label) {

View File

@@ -260,6 +260,23 @@ const ArrayFields: CollectionConfig = {
},
],
},
{
name: 'arrayWithCustomID',
type: 'array',
fields: [
{
name: 'id',
type: 'text',
admin: {
disableListFilter: true,
},
},
{
name: 'text',
type: 'text',
},
],
},
],
slug: arrayFieldsSlug,
versions: true,

View File

@@ -2068,6 +2068,21 @@ describe('Fields', () => {
}),
).rejects.toThrow('The following field is invalid: Items 1 > Sub Array 1 > Text In Row')
})
it('should not have multiple instances of the id field in an array with a nested custom id field', () => {
const arraysCollection = payload.config.collections.find(
(collection) => collection.slug === arrayFieldsSlug,
)
const arrayWithNestedCustomIDField = arraysCollection?.fields.find(
(f) => f.name === 'arrayWithCustomID',
)
const idFields = arrayWithNestedCustomIDField?.fields.filter((f) => f.name === 'id')
expect(idFields).toHaveLength(1)
expect(idFields[0].admin?.disableListFilter).toBe(true)
})
})
describe('group', () => {

View File

@@ -355,6 +355,12 @@ export interface ArrayField {
id?: string | null;
}[]
| null;
arrayWithCustomID?:
| {
id?: string | null;
text?: string | null;
}[]
| null;
updatedAt: string;
createdAt: string;
}
@@ -1977,6 +1983,12 @@ export interface ArrayFieldsSelect<T extends boolean = true> {
text?: T;
id?: T;
};
arrayWithCustomID?:
| T
| {
id?: T;
text?: T;
};
updatedAt?: T;
createdAt?: T;
}