From 4658189f73e1fab252c839983dc793c385bba27f Mon Sep 17 00:00:00 2001 From: Shamim Shahriyar Date: Thu, 26 Nov 2026 02:16:09 +0600 Subject: [PATCH] fix: handle null operands in relation queries gracefully (#10637) --- spec/DatabaseController.spec.js | 131 ++++++++++++++++++++++++++ src/Controllers/DatabaseController.js | 39 ++++++-- 2 files changed, 161 insertions(+), 9 deletions(-) diff --git a/spec/DatabaseController.spec.js b/spec/DatabaseController.spec.js index cb8c847700..841789f1a7 100644 --- a/spec/DatabaseController.spec.js +++ b/spec/DatabaseController.spec.js @@ -1053,6 +1053,137 @@ describe('DatabaseController', function () { expect(result.acknowledged).toBe(true); }); }); + + describe('reduceInRelation', () => { + const mockAdapter = { + find: (tableName, schema, query) => { + if (query && query.relatedId && query.relatedId.$in) { + if (query.relatedId.$in.includes('child1')) { + return Promise.resolve([{ owningId: 'parent1' }]); + } + } + return Promise.resolve([]); + }, + watch: () => Promise.resolve(), + getAllClasses: () => Promise.resolve([]), + }; + + const mockSchema = { + getExpectedType: (className, key) => { + if (key === 'relationField') { + return { type: 'Relation' }; + } + return null; + }, + }; + + let db; + + beforeEach(() => { + db = new DatabaseController(mockAdapter, {}); + }); + + it('should reduce valid pointer query on relation field', async () => { + const query = { + relationField: { + __type: 'Pointer', + className: 'Child', + objectId: 'child1', + }, + }; + await db.reduceInRelation('Parent', query, mockSchema); + expect(query.relationField).toBeUndefined(); + expect(query.objectId).toEqual({ $in: ['parent1'] }); + }); + + it('should handle $in with null operand gracefully (issue #10637)', async () => { + const query = { + relationField: { + $in: [null], + }, + }; + await db.reduceInRelation('Parent', query, mockSchema); + expect(query.relationField).toBeUndefined(); + expect(query.objectId).toEqual({ $in: [] }); + }); + + it('should handle $in with mixed null and valid pointer operands', async () => { + const query = { + relationField: { + $in: [null, { __type: 'Pointer', className: 'Child', objectId: 'child1' }], + }, + }; + await db.reduceInRelation('Parent', query, mockSchema); + expect(query.relationField).toBeUndefined(); + expect(query.objectId).toEqual({ $in: ['parent1'] }); + }); + + it('should handle $nin with null operand gracefully', async () => { + const query = { + relationField: { + $nin: [null], + }, + }; + await db.reduceInRelation('Parent', query, mockSchema); + expect(query.relationField).toBeUndefined(); + expect(query.objectId).toEqual({ $nin: [] }); + }); + + it('should handle $ne with null gracefully', async () => { + const query = { + relationField: { + $ne: null, + }, + }; + await db.reduceInRelation('Parent', query, mockSchema); + expect(query.relationField).toBeUndefined(); + expect(query.objectId).toEqual({ $nin: [] }); + }); + + it('should handle pointer with null objectId gracefully', async () => { + const query = { + relationField: { + __type: 'Pointer', + className: 'Child', + objectId: null, + }, + }; + await db.reduceInRelation('Parent', query, mockSchema); + expect(query.relationField).toBeUndefined(); + expect(query.objectId).toEqual({ $in: [] }); + }); + + it('should handle pointer without objectId gracefully', async () => { + const query = { + relationField: { + __type: 'Pointer', + className: 'Child', + }, + }; + await db.reduceInRelation('Parent', query, mockSchema); + expect(query.relationField).toBeUndefined(); + expect(query.objectId).toEqual({ $in: [] }); + }); + + it('should handle $or containing null relation constraints', async () => { + const query = { + $or: [ + { relationField: { $in: [null] } }, + { relationField: { __type: 'Pointer', className: 'Child', objectId: 'child1' } }, + ], + }; + await db.reduceInRelation('Parent', query, mockSchema); + expect(query.$or[0].objectId).toEqual({ $in: [] }); + expect(query.$or[1].objectId).toEqual({ $in: ['parent1'] }); + }); + + it('should handle null or non-object query inputs safely', async () => { + const res1 = await db.reduceInRelation('Parent', null, mockSchema); + expect(res1).toBeNull(); + const res2 = await db.reduceInRelation('Parent', 'primitive', mockSchema); + expect(res2).toBe('primitive'); + }); + }); }); function buildCLP(pointerNames) { diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index b1cd1aa79b..b3ebba4904 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -1103,10 +1103,13 @@ class DatabaseController { // equal-to-pointer constraints on relation fields. // Returns a promise that resolves when query is mutated reduceInRelation(className: string, query: any, schema: any): Promise { + if (!query || typeof query !== 'object') { + return Promise.resolve(query); + } // Search for an in-relation or equal-to-relation // Make it sequential for now, not sure of paralleization side effects const promises = []; - if (query['$or']) { + if (Array.isArray(query['$or'])) { const ors = query['$or']; promises.push( ...ors.map((aQuery, index) => { @@ -1116,7 +1119,7 @@ class DatabaseController { }) ); } - if (query['$and']) { + if (Array.isArray(query['$and'])) { const ands = query['$and']; promises.push( ...ands.map((aQuery, index) => { @@ -1127,6 +1130,13 @@ class DatabaseController { ); } + const extractId = (r: any): ?string => { + if (!r || typeof r !== 'object') { + return null; + } + return typeof r.objectId === 'string' ? r.objectId : null; + }; + const otherKeys = Object.keys(query).map(key => { if (key === '$and' || key === '$or') { return; @@ -1138,9 +1148,10 @@ class DatabaseController { let queries: ?(any[]) = null; if ( query[key] && - (query[key]['$in'] || - query[key]['$ne'] || - query[key]['$nin'] || + typeof query[key] === 'object' && + ('$in' in query[key] || + '$ne' in query[key] || + '$nin' in query[key] || query[key].__type == 'Pointer') ) { // Build the list of queries @@ -1148,15 +1159,21 @@ class DatabaseController { let relatedIds; let isNegation = false; if (constraintKey === 'objectId') { - relatedIds = [query[key].objectId]; + const id = extractId(query[key]); + relatedIds = id ? [id] : []; } else if (constraintKey == '$in') { - relatedIds = query[key]['$in'].map(r => r.objectId); + relatedIds = Array.isArray(query[key]['$in']) + ? query[key]['$in'].map(extractId).filter(Boolean) + : []; } else if (constraintKey == '$nin') { isNegation = true; - relatedIds = query[key]['$nin'].map(r => r.objectId); + relatedIds = Array.isArray(query[key]['$nin']) + ? query[key]['$nin'].map(extractId).filter(Boolean) + : []; } else if (constraintKey == '$ne') { isNegation = true; - relatedIds = [query[key]['$ne'].objectId]; + const id = extractId(query[key]['$ne']); + relatedIds = id ? [id] : []; } else { return; } @@ -1165,6 +1182,10 @@ class DatabaseController { relatedIds, }; }); + const validQueries = queries.filter(Boolean); + if (validQueries.length === 0) { + queries = [{ isNegation: false, relatedIds: [] }]; + } } else { queries = [{ isNegation: false, relatedIds: [] }]; }