Skip to content

Commit 315d30b

Browse files
authored
Fix for beforeSave with increment causing key to be Dropped (#4259)
* Fixes an issue where a beforeSave hook could cause a numeric val to be dropped in response. * Use hasOwnProperty to check instead * Remove redundant set
1 parent 557a2b2 commit 315d30b

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

spec/CloudCode.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,45 @@ describe('Cloud Code', () => {
10371037
})
10381038
});
10391039

1040+
/**
1041+
* Checks that incrementing a value to a zero in a beforeSave hook
1042+
* does not result in that key being omitted from the response.
1043+
*/
1044+
it('before save increment does not return undefined', (done) => {
1045+
Parse.Cloud.define("cloudIncrementClassFunction", function (req, res) {
1046+
const CloudIncrementClass = Parse.Object.extend("CloudIncrementClass");
1047+
const obj = new CloudIncrementClass();
1048+
obj.id = req.params.objectId;
1049+
obj.save().then(
1050+
function (savedObj) {
1051+
res.success(savedObj);
1052+
});
1053+
});
1054+
1055+
Parse.Cloud.beforeSave("CloudIncrementClass", function (req, res) {
1056+
const obj = req.object;
1057+
if(!req.master) {
1058+
obj.increment('points', -10);
1059+
obj.increment('num', -9);
1060+
}
1061+
res.success();
1062+
});
1063+
1064+
const CloudIncrementClass = Parse.Object.extend('CloudIncrementClass');
1065+
const obj = new CloudIncrementClass();
1066+
obj.set('points', 10);
1067+
obj.set('num', 10);
1068+
obj.save(null, {useMasterKey: true})
1069+
.then(function() {
1070+
Parse.Cloud.run('cloudIncrementClassFunction', { objectId: obj.id })
1071+
.then(function(savedObj) {
1072+
expect(savedObj.get('num')).toEqual(1);
1073+
expect(savedObj.get('points')).toEqual(0);
1074+
done();
1075+
});
1076+
});
1077+
});
1078+
10401079
describe('cloud jobs', () => {
10411080
it('should define a job', (done) => {
10421081
expect(() => {

src/RestWrite.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,9 +1195,10 @@ RestWrite.prototype._updateResponseWithData = function(response, data) {
11951195
const clientSupportsDelete = ClientSDK.supportsForwardDelete(this.clientSDK);
11961196
this.storage.fieldsChangedByTrigger.forEach(fieldName => {
11971197
const dataValue = data[fieldName];
1198-
const responseValue = response[fieldName];
11991198

1200-
response[fieldName] = responseValue || dataValue;
1199+
if(!response.hasOwnProperty(fieldName)) {
1200+
response[fieldName] = dataValue;
1201+
}
12011202

12021203
// Strips operations from responses
12031204
if (response[fieldName] && response[fieldName].__op) {

0 commit comments

Comments
 (0)