Skip to content

fix: querying by polymorphic join field relationTo with overrideAccess: false #11999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ type Args = {
errors?: { path: string }[]
overrideAccess: boolean
policies?: EntityPolicies
polymorphicJoin?: boolean
req: PayloadRequest
versionFields?: FlattenedField[]
where: Where
@@ -52,6 +53,7 @@ export async function validateQueryPaths({
collections: {},
globals: {},
},
polymorphicJoin,
req,
versionFields,
where,
@@ -77,6 +79,7 @@ export async function validateQueryPaths({
overrideAccess,
path,
policies,
polymorphicJoin,
req,
val,
versionFields,
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ type Args = {
parentIsLocalized?: boolean
path: string
policies: EntityPolicies
polymorphicJoin?: boolean
req: PayloadRequest
val: unknown
versionFields?: FlattenedField[]
@@ -39,6 +40,7 @@ export async function validateSearchParam({
parentIsLocalized,
path: incomingPath,
policies,
polymorphicJoin,
req,
val,
versionFields,
@@ -102,6 +104,10 @@ export async function validateSearchParam({
errors.push({ path })
}

if (polymorphicJoin && path === 'relationTo') {
return
}

if (!overrideAccess && fieldAffectsData(field)) {
if (collectionSlug) {
if (!policies.collections[collectionSlug]) {
@@ -140,8 +146,10 @@ export async function validateSearchParam({
const segments = fieldPath.split('.')

let fieldAccess

if (versionFields) {
fieldAccess = policies[entityType][entitySlug]

if (segments[0] === 'parent' || segments[0] === 'version') {
segments.shift()
}
2 changes: 2 additions & 0 deletions packages/payload/src/database/sanitizeJoinQuery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-strict-ignore
import type { SanitizedCollectionConfig, SanitizedJoin } from '../collections/config/types.js'
import type { FlattenedField } from '../fields/config/types.js'
import type { JoinQuery, PayloadRequest } from '../types/index.js'

import executeAccess from '../auth/executeAccess.js'
@@ -67,6 +68,7 @@ const sanitizeJoinFieldQuery = async ({
collectionConfig: joinCollectionConfig,
errors,
overrideAccess,
polymorphicJoin: Array.isArray(join.field.collection),
req,
// incoming where input, but we shouldn't validate generated from the access control.
where: joinQuery.where,
3 changes: 3 additions & 0 deletions test/joins/config.ts
Original file line number Diff line number Diff line change
@@ -222,6 +222,7 @@ export default buildConfigWithDefaults({
},
{
slug: 'multiple-collections-parents',
access: { read: () => true },
fields: [
{
type: 'join',
@@ -236,6 +237,7 @@ export default buildConfigWithDefaults({
},
{
slug: 'multiple-collections-1',
access: { read: () => true },
admin: { useAsTitle: 'title' },
fields: [
{
@@ -255,6 +257,7 @@ export default buildConfigWithDefaults({
},
{
slug: 'multiple-collections-2',
access: { read: () => true },
admin: { useAsTitle: 'title' },
fields: [
{
19 changes: 18 additions & 1 deletion test/joins/int.spec.ts
Original file line number Diff line number Diff line change
@@ -1389,7 +1389,7 @@ describe('Joins Field', () => {
expect(parent.children?.docs).toHaveLength(1)
expect(parent.children.docs[0]?.value.title).toBe('doc-1')

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

// WHERE by relationTo with overrideAccess:false
parent = await payload.findByID({
collection: 'multiple-collections-parents',
id: parent.id,
overrideAccess: false,
depth: 1,
joins: {
children: {
where: {
relationTo: {
equals: 'multiple-collections-2',
},
},
},
},
})

expect(parent.children?.docs).toHaveLength(1)
expect(parent.children.docs[0]?.value.title).toBe('doc-2')