From f27dff0ae6900051ba3e554d7a6f5e3ce01df1b4 Mon Sep 17 00:00:00 2001 From: Jeremy Louie Date: Sun, 7 Aug 2016 21:58:32 -0400 Subject: [PATCH] Handle queries with equalTo on objectId and relation conditions (#2472) * Add test for notEqualTo on relation with equalTo on objectId * Properly handles queries with equalTo on objectId and relation conditions This is done by converting shorthand $eq condition to $eq condition instead of clobbering. --- spec/ParseQuery.spec.js | 10 ++++++++++ src/Controllers/DatabaseController.js | 20 ++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/spec/ParseQuery.spec.js b/spec/ParseQuery.spec.js index 29a4b655..60985aa7 100644 --- a/spec/ParseQuery.spec.js +++ b/spec/ParseQuery.spec.js @@ -117,6 +117,16 @@ describe('Parse.Query testing', () => { let cake = results[0]; expect(cake.id).toBe(cake3.id); }); + }).then(function(){ + var query = new Parse.Query(Cake); + // Exclude user1 + query.notEqualTo("liker", user1); + // Only cake1 + query.equalTo("objectId", cake1.id) + // user1 likes cake1 so this should return no results + return query.find().then(function(results){ + equal(results.length, 0); + }); }).then(function(){ done(); }) diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index f286907f..96331322 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -647,11 +647,13 @@ DatabaseController.prototype.addInObjectIdsIds = function(ids = null, query) { idsIntersection = intersect(allIds); } - // Need to make sure we don't clobber existing $lt or other constraints on objectId. - // Clobbering $eq, $in and shorthand $eq (query.objectId === 'string') constraints - // is expected though. - if (!('objectId' in query) || typeof query.objectId === 'string') { + // Need to make sure we don't clobber existing shorthand $eq constraints on objectId. + if (!('objectId' in query)) { query.objectId = {}; + } else if (typeof query.objectId === 'string') { + query.objectId = { + $eq: query.objectId + }; } query.objectId['$in'] = idsIntersection; @@ -670,11 +672,13 @@ DatabaseController.prototype.addNotInObjectIdsIds = function(ids = null, query) idsIntersection = intersect(allIds); } - // Need to make sure we don't clobber existing $lt or other constraints on objectId. - // Clobbering $eq, $in and shorthand $eq (query.objectId === 'string') constraints - // is expected though. - if (!('objectId' in query) || typeof query.objectId === 'string') { + // Need to make sure we don't clobber existing shorthand $eq constraints on objectId. + if (!('objectId' in query)) { query.objectId = {}; + } else if (typeof query.objectId === 'string') { + query.objectId = { + $eq: query.objectId + }; } query.objectId['$nin'] = idsIntersection;