Skip to content

Commit e6f8ca6

Browse files
authored
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
1 parent ba660fd commit e6f8ca6

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

packages/payload/src/fields/config/sanitize.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ export const sanitizeFields = async ({
200200
}
201201

202202
if (field.type === 'array' && field.fields) {
203-
field.fields.push(baseIDField)
203+
const hasCustomID = field.fields.some((f) => 'name' in f && f.name === 'id')
204+
if (!hasCustomID) {
205+
field.fields.push(baseIDField)
206+
}
204207
}
205208

206209
if ((field.type === 'blocks' || field.type === 'array') && field.label) {

test/fields/collections/Array/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,23 @@ const ArrayFields: CollectionConfig = {
260260
},
261261
],
262262
},
263+
{
264+
name: 'arrayWithCustomID',
265+
type: 'array',
266+
fields: [
267+
{
268+
name: 'id',
269+
type: 'text',
270+
admin: {
271+
disableListFilter: true,
272+
},
273+
},
274+
{
275+
name: 'text',
276+
type: 'text',
277+
},
278+
],
279+
},
263280
],
264281
slug: arrayFieldsSlug,
265282
versions: true,

test/fields/int.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,21 @@ describe('Fields', () => {
20682068
}),
20692069
).rejects.toThrow('The following field is invalid: Items 1 > Sub Array 1 > Text In Row')
20702070
})
2071+
2072+
it('should not have multiple instances of the id field in an array with a nested custom id field', () => {
2073+
const arraysCollection = payload.config.collections.find(
2074+
(collection) => collection.slug === arrayFieldsSlug,
2075+
)
2076+
2077+
const arrayWithNestedCustomIDField = arraysCollection?.fields.find(
2078+
(f) => f.name === 'arrayWithCustomID',
2079+
)
2080+
2081+
const idFields = arrayWithNestedCustomIDField?.fields.filter((f) => f.name === 'id')
2082+
2083+
expect(idFields).toHaveLength(1)
2084+
expect(idFields[0].admin?.disableListFilter).toBe(true)
2085+
})
20712086
})
20722087

20732088
describe('group', () => {

test/fields/payload-types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,12 @@ export interface ArrayField {
355355
id?: string | null;
356356
}[]
357357
| null;
358+
arrayWithCustomID?:
359+
| {
360+
id?: string | null;
361+
text?: string | null;
362+
}[]
363+
| null;
358364
updatedAt: string;
359365
createdAt: string;
360366
}
@@ -1977,6 +1983,12 @@ export interface ArrayFieldsSelect<T extends boolean = true> {
19771983
text?: T;
19781984
id?: T;
19791985
};
1986+
arrayWithCustomID?:
1987+
| T
1988+
| {
1989+
id?: T;
1990+
text?: T;
1991+
};
19801992
updatedAt?: T;
19811993
createdAt?: T;
19821994
}

0 commit comments

Comments
 (0)