fix context for cascade-saving and saving existing object (#6735)
* added test cases * fixed unparsed context when updating object * fixed context inheritance for cascade-saved objects * upgraded parse dependecy to 2.14.0 * rebuild * removed superfluous comments * undo lint changes
This commit is contained in:
3649
package-lock.json
generated
3649
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -46,7 +46,7 @@
|
||||
"lru-cache": "5.1.1",
|
||||
"mime": "2.4.6",
|
||||
"mongodb": "3.5.8",
|
||||
"parse": "2.13.0",
|
||||
"parse": "2.14.0",
|
||||
"pg-promise": "10.5.6",
|
||||
"pluralize": "^8.0.0",
|
||||
"redis": "3.0.2",
|
||||
|
||||
@@ -2912,16 +2912,60 @@ describe('afterLogin hook', () => {
|
||||
done();
|
||||
});
|
||||
|
||||
it('should have access to context as save argument', async () => {
|
||||
// Declare triggers
|
||||
it('should have access to context when saving a new object', async () => {
|
||||
Parse.Cloud.beforeSave('TestObject', (req) => {
|
||||
expect(req.context.a).toEqual('a');
|
||||
});
|
||||
Parse.Cloud.afterSave('TestObject', (req) => {
|
||||
expect(req.context.a).toEqual('a');
|
||||
});
|
||||
// Save object
|
||||
const obj = new TestObject();
|
||||
await obj.save(null, { context: { a: 'a' } });
|
||||
});
|
||||
|
||||
it('should have access to context when saving an existing object', async () => {
|
||||
const obj = new TestObject();
|
||||
await obj.save(null);
|
||||
Parse.Cloud.beforeSave('TestObject', (req) => {
|
||||
expect(req.context.a).toEqual('a');
|
||||
});
|
||||
Parse.Cloud.afterSave('TestObject', (req) => {
|
||||
expect(req.context.a).toEqual('a');
|
||||
});
|
||||
await obj.save(null, { context: { a: 'a' } });
|
||||
});
|
||||
|
||||
it('should have access to context when saving a new object in a trigger', async () => {
|
||||
Parse.Cloud.beforeSave('TestObject', (req) => {
|
||||
expect(req.context.a).toEqual('a');
|
||||
});
|
||||
Parse.Cloud.afterSave('TestObject', (req) => {
|
||||
expect(req.context.a).toEqual('a');
|
||||
});
|
||||
Parse.Cloud.afterSave('TriggerObject', async () => {
|
||||
const obj = new TestObject();
|
||||
await obj.save(null, { context: { a: 'a' } });
|
||||
});
|
||||
const obj = new Parse.Object('TriggerObject');
|
||||
await obj.save(null);
|
||||
});
|
||||
|
||||
it('should have access to context when cascade-saving objects', async () => {
|
||||
Parse.Cloud.beforeSave('TestObject', (req) => {
|
||||
expect(req.context.a).toEqual('a');
|
||||
});
|
||||
Parse.Cloud.afterSave('TestObject', (req) => {
|
||||
expect(req.context.a).toEqual('a');
|
||||
});
|
||||
Parse.Cloud.beforeSave('TestObject2', (req) => {
|
||||
expect(req.context.a).toEqual('a');
|
||||
});
|
||||
Parse.Cloud.afterSave('TestObject2', (req) => {
|
||||
expect(req.context.a).toEqual('a');
|
||||
});
|
||||
const obj = new Parse.Object("TestObject");
|
||||
const obj2 = new Parse.Object("TestObject2");
|
||||
obj.set("obj2", obj2);
|
||||
await obj.save(null, { context: { a: 'a' } });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -52,12 +52,13 @@ function RestWrite(
|
||||
this.runOptions.action = action;
|
||||
}
|
||||
|
||||
// Parse context
|
||||
if (data._context && data._context instanceof Object) {
|
||||
this.context = data._context;
|
||||
delete data._context;
|
||||
}
|
||||
|
||||
if (!query) {
|
||||
// Parse context
|
||||
if (data._context && data._context instanceof Object) {
|
||||
this.context = data._context;
|
||||
delete data._context;
|
||||
}
|
||||
if (this.config.allowCustomObjectId) {
|
||||
if (
|
||||
Object.prototype.hasOwnProperty.call(data, 'objectId') &&
|
||||
|
||||
@@ -91,8 +91,8 @@ function handleBatch(router, req) {
|
||||
return initialPromise.then(() => {
|
||||
const promises = req.body.requests.map(restRequest => {
|
||||
const routablePath = makeRoutablePath(restRequest.path);
|
||||
// Construct a request that we can send to a handler
|
||||
|
||||
// Construct a request that we can send to a handler
|
||||
const request = {
|
||||
body: restRequest.body,
|
||||
config: req.config,
|
||||
@@ -100,6 +100,9 @@ function handleBatch(router, req) {
|
||||
info: req.info,
|
||||
};
|
||||
|
||||
// Add context to request body
|
||||
if (req.body._context) { request.body._context = req.body._context; }
|
||||
|
||||
return router
|
||||
.tryRouteRequest(restRequest.method, routablePath, request)
|
||||
.then(
|
||||
|
||||
Reference in New Issue
Block a user