From 8ecce49b4107dad2b363d3a6e299daa91faa4907 Mon Sep 17 00:00:00 2001 From: Charan Rathore <180254320+charan-rathore@users.noreply.github.com> Date: Sun, 29 Nov 2026 08:44:33 +0530 Subject: [PATCH] fix: preserve sibling fields in after-save and LiveQuery snapshots --- spec/CloudCode.spec.js | 46 +++++++++++++++++++++++++++++++++++++ spec/ParseLiveQuery.spec.js | 37 +++++++++++++++++++++++++++++ src/RestWrite.js | 6 +++-- 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/spec/CloudCode.spec.js b/spec/CloudCode.spec.js index 5edfe6fcac..f10764c5ce 100644 --- a/spec/CloudCode.spec.js +++ b/spec/CloudCode.spec.js @@ -21,6 +21,52 @@ const mockAdapter = { }; describe('Cloud Code', () => { + describe('nested sibling snapshot after Increment', () => { + for (const before of [false, true]) { + it(`matches stored nested siblings after Increment with preceding value=${before}`, async () => { + const initial = { views: 5, untouched: { enabled: true } }; + const obj = await new Parse.Object('IncrementSnapshot').save({ stats: initial }); + const seen = []; + Parse.Cloud.afterSave('IncrementSnapshot', ({ object }) => { + seen.push(object.get('stats')); + }); + const body = {}; + if (before) { + body['stats.title'] = 'before'; + } + body['stats.views'] = { __op: 'Increment', amount: 1 }; + body['stats.label'] = 'after'; + body['stats.active'] = false; + body['stats.tags'] = ['a', 'b']; + body['stats.metadata'] = { source: 'rest', priority: 2 }; + const response = await request({ + method: 'PUT', + url: `http://localhost:8378/1/classes/IncrementSnapshot/${obj.id}`, + headers: { + 'X-Parse-Application-Id': 'test', + 'X-Parse-Master-Key': 'test', + 'Content-Type': 'application/json', + }, + body, + }); + const stored = await new Parse.Query('IncrementSnapshot').get(obj.id, { useMasterKey: true }); + const expected = { + ...initial, + views: 6, + ...(before ? { title: 'before' } : {}), + label: 'after', + active: false, + tags: ['a', 'b'], + metadata: { source: 'rest', priority: 2 }, + }; + expect(stored.get('stats')).toEqual(expected); + expect(seen.length).toBe(1); + expect(seen[0]).toEqual(stored.get('stats')); + expect(response.data.stats).toEqual(stored.get('stats')); + }); + } + }); + it('can load absolute cloud code file', done => { reconfigureServer({ cloud: __dirname + '/cloud/cloudCodeRelativeFile.js', diff --git a/spec/ParseLiveQuery.spec.js b/spec/ParseLiveQuery.spec.js index 59ab337897..794822308e 100644 --- a/spec/ParseLiveQuery.spec.js +++ b/spec/ParseLiveQuery.spec.js @@ -12,6 +12,43 @@ const validatorFail = () => { }; describe('ParseLiveQuery', function () { + it('nested sibling snapshot after Increment matches stored data without afterSave', async () => { + const obj = await new TestObject().save({ stats: { views: 5, untouched: 'kept' } }); + const query = new Parse.Query(TestObject); + query.equalTo('objectId', obj.id); + const subscription = await query.subscribe(); + try { + const updated = new Promise(resolve => subscription.on('update', resolve)); + await request({ + method: 'PUT', + url: `http://localhost:8378/1/classes/TestObject/${obj.id}`, + headers: { + 'X-Parse-Application-Id': 'test', + 'X-Parse-Master-Key': 'test', + 'Content-Type': 'application/json', + }, + body: { + 'stats.views': { __op: 'Increment', amount: 1 }, + 'stats.title': 'after', + 'stats.active': false, + 'stats.metadata': { source: 'rest' }, + }, + }); + const eventObject = await updated; + const stored = await new Parse.Query(TestObject).get(obj.id, { useMasterKey: true }); + expect(stored.get('stats')).toEqual({ + views: 6, + untouched: 'kept', + title: 'after', + active: false, + metadata: { source: 'rest' }, + }); + expect(eventObject.get('stats')).toEqual(stored.get('stats')); + } finally { + await subscription.unsubscribe(); + } + }); + beforeEach(() => { Parse.CoreManager.getLiveQueryController().setDefaultLiveQueryClient(null); }); diff --git a/src/RestWrite.js b/src/RestWrite.js index 8b4e319a18..bfc5a6a60c 100644 --- a/src/RestWrite.js +++ b/src/RestWrite.js @@ -2512,10 +2512,12 @@ function getSaveJSON() { // The save response handling of the SDK, estimating the attributes once instead of // once per returned field function handleSaveResponse(response, status) { - let attributes; + // The SDK pops pending operations before merging returned subdocuments. Capture + // their estimate first so a partial operation result keeps sibling writes. + const attributes = estimateAttributes.call(this); Object.defineProperty(this, 'attributes', { configurable: true, - get: () => (attributes ??= estimateAttributes.call(this)), + get: () => attributes, }); try { Object.getPrototypeOf(this)._handleSaveResponse.call(this, response, status);