Skip to content

Fix #3185 #3186

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 2 commits into from
Dec 6, 2016
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
40 changes: 40 additions & 0 deletions spec/ParseQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,46 @@ describe('Parse.Query testing', () => {
});
});

it('select nested keys 2 level without include (issue #3185)', function(done) {
var Foobar = new Parse.Object('Foobar');
var BarBaz = new Parse.Object('Barbaz');
var Bazoo = new Parse.Object('Bazoo');

Bazoo.set('some', 'thing');
Bazoo.set('otherSome', 'value');
Bazoo.save().then(() => {
BarBaz.set('key', 'value');
BarBaz.set('otherKey', 'value');
BarBaz.set('bazoo', Bazoo);
return BarBaz.save();
}).then(() => {
Foobar.set('foo', 'bar');
Foobar.set('fizz', 'buzz');
Foobar.set('barBaz', BarBaz);
return Foobar.save();
}).then(function(savedFoobar){
var foobarQuery = new Parse.Query('Foobar');
foobarQuery.select(['fizz', 'barBaz.key', 'barBaz.bazoo.some']);
return foobarQuery.get(savedFoobar.id);
}).then((foobarObj) => {
equal(foobarObj.get('fizz'), 'buzz');
equal(foobarObj.get('foo'), undefined);
if (foobarObj.has('barBaz')) {
equal(foobarObj.get('barBaz').get('key'), 'value');
equal(foobarObj.get('barBaz').get('otherKey'), undefined);
if (foobarObj.get('barBaz').has('bazoo')) {
equal(foobarObj.get('barBaz').get('bazoo').get('some'), 'thing');
equal(foobarObj.get('barBaz').get('bazoo').get('otherSome'), undefined);
} else {
fail('bazoo should be set');
}
} else {
fail('barBaz should be set');
}
done();
})
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solid test.

it('properly handles nested ors', function(done) {
var objects = [];
while(objects.length != 4) {
Expand Down
66 changes: 46 additions & 20 deletions src/RestQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var SchemaController = require('./Controllers/SchemaController');
var Parse = require('parse/node').Parse;
const triggers = require('./triggers');

const AlwaysSelectedKeys = ['objectId', 'createdAt', 'updatedAt'];
// restOptions can include:
// skip
// limit
Expand Down Expand Up @@ -52,15 +53,36 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
// this.include = [['foo'], ['foo', 'baz'], ['foo', 'bar']]
this.include = [];

// If we have keys, we probably want to force some includes (n-1 level)
// See issue: https://github.com/ParsePlatform/parse-server/issues/3185
if (restOptions.hasOwnProperty('keys')) {
const keysForInclude = restOptions.keys.split(',').filter((key) => {
// At least 2 components
return key.split(".").length > 1;
}).map((key) => {
// Slice the last component (a.b.c -> a.b)
// Otherwise we'll include one level too much.
return key.slice(0, key.lastIndexOf("."));
}).join(',');

// Concat the possibly present include string with the one from the keys
// Dedup / sorting is handle in 'include' case.
if (keysForInclude.length > 0) {
if (!restOptions.include || restOptions.include.length == 0) {
restOptions.include = keysForInclude;
} else {
restOptions.include += "," + keysForInclude;
}
}
}

for (var option in restOptions) {
switch(option) {
case 'keys':
this.keys = new Set(restOptions.keys.split(','));
// Add the default
this.keys.add('objectId');
this.keys.add('createdAt');
this.keys.add('updatedAt');
case 'keys': {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm. { in case, new one on me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const keys = restOptions.keys.split(',').concat(AlwaysSelectedKeys);
this.keys = Array.from(new Set(keys));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

break;
}
case 'count':
this.doCount = true;
break;
Expand All @@ -80,22 +102,26 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
}
this.findOptions.sort = sortMap;
break;
case 'include':
var paths = restOptions.include.split(',');
var pathSet = {};
for (var path of paths) {
// Add all prefixes with a .-split to pathSet
var parts = path.split('.');
for (var len = 1; len <= parts.length; len++) {
pathSet[parts.slice(0, len).join('.')] = true;
}
}
this.include = Object.keys(pathSet).sort((a, b) => {
return a.length - b.length;
}).map((s) => {
case 'include': {
const paths = restOptions.include.split(',');
// Load the existing includes (from keys)
const pathSet = paths.reduce((memo, path) => {
// Split each paths on . (a.b.c -> [a,b,c])
// reduce to create all paths
// ([a,b,c] -> {a: true, 'a.b': true, 'a.b.c': true})
return path.split('.').reduce((memo, path, index, parts) => {
memo[parts.slice(0, index+1).join('.')] = true;
return memo;
}, memo);
}, {});

this.include = Object.keys(pathSet).map((s) => {
return s.split('.');
}).sort((a, b) => {
return a.length - b.length; // Sort by number of components
});
break;
}
case 'redirectClassNameForKey':
this.redirectKey = restOptions.redirectClassNameForKey;
this.redirectClassName = null;
Expand Down Expand Up @@ -421,7 +447,7 @@ RestQuery.prototype.runFind = function(options = {}) {
}
let findOptions = Object.assign({}, this.findOptions);
if (this.keys) {
findOptions.keys = Array.from(this.keys).map((key) => {
findOptions.keys = this.keys.map((key) => {
return key.split('.')[0];
});
}
Expand Down