Skip to content

Commit 6479875

Browse files
committed
Merge pull request #1312 from ParsePlatform/flovilmart.includesArray
Fixes bug related to include in queries
2 parents 6540ad1 + ca7d858 commit 6479875

File tree

2 files changed

+99
-24
lines changed

2 files changed

+99
-24
lines changed

spec/ParseQuery.spec.js

+72
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,78 @@ describe('Parse.Query testing', () => {
14221422
});
14231423
});
14241424

1425+
it('properly includes array', (done) => {
1426+
let objects = [];
1427+
let total = 0;
1428+
while(objects.length != 5) {
1429+
let object = new Parse.Object('AnObject');
1430+
object.set('key', objects.length);
1431+
total += objects.length;
1432+
objects.push(object);
1433+
}
1434+
Parse.Object.saveAll(objects).then(() => {
1435+
let object = new Parse.Object("AContainer");
1436+
object.set('objects', objects);
1437+
return object.save();
1438+
}).then(() => {
1439+
let query = new Parse.Query('AContainer');
1440+
query.include('objects');
1441+
return query.find()
1442+
}).then((results) => {
1443+
expect(results.length).toBe(1);
1444+
let res = results[0];
1445+
let objects = res.get('objects');
1446+
expect(objects.length).toBe(5);
1447+
objects.forEach((object) => {
1448+
total -= object.get('key');
1449+
});
1450+
expect(total).toBe(0);
1451+
done()
1452+
}, () => {
1453+
fail('should not fail');
1454+
done();
1455+
})
1456+
});
1457+
1458+
it('properly includes array of mixed objects', (done) => {
1459+
let objects = [];
1460+
let total = 0;
1461+
while(objects.length != 5) {
1462+
let object = new Parse.Object('AnObject');
1463+
object.set('key', objects.length);
1464+
total += objects.length;
1465+
objects.push(object);
1466+
}
1467+
while(objects.length != 10) {
1468+
let object = new Parse.Object('AnotherObject');
1469+
object.set('key', objects.length);
1470+
total += objects.length;
1471+
objects.push(object);
1472+
}
1473+
Parse.Object.saveAll(objects).then(() => {
1474+
let object = new Parse.Object("AContainer");
1475+
object.set('objects', objects);
1476+
return object.save();
1477+
}).then(() => {
1478+
let query = new Parse.Query('AContainer');
1479+
query.include('objects');
1480+
return query.find()
1481+
}).then((results) => {
1482+
expect(results.length).toBe(1);
1483+
let res = results[0];
1484+
let objects = res.get('objects');
1485+
expect(objects.length).toBe(10);
1486+
objects.forEach((object) => {
1487+
total -= object.get('key');
1488+
});
1489+
expect(total).toBe(0);
1490+
done()
1491+
}, (err) => {
1492+
fail('should not fail');
1493+
done();
1494+
})
1495+
})
1496+
14251497
it("result object creation uses current extension", function(done) {
14261498
var ParentObject = Parse.Object.extend({ className: "ParentObject" });
14271499
// Add a foo() method to ChildObject.

src/RestQuery.js

+27-24
Original file line numberDiff line numberDiff line change
@@ -449,39 +449,42 @@ function includePath(config, auth, response, path) {
449449
if (pointers.length == 0) {
450450
return response;
451451
}
452+
let pointersHash = {};
452453
var className = null;
453454
var objectIds = {};
454455
for (var pointer of pointers) {
455-
if (className === null) {
456-
className = pointer.className;
457-
} else {
458-
if (className != pointer.className) {
459-
throw new Parse.Error(Parse.Error.INVALID_JSON,
460-
'inconsistent type data for include');
461-
}
456+
let className = pointer.className;
457+
// only include the good pointers
458+
if (className) {
459+
pointersHash[className] = pointersHash[className] || [];
460+
pointersHash[className].push(pointer.objectId);
462461
}
463-
objectIds[pointer.objectId] = true;
464-
}
465-
if (!className) {
466-
throw new Parse.Error(Parse.Error.INVALID_JSON,
467-
'bad pointers');
468462
}
469463

464+
let queryPromises = Object.keys(pointersHash).map((className) => {
465+
var where = {'objectId': {'$in': pointersHash[className]}};
466+
var query = new RestQuery(config, auth, className, where);
467+
return query.execute().then((results) => {
468+
results.className = className;
469+
return Promise.resolve(results);
470+
})
471+
})
472+
470473
// Get the objects for all these object ids
471-
var where = {'objectId': {'$in': Object.keys(objectIds)}};
472-
var query = new RestQuery(config, auth, className, where);
473-
return query.execute().then((includeResponse) => {
474-
var replace = {};
475-
for (var obj of includeResponse.results) {
476-
obj.__type = 'Object';
477-
obj.className = className;
478-
479-
if(className == "_User"){
480-
delete obj.sessionToken;
474+
return Promise.all(queryPromises).then((responses) => {
475+
var replace = responses.reduce((replace, includeResponse) => {
476+
for (var obj of includeResponse.results) {
477+
obj.__type = 'Object';
478+
obj.className = includeResponse.className;
479+
480+
if(className == "_User"){
481+
delete obj.sessionToken;
482+
}
483+
replace[obj.objectId] = obj;
481484
}
485+
return replace;
486+
}, {})
482487

483-
replace[obj.objectId] = obj;
484-
}
485488
var resp = {
486489
results: replacePointers(response.results, path, replace)
487490
};

0 commit comments

Comments
 (0)