From 12edb1cc4b2675d9b0948fb7f3439f61c6e2015d Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Fri, 21 Apr 2023 21:12:30 -0400 Subject: [PATCH] fix: query localized fields without localization configured --- src/mongoose/buildQuery.ts | 2 +- test/collections-rest/config.ts | 6 ++++++ test/collections-rest/int.spec.ts | 18 ++++++++++++++++++ test/collections-rest/payload-types.ts | 3 +++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/mongoose/buildQuery.ts b/src/mongoose/buildQuery.ts index 9c2f87b9e..37aa862c6 100644 --- a/src/mongoose/buildQuery.ts +++ b/src/mongoose/buildQuery.ts @@ -449,7 +449,7 @@ export class ParamParser { // Skip the next iteration, because it's a locale i += 1; currentPath = `${currentPath}.${nextSegment}`; - } else if ('localized' in matchedField && matchedField.localized) { + } else if (this.localizationConfig && 'localized' in matchedField && matchedField.localized) { currentPath = `${currentPath}.${this.req.locale}`; } diff --git a/test/collections-rest/config.ts b/test/collections-rest/config.ts index 0ece14ea6..ff4e8579e 100644 --- a/test/collections-rest/config.ts +++ b/test/collections-rest/config.ts @@ -75,6 +75,12 @@ export default buildConfig({ name: 'number', type: 'number', }, + { + name: 'fakeLocalization', + type: 'text', + // field is localized even though the config localization is not on + localized: true, + }, // Relationship { name: 'relationField', diff --git a/test/collections-rest/int.spec.ts b/test/collections-rest/int.spec.ts index 54058b259..a198acc1f 100644 --- a/test/collections-rest/int.spec.ts +++ b/test/collections-rest/int.spec.ts @@ -469,6 +469,24 @@ describe('collections-rest', () => { }); }); + describe('Edge cases', () => { + it('should query a localized field without localization configured', async () => { + const test = 'test'; + await createPost({ fakeLocalization: test }); + + const { status, result } = await client.find({ + query: { + fakeLocalization: { + equals: test, + }, + }, + }); + + expect(status).toEqual(200); + expect(result.docs).toHaveLength(1); + }); + }); + describe('Operators', () => { it('equals', async () => { const valueToQuery = 'valueToQuery'; diff --git a/test/collections-rest/payload-types.ts b/test/collections-rest/payload-types.ts index 0a748b2b8..b769fe51e 100644 --- a/test/collections-rest/payload-types.ts +++ b/test/collections-rest/payload-types.ts @@ -23,6 +23,7 @@ export interface Post { title?: string; description?: string; number?: number; + fakeLocalization?: string; relationField?: string | Relation; relationHasManyField?: string[] | Relation[]; relationMultiRelationTo?: @@ -55,6 +56,7 @@ export interface Post { relationTo: 'dummy'; } )[]; + restrictedField?: string; createdAt: string; updatedAt: string; } @@ -66,6 +68,7 @@ export interface Relation { } export interface Dummy { id: string; + title?: string; name?: string; createdAt: string; updatedAt: string;