Skip to content

Commit ad02823

Browse files
authored
Fix 477 (#492)
* fix some interactions between select queries and sub objects * fix another issue with selection of nested sub-objects * fix some spacing * Ensure source object is not empty before copying missing data
1 parent 293e2e1 commit ad02823

File tree

2 files changed

+111
-5
lines changed

2 files changed

+111
-5
lines changed

src/ParseQuery.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,19 @@ function handleSelectResult(data: any, select: Array<string>){
6767

6868
pathComponents.forEach((component, index, arr) => {
6969
// add keys if the expected data is missing
70-
if (!obj[component]) {
71-
obj[component] = (index == arr.length-1) ? undefined : {};
70+
if (obj && !obj.hasOwnProperty(component)) {
71+
obj[component] = undefined;
72+
}
73+
if (obj !== undefined) {
74+
obj = obj[component];
7275
}
73-
obj = obj[component];
7476

7577
//add this path component to the server mask so we can fill it in later if needed
7678
if (index < arr.length-1) {
7779
if (!serverMask[component]) {
7880
serverMask[component] = {};
7981
}
82+
serverMask = serverMask[component];
8083
}
8184
});
8285
}
@@ -100,8 +103,10 @@ function handleSelectResult(data: any, select: Array<string>){
100103
}
101104
}
102105
for (var key in mask) {
103-
//traverse into objects as needed
104-
copyMissingDataWithMask(src[key], dest[key], mask[key], true);
106+
if (dest[key] !== undefined && dest[key] !== null && src !== undefined && src !== null) {
107+
//traverse into objects as needed
108+
copyMissingDataWithMask(src[key], dest[key], mask[key], true);
109+
}
105110
}
106111
}
107112

src/__tests__/ParseQuery-test.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,4 +1584,105 @@ describe('ParseQuery', () => {
15841584
});
15851585

15861586
});
1587+
1588+
it('selecting sub-objects does not inject objects when sub-object does not exist', (done) => {
1589+
jest.dontMock("../ParseObject");
1590+
jest.resetModules();
1591+
ParseObject = require('../ParseObject').default;
1592+
CoreManager = require('../CoreManager');
1593+
ParseQuery = require('../ParseQuery').default;
1594+
1595+
ParseObject.enableSingleInstance();
1596+
1597+
var objectToReturn = {
1598+
objectId: 'T01',
1599+
name: 'Name',
1600+
tbd: 'exists',
1601+
className:"Thing",
1602+
createdAt: '2017-01-10T10:00:00Z'
1603+
};
1604+
1605+
CoreManager.setQueryController({
1606+
find(className, params, options) {
1607+
return ParsePromise.as({
1608+
results: [objectToReturn]
1609+
});
1610+
}
1611+
});
1612+
1613+
var q = new ParseQuery("Thing");
1614+
q.select("other", "tbd", "subObject.key1")
1615+
var testObject;
1616+
return q.find().then((results) => {
1617+
testObject = results[0];
1618+
1619+
expect(testObject.get("name")).toBe("Name");
1620+
expect(testObject.has("other")).toBe(false);
1621+
expect(testObject.has("subObject")).toBe(false);
1622+
1623+
}).then(() => {
1624+
done();
1625+
}, (error) => {
1626+
done.fail(error);
1627+
});
1628+
});
1629+
1630+
it('removes missing sub objects from the cached object when they are selected', (done) => {
1631+
jest.dontMock("../ParseObject");
1632+
jest.resetModules();
1633+
ParseObject = require('../ParseObject').default;
1634+
CoreManager = require('../CoreManager');
1635+
ParseQuery = require('../ParseQuery').default;
1636+
1637+
ParseObject.enableSingleInstance();
1638+
1639+
var objectToReturn = {
1640+
objectId: 'T01',
1641+
name: 'Name',
1642+
tbd: 'exists',
1643+
className:"Thing",
1644+
subObject1: {foo:"bar"},
1645+
subObject2: {foo:"bar"},
1646+
subObject3: {foo:"bar"},
1647+
subObject5: {subSubObject:{foo:"foo", bar:"bar"}},
1648+
createdAt: '2017-01-10T10:00:00Z'
1649+
};
1650+
1651+
CoreManager.setQueryController({
1652+
find(className, params, options) {
1653+
return ParsePromise.as({
1654+
results: [objectToReturn]
1655+
});
1656+
}
1657+
});
1658+
1659+
var q = new ParseQuery("Thing");
1660+
var testObject;
1661+
return q.find().then((results) => {
1662+
testObject = results[0];
1663+
1664+
expect(testObject.has("subObject1")).toBe(true);
1665+
expect(testObject.has("subObject2")).toBe(true);
1666+
expect(testObject.has("subObject3")).toBe(true);
1667+
expect(testObject.has("subObject4")).toBe(false);
1668+
1669+
var q2 = new ParseQuery("Thing");
1670+
q.select("name","subObject1", "subObject2.foo", "subObject4.foo", "subObject5.subSubObject.foo");
1671+
objectToReturn = { objectId: 'T01', name:"Name", subObject4: {foo:"bar"}, subObject5: {subSubObject:{}}};
1672+
return q.find();
1673+
}).then((results)=>{
1674+
expect(testObject.has("subObject1")).toBe(false); //selected and not returned
1675+
expect(testObject.has("subObject2")).toBe(false); //selected and not returned
1676+
expect(testObject.has("subObject3")).toBe(true); //not selected, so should still be there
1677+
expect(testObject.has("subObject4")).toBe(true); //selected and just added
1678+
expect(testObject.has("subObject5")).toBe(true);
1679+
expect(testObject.get("subObject5").subSubObject).toBeDefined();
1680+
expect(testObject.get("subObject5").subSubObject.bar).toBeDefined(); //not selected but a sibiling was, so should still be there
1681+
}).then(() => {
1682+
done();
1683+
}, (error) => {
1684+
done.fail(error);
1685+
});
1686+
});
1687+
15871688
});

0 commit comments

Comments
 (0)