Fixes Issue unsetting in beforeSave doesn't allow object creation (#4610)

* skip unset fields on canAddField

* removed fit

* add null check
This commit is contained in:
Diamond Lewis
2018-03-08 10:31:53 -06:00
committed by GitHub
parent f03e35ff76
commit f0674df424
2 changed files with 47 additions and 2 deletions

View File

@@ -1667,7 +1667,48 @@ describe('schemas', () => {
fail(JSON.stringify(error));
done();
})
})
});
it('unset field in beforeSave should not stop object creation', (done) => {
const hook = {
method: function(req, res) {
if (req.object.get('undesiredField')) {
req.object.unset('undesiredField');
}
return res.success();
}
};
spyOn(hook, 'method').and.callThrough();
Parse.Cloud.beforeSave('AnObject', hook.method);
setPermissionsOnClass('AnObject', {
get: {"*": true},
find: {"*": true},
create: {'*': true},
update: {'*': true},
delete: {'*': true},
addField:{}
}).then(() => {
const obj = new Parse.Object('AnObject');
obj.set('desiredField', 'createMe');
return obj.save(null, {useMasterKey: true});
}).then(() => {
const obj = new Parse.Object('AnObject');
obj.set('desiredField', 'This value should be kept');
obj.set('undesiredField', 'This value should be IGNORED');
return obj.save();
}).then(() => {
const query = new Parse.Query('AnObject');
return query.find();
}).then((results) => {
expect(results.length).toBe(2);
expect(results[0].has('desiredField')).toBe(true);
expect(results[1].has('desiredField')).toBe(true);
expect(results[0].has('undesiredField')).toBe(false);
expect(results[1].has('undesiredField')).toBe(false);
expect(hook.method).toHaveBeenCalled();
done();
});
});
it('gives correct response when deleting a schema with CLPs (regression test #1919)', done => {
new Parse.Object('MyClass').save({ data: 'foo'})

View File

@@ -622,8 +622,12 @@ class DatabaseController {
const fields = Object.keys(object);
const schemaFields = Object.keys(classSchema);
const newKeys = fields.filter((field) => {
// Skip fields that are unset
if (object[field] && object[field].__op && object[field].__op === 'Delete') {
return false;
}
return schemaFields.indexOf(field) < 0;
})
});
if (newKeys.length > 0) {
return schema.validatePermission(className, aclGroup, 'addField');
}