Skip to content

Commit 4f0f0ec

Browse files
authored
fix: Unable to create new role if beforeSave hook exists (#8474)
1 parent 0ec9239 commit 4f0f0ec

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

spec/ParseRole.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -601,4 +601,12 @@ describe('Parse Role testing', () => {
601601
});
602602
});
603603
});
604+
605+
it('should save role when beforeSave hook for _Role is present.', async done => {
606+
Parse.Cloud.beforeSave('_Role', () => {});
607+
const role = new Parse.Role('roleName', new Parse.ACL());
608+
await role.save({}, { useMasterKey: true });
609+
expect(role.id).toBeDefined();
610+
done();
611+
});
604612
});

src/RestWrite.js

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

9396
// A convenient method to perform all the steps of processing the
@@ -219,10 +222,13 @@ RestWrite.prototype.runBeforeSaveTrigger = function () {
219222
}
220223

221224
const { originalObject, updatedObject } = this.buildParseObjects();
222-
225+
const identifier = updatedObject._getStateIdentifier();
223226
const stateController = Parse.CoreManager.getObjectStateController();
224-
const [pending] = stateController.getPendingOps(updatedObject._getStateIdentifier());
225-
this.pendingOps = { ...pending };
227+
const [pending] = stateController.getPendingOps(identifier);
228+
this.pendingOps = {
229+
operations: { ...pending },
230+
identifier,
231+
};
226232

227233
return Promise.resolve()
228234
.then(() => {
@@ -1569,7 +1575,7 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
15691575
.then(result => {
15701576
const jsonReturned = result && !result._toFullJSON;
15711577
if (jsonReturned) {
1572-
this.pendingOps = {};
1578+
this.pendingOps.operations = {};
15731579
this.response.response = result;
15741580
} else {
15751581
this.response.response = this._updateResponseWithData(
@@ -1673,10 +1679,9 @@ RestWrite.prototype.cleanUserAuthData = function () {
16731679
};
16741680

16751681
RestWrite.prototype._updateResponseWithData = function (response, data) {
1676-
const { updatedObject } = this.buildParseObjects();
16771682
const stateController = Parse.CoreManager.getObjectStateController();
1678-
const [pending] = stateController.getPendingOps(updatedObject._getStateIdentifier());
1679-
for (const key in this.pendingOps) {
1683+
const [pending] = stateController.getPendingOps(this.pendingOps.identifier);
1684+
for (const key in this.pendingOps.operations) {
16801685
if (!pending[key]) {
16811686
data[key] = this.originalData ? this.originalData[key] : { __op: 'Delete' };
16821687
this.storage.fieldsChangedByTrigger.push(key);

0 commit comments

Comments
 (0)