Skip to content

Fix: Undefined dot notation in matchKeyInQuery #5917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions spec/ParseQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4575,6 +4575,71 @@ describe('Parse.Query testing', () => {
});
});

it('should not throw error with undefined dot notation when using matchesKeyInQuery', async () => {
const group = new Parse.Object('Group', { name: 'Group #1' });
await group.save();

const role1 = new Parse.Object('Role', {
name: 'Role #1',
type: 'x',
belongsTo: group,
});

const role2 = new Parse.Object('Role', {
name: 'Role #2',
type: 'y',
belongsTo: undefined,
});
await Parse.Object.saveAll([role1, role2]);

const rolesOfTypeX = new Parse.Query('Role');
rolesOfTypeX.equalTo('type', 'x');

const groupsWithRoleX = new Parse.Query('Group');
groupsWithRoleX.matchesKeyInQuery(
'objectId',
'belongsTo.objectId',
rolesOfTypeX
);

const results = await groupsWithRoleX.find();
equal(results.length, 1);
equal(results[0].get('name'), group.get('name'));
});

it('should not throw error with undefined dot notation when using doesNotMatchKeyInQuery', async () => {
const group1 = new Parse.Object('Group', { name: 'Group #1' });
const group2 = new Parse.Object('Group', { name: 'Group #2' });
await Parse.Object.saveAll([group1, group2]);

const role1 = new Parse.Object('Role', {
name: 'Role #1',
type: 'x',
belongsTo: group1,
});

const role2 = new Parse.Object('Role', {
name: 'Role #2',
type: 'y',
belongsTo: undefined,
});
await Parse.Object.saveAll([role1, role2]);

const rolesOfTypeX = new Parse.Query('Role');
rolesOfTypeX.equalTo('type', 'x');

const groupsWithRoleX = new Parse.Query('Group');
groupsWithRoleX.doesNotMatchKeyInQuery(
'objectId',
'belongsTo.objectId',
rolesOfTypeX
);

const results = await groupsWithRoleX.find();
equal(results.length, 1);
equal(results[0].get('name'), group2.get('name'));
});

it('withJSON supports geoWithin.centerSphere', done => {
const inbound = new Parse.GeoPoint(1.5, 1.5);
const onbound = new Parse.GeoPoint(10, 10);
Expand Down
2 changes: 1 addition & 1 deletion src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => {
const inPatterns = [];
values.push(fieldName);
baseArray.forEach((listElem, listIndex) => {
if (listElem !== null) {
if (listElem != null) {
values.push(listElem);
inPatterns.push(`$${index + 1 + listIndex}`);
}
Expand Down
12 changes: 10 additions & 2 deletions src/RestQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,18 @@ RestQuery.prototype.replaceNotInQuery = function() {
});
};

// Used to get the deepest object from json using dot notation.
const getDeepestObjectFromKey = (json, key, idx, src) => {
if (key in json) {
return json[key];
}
src.splice(1); // Exit Early
};

const transformSelect = (selectObject, key, objects) => {
var values = [];
for (var result of objects) {
values.push(key.split('.').reduce((o, i) => o[i], result));
values.push(key.split('.').reduce(getDeepestObjectFromKey, result));
}
delete selectObject['$select'];
if (Array.isArray(selectObject['$in'])) {
Expand Down Expand Up @@ -523,7 +531,7 @@ RestQuery.prototype.replaceSelect = function() {
const transformDontSelect = (dontSelectObject, key, objects) => {
var values = [];
for (var result of objects) {
values.push(key.split('.').reduce((o, i) => o[i], result));
values.push(key.split('.').reduce(getDeepestObjectFromKey, result));
}
delete dontSelectObject['$dontSelect'];
if (Array.isArray(dontSelectObject['$nin'])) {
Expand Down