Skip to content

Commit 526e535

Browse files
authored
fix: ensure custom IDs are returned to the result when select query exists (#11400)
Previously, behavior with custom IDs and `select` query was incorrect. By default, the `id` field is guaranteed to be selected, even if it doesn't exist in the `select` query, this wasn't true for custom IDs.
1 parent e4712a8 commit 526e535

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

packages/payload/src/fields/hooks/afterRead/promise.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export const promise = async ({
123123
}
124124

125125
// Strip unselected fields
126-
if (fieldAffectsData(field) && select && selectMode) {
126+
if (fieldAffectsData(field) && select && selectMode && path !== 'id') {
127127
if (selectMode === 'include') {
128128
if (!select[field.name]) {
129129
delete siblingDoc[field.name]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { CollectionConfig } from 'payload'
2+
3+
export const CustomID: CollectionConfig = {
4+
slug: 'custom-ids',
5+
fields: [
6+
{
7+
name: 'id',
8+
type: 'number',
9+
},
10+
{
11+
name: 'text',
12+
type: 'text',
13+
},
14+
],
15+
}

test/select/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'path'
44

55
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'
66
import { devUser } from '../credentials.js'
7+
import { CustomID } from './collections/CustomID/index.js'
78
import { DeepPostsCollection } from './collections/DeepPosts/index.js'
89
import { LocalizedPostsCollection } from './collections/LocalizedPosts/index.js'
910
import { Pages } from './collections/Pages/index.js'
@@ -34,6 +35,7 @@ export default buildConfigWithDefaults({
3435
slug: 'rels',
3536
fields: [],
3637
},
38+
CustomID,
3739
],
3840
globals: [
3941
{

test/select/int.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { randomUUID } from 'crypto'
12
import path from 'path'
23
import { deepCopyObject, type Payload } from 'payload'
34
import { assert } from 'ts-essentials'
@@ -75,6 +76,21 @@ describe('Select', () => {
7576
})
7677
})
7778

79+
it('customID - should select only id as default', async () => {
80+
const { id } = await createCustomID()
81+
82+
const res = await payload.findByID({
83+
collection: 'custom-ids',
84+
id,
85+
select: {},
86+
depth: 0,
87+
})
88+
89+
expect(res).toStrictEqual({
90+
id,
91+
})
92+
})
93+
7894
it('should select only number', async () => {
7995
const res = await payload.findByID({
8096
collection: 'posts',
@@ -91,6 +107,24 @@ describe('Select', () => {
91107
})
92108
})
93109

110+
it('customID - should select only text', async () => {
111+
const { id, text } = await createCustomID()
112+
113+
const res = await payload.findByID({
114+
collection: 'custom-ids',
115+
id,
116+
select: {
117+
text: true,
118+
},
119+
depth: 0,
120+
})
121+
122+
expect(res).toStrictEqual({
123+
id,
124+
text,
125+
})
126+
})
127+
94128
it('should select only select', async () => {
95129
const res = await payload.findByID({
96130
collection: 'posts',
@@ -446,6 +480,25 @@ describe('Select', () => {
446480
expect(res).toStrictEqual(expected)
447481
})
448482

483+
it('customID - should exclude text', async () => {
484+
const { id, createdAt, updatedAt } = await createCustomID()
485+
486+
const res = await payload.findByID({
487+
collection: 'custom-ids',
488+
id,
489+
select: {
490+
text: false,
491+
},
492+
depth: 0,
493+
})
494+
495+
expect(res).toStrictEqual({
496+
id,
497+
createdAt,
498+
updatedAt,
499+
})
500+
})
501+
449502
it('should exclude number', async () => {
450503
const res = await payload.findByID({
451504
collection: 'posts',
@@ -2428,3 +2481,9 @@ function createVersionedPost() {
24282481
function createPoint() {
24292482
return payload.create({ collection: 'points', data: { text: 'some', point: [10, 20] } })
24302483
}
2484+
2485+
let id = 1
2486+
2487+
function createCustomID() {
2488+
return payload.create({ collection: 'custom-ids', data: { id: id++, text: randomUUID() } })
2489+
}

test/select/payload-types.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export interface Config {
7474
points: Point;
7575
upload: Upload;
7676
rels: Rel;
77+
'custom-ids': CustomId;
7778
users: User;
7879
'payload-locked-documents': PayloadLockedDocument;
7980
'payload-preferences': PayloadPreference;
@@ -89,6 +90,7 @@ export interface Config {
8990
points: PointsSelect<false> | PointsSelect<true>;
9091
upload: UploadSelect<false> | UploadSelect<true>;
9192
rels: RelsSelect<false> | RelsSelect<true>;
93+
'custom-ids': CustomIdsSelect<false> | CustomIdsSelect<true>;
9294
users: UsersSelect<false> | UsersSelect<true>;
9395
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
9496
'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
@@ -443,6 +445,16 @@ export interface Point {
443445
updatedAt: string;
444446
createdAt: string;
445447
}
448+
/**
449+
* This interface was referenced by `Config`'s JSON-Schema
450+
* via the `definition` "custom-ids".
451+
*/
452+
export interface CustomId {
453+
id: number;
454+
text?: string | null;
455+
updatedAt: string;
456+
createdAt: string;
457+
}
446458
/**
447459
* This interface was referenced by `Config`'s JSON-Schema
448460
* via the `definition` "users".
@@ -499,6 +511,10 @@ export interface PayloadLockedDocument {
499511
relationTo: 'rels';
500512
value: string | Rel;
501513
} | null)
514+
| ({
515+
relationTo: 'custom-ids';
516+
value: number | CustomId;
517+
} | null)
502518
| ({
503519
relationTo: 'users';
504520
value: string | User;
@@ -844,6 +860,16 @@ export interface RelsSelect<T extends boolean = true> {
844860
updatedAt?: T;
845861
createdAt?: T;
846862
}
863+
/**
864+
* This interface was referenced by `Config`'s JSON-Schema
865+
* via the `definition` "custom-ids_select".
866+
*/
867+
export interface CustomIdsSelect<T extends boolean = true> {
868+
id?: T;
869+
text?: T;
870+
updatedAt?: T;
871+
createdAt?: T;
872+
}
847873
/**
848874
* This interface was referenced by `Config`'s JSON-Schema
849875
* via the `definition` "users_select".

0 commit comments

Comments
 (0)