Skip to content

fix: Fixed issue where deeply nested keys were having incorrect values #9720

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

Open
wants to merge 5 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
52 changes: 52 additions & 0 deletions spec/ParseObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2171,4 +2171,56 @@ describe('Parse.Object testing', () => {
expect(objectAgain.get(value.field)).toEqual(value.value);
}
});

it_only_db('mongo')('test afterSave with deeply nested keys (#7384) (#9720)', async () => {
let triggerTime = 0;
Parse.Cloud.afterSave('GameScore', function (req) {
const object = req.object;
expect(object instanceof Parse.Object).toBeTruthy();
triggerTime++;
if (triggerTime == 1) {
// Create
expect(object.get('a')).toEqual({ b: 0, c: { d: 1 } });
expect(object.get('e')).toEqual(2);
} else if (triggerTime == 2) {
// Update, increment
expect(object.get('a')).toEqual({ b: 10, c: { d: 12 } });
expect(object.get('e')).toEqual(14);
} else if (triggerTime == 3) {
// Update, set
expect(object.get('a')).toEqual({ b: 100, c: { d: 200 } });
expect(object.get('e')).toEqual(300);
} else if (triggerTime == 4) {
// Update, unset on a.c.d
expect(object.get('a')).toEqual({ b: 100, c: {} });
expect(object.get('e')).toEqual(300);
} else if (triggerTime == 5) {
// Update, unset on a.b
expect(object.get('a')).toEqual({ c: {} });
expect(object.get('e')).toEqual(300);
} else {
throw new Error();
}

});

const obj = new Parse.Object('GameScore');
obj.set('a', { b: 0, c: { d: 1 } });
obj.set('e', 2);
await obj.save();
obj.increment('a.b', 10);
obj.increment('a.c.d', 11);
obj.increment('e', 12);
await obj.save();
obj.set('a.b', 100);
obj.set('a.c.d', 200);
obj.set('e', 300);
await obj.save();
obj.unset('a.c.d');
await obj.save();
obj.unset('a.b');
await obj.save();
// Make sure the checking has been triggered
expect(triggerTime).toBe(5);
});
});
16 changes: 1 addition & 15 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -1751,21 +1751,7 @@ RestWrite.prototype.buildParseObjects = function () {
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') {
if (!readOnlyAttributes.includes(key)) {
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]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line replaces a bunch of code. Why was that code not needed, can it just be removed? Looking at this check for example:

if (typeof data[key].__op === 'string') {
        if (!readOnlyAttributes.includes(key)) {
          updatedObject.set(key, data[key]);
        }
      }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the parse js sdk handles it as per @dplewis comment here #7385 (comment)

This pr is just a duplicate for #7385 since its in draft mode.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, maybe @dplewis wants to chime in here an approve this since he already looked into this.

delete data[key];
}
return data;
Expand Down
Loading