Skip to content

Pass deeply nested keys correctly to afterSave triggers #7385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: alpha
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions spec/ParseAPI.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,32 @@ describe('miscellaneous', function () {
);
});

it('test afterSave with deeply nested keys (#7384)', async () => {
let triggerTime = 0;
Parse.Cloud.afterSave('GameScore', function (req) {
const object = req.object;
expect(object instanceof Parse.Object).toBeTruthy();
if (triggerTime == 0) {
// Create
expect(object.get('a')).toEqual({ b: { c: 0 } });
} else if (triggerTime == 1) {
// Update
expect(object.get('a')).toEqual({ b: { c: 1 } });
} else {
throw new Error();
}
triggerTime++;
});

const obj = new Parse.Object('GameScore');
obj.set('a', { b: { c: 0 } });
await obj.save();
obj.set('a.b.c', 1);
await obj.save();
// Make sure the checking has been triggered
expect(triggerTime).toBe(2);
});

it('test afterSave get original object on update', function (done) {
let triggerTime = 0;
// Register a mock beforeSave hook
Expand Down
14 changes: 1 addition & 13 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -1589,19 +1589,7 @@ RestWrite.prototype.buildUpdatedObject = function (extraData) {
const updatedObject = triggers.inflate(extraData, this.originalData);
Object.keys(this.data).reduce(function (data, key) {
if (key.indexOf('.') > 0) {
if (typeof data[key].__op === 'string') {
updatedObject.set(key, data[key]);
} else {
// subdocument key with dot notation { 'x.y': v } => { 'x': { 'y' : v } })
const splittedKey = key.split('.');
const parentProp = splittedKey[0];
let parentVal = updatedObject.get(parentProp);
if (typeof parentVal !== 'object') {
parentVal = {};
}
parentVal[splittedKey[1]] = data[key];
updatedObject.set(parentProp, parentVal);
}
updatedObject.set(key, data[key]);
delete data[key];
}
return data;
Expand Down