Skip to content
Open
Show file tree
Hide file tree
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
34 changes: 33 additions & 1 deletion packages/drizzle/src/find/createPolymorphicJoinWherePlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,17 @@ export const createPolymorphicJoinWherePlan = ({
const scalarQueryValueSignatures = new Set<string>()

for (const { collection, collectionConfig, table, tableName } of collectionContexts) {
const fieldAtPath = getFieldByPath({
// `getFieldByPath` keeps searching the same fields when a segment has no subfields, so it
// resolves `title.tags` to an unrelated top-level `tags`. Treat such a path as absent.
const fieldAtPath = isPathTraversable({
fields: collectionConfig.flattenedFields,
path: schemaPath,
})
? getFieldByPath({
fields: collectionConfig.flattenedFields,
path: schemaPath,
})
: null

if (schemaPath === 'id') {
const idColumn = table['id'] as Column | undefined
Expand Down Expand Up @@ -240,6 +247,31 @@ const getColumnPath = (schemaPath: string): string =>
.map((segment) => sanitizePathSegment(segment))
.join('_')

/**
* Reports whether every segment before the leaf is a field that can be descended into. A path
* whose ancestor is a leaf field, such as `title.tags`, is not traversable.
*/
const isPathTraversable = ({ fields, path }: { fields: FlattenedField[]; path: string }) => {
const pathSegments = path.split('.')

for (let segmentIndex = 1; segmentIndex < pathSegments.length; segmentIndex++) {
const fieldAtAncestorPath = getFieldByPath({
fields,
path: pathSegments.slice(0, segmentIndex).join('.'),
})

if (
fieldAtAncestorPath &&
!('flattenedFields' in fieldAtAncestorPath.field) &&
!('blocks' in fieldAtAncestorPath.field)
) {
return false
}
}

return true
}

const pathHasSeparateRows = ({ fields, path }: { fields: FlattenedField[]; path: string }) => {
const pathSegments = path.split('.')

Expand Down
4 changes: 4 additions & 0 deletions test/joins/collections/AccessJoinArticles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ export const AccessJoinArticles: CollectionConfig = {
return { 'settings.approved': { exists: false } }
}

if (req.context.useLeafSubpathAccessConstraint) {
return { 'title.tags': { equals: 'available' } }
}

return {
availability: { not_equals: 'missing' },
tags: { equals: 'available' },
Expand Down
17 changes: 17 additions & 0 deletions test/joins/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3435,6 +3435,23 @@ test.suite('Joins Field', { config: './config.ts', resetBetweenTests: false }, (
).rejects.toThrow('The following path cannot be queried: settings.approved.exists')
})

test('should reject a sub-path of a field that cannot be descended into', async ({
payload,
}) => {
const { parent } = await createConstrainedJoinDocuments(payload)

await expect(
payload.findByID({
id: parent.id,
collection: accessJoinParentsSlug,
context: { useLeafSubpathAccessConstraint: true },
depth: 1,
overrideAccess: false,
user,
}),
).rejects.toThrow('The following path cannot be queried: title.tags')
})

test('should preserve substring matching in REST join constraints', async ({
payload,
restClient,
Expand Down
Loading