Skip to content

Commit 89eba56

Browse files
committed
fix: querying by polymorphic join field relationTo with overrideAccess: false
1 parent 750210f commit 89eba56

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

packages/payload/src/database/queryValidation/validateQueryPaths.ts

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Args = {
1313
errors?: { path: string }[]
1414
overrideAccess: boolean
1515
policies?: EntityPolicies
16+
polymorphicJoin?: boolean
1617
req: PayloadRequest
1718
versionFields?: FlattenedField[]
1819
where: Where
@@ -52,6 +53,7 @@ export async function validateQueryPaths({
5253
collections: {},
5354
globals: {},
5455
},
56+
polymorphicJoin,
5557
req,
5658
versionFields,
5759
where,
@@ -77,6 +79,7 @@ export async function validateQueryPaths({
7779
overrideAccess,
7880
path,
7981
policies,
82+
polymorphicJoin,
8083
req,
8184
val,
8285
versionFields,

packages/payload/src/database/queryValidation/validateSearchParams.ts

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Args = {
2121
parentIsLocalized?: boolean
2222
path: string
2323
policies: EntityPolicies
24+
polymorphicJoin?: boolean
2425
req: PayloadRequest
2526
val: unknown
2627
versionFields?: FlattenedField[]
@@ -39,6 +40,7 @@ export async function validateSearchParam({
3940
parentIsLocalized,
4041
path: incomingPath,
4142
policies,
43+
polymorphicJoin,
4244
req,
4345
val,
4446
versionFields,
@@ -102,6 +104,10 @@ export async function validateSearchParam({
102104
errors.push({ path })
103105
}
104106

107+
if (polymorphicJoin && path === 'relationTo') {
108+
return
109+
}
110+
105111
if (!overrideAccess && fieldAffectsData(field)) {
106112
if (collectionSlug) {
107113
if (!policies.collections[collectionSlug]) {
@@ -140,8 +146,10 @@ export async function validateSearchParam({
140146
const segments = fieldPath.split('.')
141147

142148
let fieldAccess
149+
143150
if (versionFields) {
144151
fieldAccess = policies[entityType][entitySlug]
152+
145153
if (segments[0] === 'parent' || segments[0] === 'version') {
146154
segments.shift()
147155
}

packages/payload/src/database/sanitizeJoinQuery.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @ts-strict-ignore
22
import type { SanitizedCollectionConfig, SanitizedJoin } from '../collections/config/types.js'
3+
import type { FlattenedField } from '../fields/config/types.js'
34
import type { JoinQuery, PayloadRequest } from '../types/index.js'
45

56
import executeAccess from '../auth/executeAccess.js'
@@ -67,6 +68,7 @@ const sanitizeJoinFieldQuery = async ({
6768
collectionConfig: joinCollectionConfig,
6869
errors,
6970
overrideAccess,
71+
polymorphicJoin: Array.isArray(join.field.collection),
7072
req,
7173
// incoming where input, but we shouldn't validate generated from the access control.
7274
where: joinQuery.where,

test/joins/config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export default buildConfigWithDefaults({
222222
},
223223
{
224224
slug: 'multiple-collections-parents',
225+
access: { read: () => true },
225226
fields: [
226227
{
227228
type: 'join',
@@ -236,6 +237,7 @@ export default buildConfigWithDefaults({
236237
},
237238
{
238239
slug: 'multiple-collections-1',
240+
access: { read: () => true },
239241
admin: { useAsTitle: 'title' },
240242
fields: [
241243
{
@@ -255,6 +257,7 @@ export default buildConfigWithDefaults({
255257
},
256258
{
257259
slug: 'multiple-collections-2',
260+
access: { read: () => true },
258261
admin: { useAsTitle: 'title' },
259262
fields: [
260263
{

test/joins/int.spec.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ describe('Joins Field', () => {
13891389
expect(parent.children?.docs).toHaveLength(1)
13901390
expect(parent.children.docs[0]?.value.title).toBe('doc-1')
13911391

1392-
// WHERE by _relationTo (join for specific collectionSlug)
1392+
// WHERE by relationTo (join for specific collectionSlug)
13931393
parent = await payload.findByID({
13941394
collection: 'multiple-collections-parents',
13951395
id: parent.id,
@@ -1405,6 +1405,23 @@ describe('Joins Field', () => {
14051405
},
14061406
})
14071407

1408+
// WHERE by relationTo with overrideAccess:false
1409+
parent = await payload.findByID({
1410+
collection: 'multiple-collections-parents',
1411+
id: parent.id,
1412+
overrideAccess: false,
1413+
depth: 1,
1414+
joins: {
1415+
children: {
1416+
where: {
1417+
relationTo: {
1418+
equals: 'multiple-collections-2',
1419+
},
1420+
},
1421+
},
1422+
},
1423+
})
1424+
14081425
expect(parent.children?.docs).toHaveLength(1)
14091426
expect(parent.children.docs[0]?.value.title).toBe('doc-2')
14101427

0 commit comments

Comments
 (0)