fix(beforeSave/afterSave): Return value instead of Parse.Op for nested fields (#7005)
* fix(beforeSave): Return value instead of Parse.Op * afterSave test * Improve Tests * Fixed postgres test by saveArgumentsByValue
This commit is contained in:
@@ -1480,17 +1480,41 @@ describe('Cloud Code', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('beforeSave should not sanitize database', async done => {
|
it('beforeSave should not sanitize database', async done => {
|
||||||
|
const { adapter } = Config.get(Parse.applicationId).database;
|
||||||
|
const spy = spyOn(adapter, 'findOneAndUpdate').and.callThrough();
|
||||||
|
spy.calls.saveArgumentsByValue();
|
||||||
|
|
||||||
let count = 0;
|
let count = 0;
|
||||||
Parse.Cloud.beforeSave('CloudIncrementNested', () => {
|
Parse.Cloud.beforeSave('CloudIncrementNested', req => {
|
||||||
count += 1;
|
count += 1;
|
||||||
|
req.object.set('foo', 'baz');
|
||||||
|
expect(typeof req.object.get('objectField').number).toBe('number');
|
||||||
|
});
|
||||||
|
|
||||||
|
Parse.Cloud.afterSave('CloudIncrementNested', req => {
|
||||||
|
expect(typeof req.object.get('objectField').number).toBe('number');
|
||||||
});
|
});
|
||||||
|
|
||||||
const obj = new Parse.Object('CloudIncrementNested');
|
const obj = new Parse.Object('CloudIncrementNested');
|
||||||
obj.set('objectField', { number: 5 });
|
obj.set('objectField', { number: 5 });
|
||||||
|
obj.set('foo', 'bar');
|
||||||
await obj.save();
|
await obj.save();
|
||||||
|
|
||||||
obj.increment('objectField.number', 10);
|
obj.increment('objectField.number', 10);
|
||||||
await obj.save();
|
await obj.save();
|
||||||
|
|
||||||
|
const [
|
||||||
|
,
|
||||||
|
,
|
||||||
|
,
|
||||||
|
/* className */ /* schema */ /* query */ update,
|
||||||
|
] = adapter.findOneAndUpdate.calls.first().args;
|
||||||
|
expect(update).toEqual({
|
||||||
|
'objectField.number': { __op: 'Increment', amount: 10 },
|
||||||
|
foo: 'baz',
|
||||||
|
updatedAt: obj.updatedAt.toISOString(),
|
||||||
|
});
|
||||||
|
|
||||||
count === 2 ? done() : fail();
|
count === 2 ? done() : fail();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1558,15 +1558,19 @@ RestWrite.prototype.buildUpdatedObject = function (extraData) {
|
|||||||
const updatedObject = triggers.inflate(extraData, this.originalData);
|
const updatedObject = triggers.inflate(extraData, this.originalData);
|
||||||
Object.keys(this.data).reduce(function (data, key) {
|
Object.keys(this.data).reduce(function (data, key) {
|
||||||
if (key.indexOf('.') > 0) {
|
if (key.indexOf('.') > 0) {
|
||||||
// subdocument key with dot notation ('x.y':v => 'x':{'y':v})
|
if (typeof data[key].__op === 'string') {
|
||||||
const splittedKey = key.split('.');
|
updatedObject.set(key, data[key]);
|
||||||
const parentProp = splittedKey[0];
|
} else {
|
||||||
let parentVal = updatedObject.get(parentProp);
|
// subdocument key with dot notation { 'x.y': v } => { 'x': { 'y' : v } })
|
||||||
if (typeof parentVal !== 'object') {
|
const splittedKey = key.split('.');
|
||||||
parentVal = {};
|
const parentProp = splittedKey[0];
|
||||||
|
let parentVal = updatedObject.get(parentProp);
|
||||||
|
if (typeof parentVal !== 'object') {
|
||||||
|
parentVal = {};
|
||||||
|
}
|
||||||
|
parentVal[splittedKey[1]] = data[key];
|
||||||
|
updatedObject.set(parentProp, parentVal);
|
||||||
}
|
}
|
||||||
parentVal[splittedKey[1]] = data[key];
|
|
||||||
updatedObject.set(parentProp, parentVal);
|
|
||||||
delete data[key];
|
delete data[key];
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
|
|||||||
Reference in New Issue
Block a user