From 37b21b07628e892e85c2cf979d9e2c8af0d291f7 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 1 Nov 2021 17:07:42 -0400 Subject: [PATCH] fix: ensures tquerying by relationship subpaths works --- src/mongoose/buildQuery.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/mongoose/buildQuery.ts b/src/mongoose/buildQuery.ts index a9704f10a6a..af28e4d7572 100644 --- a/src/mongoose/buildQuery.ts +++ b/src/mongoose/buildQuery.ts @@ -1,7 +1,7 @@ /* eslint-disable no-await-in-loop */ /* eslint-disable no-restricted-syntax */ import deepmerge from 'deepmerge'; -import mongoose, { FilterQuery } from 'mongoose'; +import mongoose, { FilterQuery, SchemaType } from 'mongoose'; import { combineMerge } from '../utilities/combineMerge'; import { CollectionModel } from '../collections/config/types'; import { getSchemaTypeOptions } from './getSchemaTypeOptions'; @@ -97,12 +97,12 @@ class ParamParser { if (validOperators.includes(operator)) { const searchParam = await this.buildSearchParam(this.model.schema, relationOrPath, pathOperators[operator], operator); - if ('path' in searchParam) { + if (searchParam && 'path' in searchParam) { result = { ...result, [searchParam.path]: searchParam.value, }; - } else if (typeof searchParam.value === 'object') { + } else if (typeof searchParam?.value === 'object') { result = deepmerge(result, searchParam.value, { arrayMerge: combineMerge }); } @@ -149,9 +149,12 @@ class ParamParser { const { path } = lastIncompletePath; const currentPath = path ? `${path}.${segment}` : segment; - const currentSchemaType = schema.path(currentPath); + const currentSchemaType: SchemaType & { path: string } = schema.path(currentPath); - if (currentSchemaType) { + // If we find a schema type, and it matches the exact current path + // NOTE - not a sub-path. Some schema types like `mixed` will return anything + // nested within. Need to make sure that schema type path matches exactly + if (currentSchemaType && (currentSchemaType.path === currentPath || currentSchemaType.instance === 'Embedded')) { const currentSchemaTypeOptions = getSchemaTypeOptions(currentSchemaType); if (currentSchemaTypeOptions.localized) { @@ -281,7 +284,6 @@ class ParamParser { let overrideQuery = false; let query; - // If there is a ref, this is a relationship or upload field // IDs can be either string, number, or ObjectID // So we need to build an `or` query for all these types