Skip to content

Commit e8aa1ad

Browse files
authored
Fix null pointer includes (#2657)
* Adds failing test for #2189 * Improves support for null values in includes * nit
1 parent fc576cb commit e8aa1ad

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

spec/RestQuery.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,36 @@ describe('rest query', () => {
235235
done();
236236
});
237237
});
238+
239+
it('makes sure null pointers are handed correctly #2189', done => {
240+
let object = new Parse.Object('AnObject');
241+
let anotherObject = new Parse.Object('AnotherObject');
242+
anotherObject.save().then(() => {
243+
object.set('values', [null, null, anotherObject]);
244+
return object.save();
245+
}).then(() => {
246+
let query = new Parse.Query('AnObject');
247+
query.include('values');
248+
return query.first();
249+
}).then((result) => {
250+
let values = result.get('values');
251+
expect(values.length).toBe(3);
252+
let anotherObjectFound = false;
253+
let nullCounts = 0;
254+
for(let value of values) {
255+
if (value === null) {
256+
nullCounts++;
257+
} else if (value instanceof Parse.Object) {
258+
anotherObjectFound = true;
259+
}
260+
}
261+
expect(nullCounts).toBe(2);
262+
expect(anotherObjectFound).toBeTruthy();
263+
done();
264+
}, (err) => {
265+
console.error(err);
266+
fail(err);
267+
done();
268+
});
269+
});
238270
});

src/RestQuery.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@ function includePath(config, auth, response, path) {
480480
let pointersHash = {};
481481
var objectIds = {};
482482
for (var pointer of pointers) {
483+
if (!pointer) {
484+
continue;
485+
}
483486
let className = pointer.className;
484487
// only include the good pointers
485488
if (className) {
@@ -542,7 +545,7 @@ function findPointers(object, path) {
542545
}
543546

544547
if (path.length == 0) {
545-
if (object.__type == 'Pointer') {
548+
if (object === null || object.__type == 'Pointer') {
546549
return [object];
547550
}
548551
return [];
@@ -564,15 +567,15 @@ function findPointers(object, path) {
564567
function replacePointers(object, path, replace) {
565568
if (object instanceof Array) {
566569
return object.map((obj) => replacePointers(obj, path, replace))
567-
.filter((obj) => obj != null && obj != undefined);
570+
.filter((obj) => typeof obj !== 'undefined');
568571
}
569572

570573
if (typeof object !== 'object') {
571574
return object;
572575
}
573576

574577
if (path.length === 0) {
575-
if (object.__type === 'Pointer') {
578+
if (object && object.__type === 'Pointer') {
576579
return replace[object.objectId];
577580
}
578581
return object;

0 commit comments

Comments
 (0)