From 7050b5285e65a709a0bdfb68e8403839ef75151f Mon Sep 17 00:00:00 2001 From: James Date: Wed, 24 Nov 2021 18:01:33 -0500 Subject: [PATCH] fix: ensures non-localized relationships with many relationTos can be queried --- demo/collections/RelationshipB.ts | 5 +++++ src/collections/tests/collections.spec.js | 22 +++++++++++++++++++++- src/mongoose/buildQuery.ts | 14 +++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/demo/collections/RelationshipB.ts b/demo/collections/RelationshipB.ts index bb8ab298e4..969565cf13 100644 --- a/demo/collections/RelationshipB.ts +++ b/demo/collections/RelationshipB.ts @@ -40,6 +40,11 @@ const RelationshipB: CollectionConfig = { hasMany: true, relationTo: ['localized-posts', 'previewable-post'], }, + { + name: 'nonLocalizedRelationToMany', + type: 'relationship', + relationTo: ['localized-posts', 'relationship-a'], + }, { name: 'strictAccess', type: 'relationship', diff --git a/src/collections/tests/collections.spec.js b/src/collections/tests/collections.spec.js index a8344be9b7..ecd14c99d8 100644 --- a/src/collections/tests/collections.spec.js +++ b/src/collections/tests/collections.spec.js @@ -432,7 +432,7 @@ describe('Collections - REST', () => { expect(data1.docs).toHaveLength(1); }); - it('should allow querying by a localized nested relationship property with many relationTos', async () => { + it('should allow querying by a localized nested relationship with many relationTos', async () => { const relationshipBTitle = 'lawleifjawelifjew'; const relationshipB = await fetch(`${url}/api/relationship-b?depth=0`, { body: JSON.stringify({ @@ -462,6 +462,26 @@ describe('Collections - REST', () => { expect(data.docs).toHaveLength(1); }); + it('should allow querying by a non-localized relationship with many relationTos', async () => { + const relationshipB = await fetch(`${url}/api/relationship-b?depth=0`, { + body: JSON.stringify({ + title: 'awefjlaiwejfalweiijfaew', + nonLocalizedRelationToMany: { + relationTo: 'localized-posts', + value: localizedPostID, + }, + }), + headers, + method: 'post', + }).then((res) => res.json()); + + expect(relationshipB.doc.id).toBeDefined(); + + const queryRes = await fetch(`${url}/api/relationship-b?where[nonLocalizedRelationToMany.value][equals]=${localizedPostID}`); + const data = await queryRes.json(); + expect(data.docs).toHaveLength(1); + }); + it('should allow querying by a numeric custom ID', async () => { const customID = 1988; diff --git a/src/mongoose/buildQuery.ts b/src/mongoose/buildQuery.ts index c3e1fd7556..cb69b046bb 100644 --- a/src/mongoose/buildQuery.ts +++ b/src/mongoose/buildQuery.ts @@ -150,6 +150,8 @@ class ParamParser { const currentSchemaType = schema.path(currentPath); const currentSchemaPathType = schema.pathType(currentPath); + const upcomingSegment = pathSegments[i + 1]; + if (currentSchemaType && currentSchemaPathType !== 'adhocOrUndefined') { const currentSchemaTypeOptions = getSchemaTypeOptions(currentSchemaType); @@ -162,7 +164,6 @@ class ParamParser { return; } - const upcomingSegment = pathSegments[i + 1]; const upcomingPathWithLocale = `${currentPath}.${this.locale}.${upcomingSegment}`; const upcomingSchemaTypeWithLocale = schema.path(upcomingPathWithLocale); @@ -195,6 +196,17 @@ class ParamParser { } } + const upcomingPath = `${currentPath}.${upcomingSegment}`; + const upcomingSchemaType = schema.path(upcomingPath); + + if (upcomingSchemaType) { + lastIncompletePath.path = upcomingPath; + lastIncompletePath.complete = true; + // Remove the next segment as it has been used here + pathSegments.splice(i + 1, 1); + return; + } + if (operator === 'near') { lastIncompletePath.path = currentPath; }