Skip to content

Commit b5af4d0

Browse files
authored
Makes sure pointer includes are interpolated as get (#2747)
* test for repro #2005 * Adds ability to override CLP op from RestQuery.execute * nits
1 parent 9260e8b commit b5af4d0

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

spec/schemas.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,34 @@ describe('schemas', () => {
15481548
});
15491549
});
15501550

1551+
it('can query with include and CLP (issue #2005)', (done) => {
1552+
setPermissionsOnClass('AnotherObject', {
1553+
get: {"*": true},
1554+
find: {},
1555+
create: {'*': true},
1556+
update: {'*': true},
1557+
delete: {'*': true},
1558+
addField:{'*': true}
1559+
}).then(() => {
1560+
let obj = new Parse.Object('AnObject');
1561+
let anotherObject = new Parse.Object('AnotherObject');
1562+
return obj.save({
1563+
anotherObject
1564+
})
1565+
}).then(() => {
1566+
let query = new Parse.Query('AnObject');
1567+
query.include('anotherObject');
1568+
return query.find();
1569+
}).then((res) => {
1570+
expect(res.length).toBe(1);
1571+
expect(res[0].get('anotherObject')).not.toBeUndefined();
1572+
done();
1573+
}).catch((err) => {
1574+
jfail(err);
1575+
done();
1576+
})
1577+
});
1578+
15511579
it('can add field as master (issue #1257)', (done) => {
15521580
setPermissionsOnClass('AClass', {
15531581
'addField': {}

src/Controllers/DatabaseController.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -721,11 +721,12 @@ DatabaseController.prototype.find = function(className, query, {
721721
acl,
722722
sort = {},
723723
count,
724-
keys
724+
keys,
725+
op
725726
} = {}) {
726727
let isMaster = acl === undefined;
727728
let aclGroup = acl || [];
728-
let op = typeof query.objectId == 'string' && Object.keys(query).length === 1 ? 'get' : 'find';
729+
op = op || (typeof query.objectId == 'string' && Object.keys(query).length === 1 ? 'get' : 'find');
729730
let classExists = true;
730731
return this.loadSchema()
731732
.then(schemaController => {

src/RestQuery.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
113113
// Returns a promise for the response - an object with optional keys
114114
// 'results' and 'count'.
115115
// TODO: consolidate the replaceX functions
116-
RestQuery.prototype.execute = function() {
116+
RestQuery.prototype.execute = function(executeOptions) {
117117
return Promise.resolve().then(() => {
118118
return this.buildRestWhere();
119119
}).then(() => {
120-
return this.runFind();
120+
return this.runFind(executeOptions);
121121
}).then(() => {
122122
return this.runCount();
123123
}).then(() => {
@@ -387,18 +387,22 @@ RestQuery.prototype.replaceDontSelect = function() {
387387

388388
// Returns a promise for whether it was successful.
389389
// Populates this.response with an object that only has 'results'.
390-
RestQuery.prototype.runFind = function() {
390+
RestQuery.prototype.runFind = function(options = {}) {
391391
if (this.findOptions.limit === 0) {
392392
this.response = {results: []};
393393
return Promise.resolve();
394394
}
395+
let findOptions = Object.assign({}, this.findOptions);
395396
if (this.keys) {
396-
this.findOptions.keys = Array.from(this.keys).map((key) => {
397+
findOptions.keys = Array.from(this.keys).map((key) => {
397398
return key.split('.')[0];
398399
});
399400
}
401+
if (options.op) {
402+
findOptions.op = options.op;
403+
}
400404
return this.config.database.find(
401-
this.className, this.restWhere, this.findOptions).then((results) => {
405+
this.className, this.restWhere, findOptions).then((results) => {
402406
if (this.className === '_User') {
403407
for (var result of results) {
404408
delete result.password;
@@ -473,16 +477,15 @@ function includePath(config, auth, response, path, restOptions = {}) {
473477
return response;
474478
}
475479
let pointersHash = {};
476-
var objectIds = {};
477480
for (var pointer of pointers) {
478481
if (!pointer) {
479482
continue;
480483
}
481484
let className = pointer.className;
482485
// only include the good pointers
483486
if (className) {
484-
pointersHash[className] = pointersHash[className] || [];
485-
pointersHash[className].push(pointer.objectId);
487+
pointersHash[className] = pointersHash[className] || new Set();
488+
pointersHash[className].add(pointer.objectId);
486489
}
487490
}
488491

@@ -504,9 +507,9 @@ function includePath(config, auth, response, path, restOptions = {}) {
504507
}
505508

506509
let queryPromises = Object.keys(pointersHash).map((className) => {
507-
var where = {'objectId': {'$in': pointersHash[className]}};
510+
let where = {'objectId': {'$in': Array.from(pointersHash[className])}};
508511
var query = new RestQuery(config, auth, className, where, includeRestOptions);
509-
return query.execute().then((results) => {
512+
return query.execute({op: 'get'}).then((results) => {
510513
results.className = className;
511514
return Promise.resolve(results);
512515
})

0 commit comments

Comments
 (0)