Skip to content

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

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 3 commits into from
Mar 8, 2018
Merged
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
43 changes: 42 additions & 1 deletion spec/schemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Expand Down
6 changes: 5 additions & 1 deletion src/Controllers/DatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down