Skip to content

Commit ff9b153

Browse files
dplewisdavimacedo
authored andcommitted
Fix: Undefined dot notation in matchKeyInQuery (parse-community#5917)
* Fix: Undefined dot notation in matchKeyInQuery * fix test * fix postgres test * improve tests * FINAL test
1 parent 700d2fa commit ff9b153

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

spec/ParseQuery.spec.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4575,6 +4575,71 @@ describe('Parse.Query testing', () => {
45754575
});
45764576
});
45774577

4578+
it('should not throw error with undefined dot notation when using matchesKeyInQuery', async () => {
4579+
const group = new Parse.Object('Group', { name: 'Group #1' });
4580+
await group.save();
4581+
4582+
const role1 = new Parse.Object('Role', {
4583+
name: 'Role #1',
4584+
type: 'x',
4585+
belongsTo: group,
4586+
});
4587+
4588+
const role2 = new Parse.Object('Role', {
4589+
name: 'Role #2',
4590+
type: 'y',
4591+
belongsTo: undefined,
4592+
});
4593+
await Parse.Object.saveAll([role1, role2]);
4594+
4595+
const rolesOfTypeX = new Parse.Query('Role');
4596+
rolesOfTypeX.equalTo('type', 'x');
4597+
4598+
const groupsWithRoleX = new Parse.Query('Group');
4599+
groupsWithRoleX.matchesKeyInQuery(
4600+
'objectId',
4601+
'belongsTo.objectId',
4602+
rolesOfTypeX
4603+
);
4604+
4605+
const results = await groupsWithRoleX.find();
4606+
equal(results.length, 1);
4607+
equal(results[0].get('name'), group.get('name'));
4608+
});
4609+
4610+
it('should not throw error with undefined dot notation when using doesNotMatchKeyInQuery', async () => {
4611+
const group1 = new Parse.Object('Group', { name: 'Group #1' });
4612+
const group2 = new Parse.Object('Group', { name: 'Group #2' });
4613+
await Parse.Object.saveAll([group1, group2]);
4614+
4615+
const role1 = new Parse.Object('Role', {
4616+
name: 'Role #1',
4617+
type: 'x',
4618+
belongsTo: group1,
4619+
});
4620+
4621+
const role2 = new Parse.Object('Role', {
4622+
name: 'Role #2',
4623+
type: 'y',
4624+
belongsTo: undefined,
4625+
});
4626+
await Parse.Object.saveAll([role1, role2]);
4627+
4628+
const rolesOfTypeX = new Parse.Query('Role');
4629+
rolesOfTypeX.equalTo('type', 'x');
4630+
4631+
const groupsWithRoleX = new Parse.Query('Group');
4632+
groupsWithRoleX.doesNotMatchKeyInQuery(
4633+
'objectId',
4634+
'belongsTo.objectId',
4635+
rolesOfTypeX
4636+
);
4637+
4638+
const results = await groupsWithRoleX.find();
4639+
equal(results.length, 1);
4640+
equal(results[0].get('name'), group2.get('name'));
4641+
});
4642+
45784643
it('withJSON supports geoWithin.centerSphere', done => {
45794644
const inbound = new Parse.GeoPoint(1.5, 1.5);
45804645
const onbound = new Parse.GeoPoint(10, 10);

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => {
442442
const inPatterns = [];
443443
values.push(fieldName);
444444
baseArray.forEach((listElem, listIndex) => {
445-
if (listElem !== null) {
445+
if (listElem != null) {
446446
values.push(listElem);
447447
inPatterns.push(`$${index + 1 + listIndex}`);
448448
}

src/RestQuery.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,18 @@ RestQuery.prototype.replaceNotInQuery = function() {
455455
});
456456
};
457457

458+
// Used to get the deepest object from json using dot notation.
459+
const getDeepestObjectFromKey = (json, key, idx, src) => {
460+
if (key in json) {
461+
return json[key];
462+
}
463+
src.splice(1); // Exit Early
464+
};
465+
458466
const transformSelect = (selectObject, key, objects) => {
459467
var values = [];
460468
for (var result of objects) {
461-
values.push(key.split('.').reduce((o, i) => o[i], result));
469+
values.push(key.split('.').reduce(getDeepestObjectFromKey, result));
462470
}
463471
delete selectObject['$select'];
464472
if (Array.isArray(selectObject['$in'])) {
@@ -523,7 +531,7 @@ RestQuery.prototype.replaceSelect = function() {
523531
const transformDontSelect = (dontSelectObject, key, objects) => {
524532
var values = [];
525533
for (var result of objects) {
526-
values.push(key.split('.').reduce((o, i) => o[i], result));
534+
values.push(key.split('.').reduce(getDeepestObjectFromKey, result));
527535
}
528536
delete dontSelectObject['$dontSelect'];
529537
if (Array.isArray(dontSelectObject['$nin'])) {

0 commit comments

Comments
 (0)