Skip to content

Commit 28b086a

Browse files
committed
Pass deeply nested keys correctly to afterSave triggers
Also added a regression test
1 parent 51e0800 commit 28b086a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

spec/ParseAPI.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,32 @@ describe('miscellaneous', function () {
691691
);
692692
});
693693

694+
it('test afterSave with deeply nested keys (#7384)', async () => {
695+
let triggerTime = 0;
696+
Parse.Cloud.afterSave('GameScore', function (req) {
697+
const object = req.object;
698+
expect(object instanceof Parse.Object).toBeTruthy();
699+
if (triggerTime == 0) {
700+
// Create
701+
expect(object.get('a')).toEqual({ b: { c: 0 } });
702+
} else if (triggerTime == 1) {
703+
// Update
704+
expect(object.get('a')).toEqual({ b: { c: 1 } });
705+
} else {
706+
throw new Error();
707+
}
708+
triggerTime++;
709+
});
710+
711+
const obj = new Parse.Object('GameScore');
712+
obj.set('a', { b: { c: 0 } });
713+
await obj.save();
714+
obj.set('a.b.c', 1);
715+
await obj.save();
716+
// Make sure the checking has been triggered
717+
expect(triggerTime).toBe(2);
718+
});
719+
694720
it('test afterSave get original object on update', function (done) {
695721
let triggerTime = 0;
696722
// Register a mock beforeSave hook

src/RestWrite.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,12 @@ RestWrite.prototype.buildUpdatedObject = function (extraData) {
15991599
if (typeof parentVal !== 'object') {
16001600
parentVal = {};
16011601
}
1602-
parentVal[splittedKey[1]] = data[key];
1602+
let curObj = parentVal;
1603+
for (let i = 1; i < splittedKey.length - 1; i++) {
1604+
if (typeof curObj[splittedKey[i]] === 'undefined') curObj[splittedKey[i]] = {};
1605+
curObj = curObj[splittedKey[i]];
1606+
}
1607+
curObj[splittedKey[splittedKey.length - 1]] = data[key];
16031608
updatedObject.set(parentProp, parentVal);
16041609
}
16051610
delete data[key];

0 commit comments

Comments
 (0)