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
This commit is contained in:
Benjamin Wilson Friedman
2017-10-15 21:15:30 -07:00
committed by GitHub
parent 557a2b2827
commit 315d30b426
2 changed files with 42 additions and 2 deletions

View File

@@ -1037,6 +1037,45 @@ describe('Cloud Code', () => {
}) })
}); });
/**
* Checks that incrementing a value to a zero in a beforeSave hook
* does not result in that key being omitted from the response.
*/
it('before save increment does not return undefined', (done) => {
Parse.Cloud.define("cloudIncrementClassFunction", function (req, res) {
const CloudIncrementClass = Parse.Object.extend("CloudIncrementClass");
const obj = new CloudIncrementClass();
obj.id = req.params.objectId;
obj.save().then(
function (savedObj) {
res.success(savedObj);
});
});
Parse.Cloud.beforeSave("CloudIncrementClass", function (req, res) {
const obj = req.object;
if(!req.master) {
obj.increment('points', -10);
obj.increment('num', -9);
}
res.success();
});
const CloudIncrementClass = Parse.Object.extend('CloudIncrementClass');
const obj = new CloudIncrementClass();
obj.set('points', 10);
obj.set('num', 10);
obj.save(null, {useMasterKey: true})
.then(function() {
Parse.Cloud.run('cloudIncrementClassFunction', { objectId: obj.id })
.then(function(savedObj) {
expect(savedObj.get('num')).toEqual(1);
expect(savedObj.get('points')).toEqual(0);
done();
});
});
});
describe('cloud jobs', () => { describe('cloud jobs', () => {
it('should define a job', (done) => { it('should define a job', (done) => {
expect(() => { expect(() => {

View File

@@ -1195,9 +1195,10 @@ RestWrite.prototype._updateResponseWithData = function(response, data) {
const clientSupportsDelete = ClientSDK.supportsForwardDelete(this.clientSDK); const clientSupportsDelete = ClientSDK.supportsForwardDelete(this.clientSDK);
this.storage.fieldsChangedByTrigger.forEach(fieldName => { this.storage.fieldsChangedByTrigger.forEach(fieldName => {
const dataValue = data[fieldName]; const dataValue = data[fieldName];
const responseValue = response[fieldName];
response[fieldName] = responseValue || dataValue; if(!response.hasOwnProperty(fieldName)) {
response[fieldName] = dataValue;
}
// Strips operations from responses // Strips operations from responses
if (response[fieldName] && response[fieldName].__op) { if (response[fieldName] && response[fieldName].__op) {