From 408f02a598aa007212c977983114cd93fb9a448e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kartal=20Kaan=20Bozdo=C4=9Fan?= Date: Fri, 12 Feb 2021 22:57:22 +0100 Subject: [PATCH] fix: Pass customObjectId in beforeSave (#7167) * beforeSave custom object id fix * Added regression test case for issue #6733 --- spec/CloudCode.spec.js | 33 +++++++++++++++++++++++++++++++++ src/triggers.js | 1 + 2 files changed, 34 insertions(+) diff --git a/spec/CloudCode.spec.js b/spec/CloudCode.spec.js index 7bc39a43bb..d4754c03ef 100644 --- a/spec/CloudCode.spec.js +++ b/spec/CloudCode.spec.js @@ -1779,6 +1779,39 @@ describe('beforeSave hooks', () => { const myObject = new MyObject(); myObject.save().then(() => done()); }); + + it('should respect custom object ids (#6733)', async () => { + Parse.Cloud.beforeSave('TestObject', req => { + expect(req.object.id).toEqual('test_6733'); + }); + + await reconfigureServer({ allowCustomObjectId: true }); + + const req = request({ + // Parse JS SDK does not currently support custom object ids (see #1097), so we do a REST request + method: 'POST', + url: 'http://localhost:8378/1/classes/TestObject', + headers: { + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + }, + body: { + objectId: 'test_6733', + foo: 'bar', + }, + }); + + { + const res = await req; + expect(res.data.objectId).toEqual('test_6733'); + } + + const query = new Parse.Query('TestObject'); + query.equalTo('objectId', 'test_6733'); + const res = await query.find(); + expect(res.length).toEqual(1); + expect(res[0].get('foo')).toEqual('bar'); + }); }); describe('afterSave hooks', () => { diff --git a/src/triggers.js b/src/triggers.js index eeb86c8369..8674cf9647 100644 --- a/src/triggers.js +++ b/src/triggers.js @@ -327,6 +327,7 @@ export function getResponseObject(request, resolve, reject) { response = {}; if (request.triggerName === Types.beforeSave) { response['object'] = request.object._getSaveJSON(); + response['object']['objectId'] = request.object.id; } return resolve(response); },