Skip to content

Commit f29d972

Browse files
authored
fix: Cloud Code trigger beforeSave does not work with Parse.Role (#8320)
1 parent 4b1d46f commit f29d972

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

spec/CloudCode.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,22 @@ describe('Cloud Code', () => {
15581558
expect(obj.get('foo')).toBe('bar');
15591559
});
15601560

1561+
it('create role with name and ACL and a beforeSave', async () => {
1562+
Parse.Cloud.beforeSave(Parse.Role, ({ object }) => {
1563+
return object;
1564+
});
1565+
1566+
const obj = new Parse.Role('TestRole', new Parse.ACL({ '*': { read: true, write: true } }));
1567+
await obj.save();
1568+
1569+
expect(obj.getACL()).toEqual(new Parse.ACL({ '*': { read: true, write: true } }));
1570+
expect(obj.get('name')).toEqual('TestRole');
1571+
await obj.fetch();
1572+
1573+
expect(obj.getACL()).toEqual(new Parse.ACL({ '*': { read: true, write: true } }));
1574+
expect(obj.get('name')).toEqual('TestRole');
1575+
});
1576+
15611577
it('can unset in afterSave', async () => {
15621578
Parse.Cloud.beforeSave('TestObject', ({ object }) => {
15631579
if (!object.existed()) {

src/RestWrite.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ function RestWrite(config, auth, className, query, data, originalData, clientSDK
8686
// Shared SchemaController to be reused to reduce the number of loadSchema() calls per request
8787
// Once set the schemaData should be immutable
8888
this.validSchemaController = null;
89-
this.pendingOps = {};
89+
this.pendingOps = {
90+
operations: null,
91+
identifier: null,
92+
};
9093
}
9194

9295
// A convenient method to perform all the steps of processing the
@@ -227,10 +230,13 @@ RestWrite.prototype.runBeforeSaveTrigger = function () {
227230
}
228231

229232
const { originalObject, updatedObject } = this.buildParseObjects();
230-
233+
const identifier = updatedObject._getStateIdentifier();
231234
const stateController = Parse.CoreManager.getObjectStateController();
232-
const [pending] = stateController.getPendingOps(updatedObject._getStateIdentifier());
233-
this.pendingOps = { ...pending };
235+
const [pending] = stateController.getPendingOps(identifier);
236+
this.pendingOps = {
237+
operations: { ...pending },
238+
identifier,
239+
};
234240

235241
return Promise.resolve()
236242
.then(() => {
@@ -1586,7 +1592,7 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
15861592
.then(result => {
15871593
const jsonReturned = result && !result._toFullJSON;
15881594
if (jsonReturned) {
1589-
this.pendingOps = {};
1595+
this.pendingOps.operations = {};
15901596
this.response.response = result;
15911597
} else {
15921598
this.response.response = this._updateResponseWithData(
@@ -1690,10 +1696,9 @@ RestWrite.prototype.cleanUserAuthData = function () {
16901696
};
16911697

16921698
RestWrite.prototype._updateResponseWithData = function (response, data) {
1693-
const { updatedObject } = this.buildParseObjects();
16941699
const stateController = Parse.CoreManager.getObjectStateController();
1695-
const [pending] = stateController.getPendingOps(updatedObject._getStateIdentifier());
1696-
for (const key in this.pendingOps) {
1700+
const [pending] = stateController.getPendingOps(this.pendingOps.identifier);
1701+
for (const key in this.pendingOps.operations) {
16971702
if (!pending[key]) {
16981703
data[key] = this.originalData ? this.originalData[key] : { __op: 'Delete' };
16991704
this.storage.fieldsChangedByTrigger.push(key);

0 commit comments

Comments
 (0)