Skip to content

fix: afterSave trigger removes pointer in Parse object #7913

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

Merged
merged 16 commits into from
May 20, 2022
Merged
26 changes: 26 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,32 @@ describe('Cloud Code', () => {
expect(obj.get('count')).toBe(0);
});

it('Pointer should not be cleared by triggers', async () => {
Parse.Cloud.afterSave('MyObject', () => {});
const foo = await new Parse.Object('Test', { foo: 'bar' }).save();
const obj = await new Parse.Object('MyObject', { foo }).save();
const foo2 = obj.get('foo');
expect(foo2.get('foo')).toBe('bar');
});

it('Can set a pointer in triggers', async () => {
Parse.Cloud.beforeSave('MyObject', () => {});
Parse.Cloud.afterSave(
'MyObject',
async ({ object }) => {
const foo = await new Parse.Object('Test', { foo: 'bar' }).save();
object.set({ foo });
await object.save(null, { useMasterKey: true });
},
{
skipWithMasterKey: true,
}
);
const obj = await new Parse.Object('MyObject').save();
const foo2 = obj.get('foo');
expect(foo2.get('foo')).toBe('bar');
});

it('beforeSave should not sanitize database', async done => {
const { adapter } = Config.get(Parse.applicationId).database;
const spy = spyOn(adapter, 'findOneAndUpdate').and.callThrough();
Expand Down
22 changes: 22 additions & 0 deletions spec/RestQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,26 @@ describe('RestQuery.each', () => {
done();
});
});

it('test afterSave should not affect save response', async () => {
Parse.Cloud.beforeSave('TestObject2', ({ object }) => {
object.set('addedBeforeSave', true);
});
Parse.Cloud.afterSave('TestObject2', ({ object }) => {
object.set('addedAfterSave', true);
object.unset('initialToRemove');
});
const { response } = await rest.create(config, nobody, 'TestObject2', {
initialSave: true,
initialToRemove: true,
});
expect(Object.keys(response).sort()).toEqual([
'addedAfterSave',
'addedBeforeSave',
'createdAt',
'initialToRemove',
'objectId',
'updatedAt',
]);
});
});
20 changes: 19 additions & 1 deletion src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
this.response.response = result;
} else {
this.response.response = this._updateResponseWithData(
(result || updatedObject)._toFullJSON(),
(result || updatedObject).toJSON(),
this.data
);
}
Expand Down Expand Up @@ -1665,6 +1665,24 @@ RestWrite.prototype._updateResponseWithData = function (response, data) {
this.storage.fieldsChangedByTrigger.push(key);
}
}
const defaultMandatoryColumnsInResponse = Object.freeze({
_User: ['username'],
});
const skipKeys = [
'objectId',
'createdAt',
'updatedAt',
...(defaultMandatoryColumnsInResponse[this.className] || []),
];
for (const key in response) {
if (skipKeys.includes(key)) {
continue;
}
const value = response[key];
if (value == null || (value.__type && value.__type === 'Pointer') || data[key] === value) {
delete response[key];
}
}
if (_.isEmpty(this.storage.fieldsChangedByTrigger)) {
return response;
}
Expand Down