Skip to content

Commit

Permalink
fix: Pending updates to nested field causes ParseObject.toJSON() to…
Browse files Browse the repository at this point in the history
… return incorrect object (#1453)
  • Loading branch information
mstniy authored May 2, 2024
1 parent 8b7fd1b commit 23cc573
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
12 changes: 12 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@ describe('Parse Object', () => {
assert.strictEqual(result.get('a').b.c.d, 2);
});

it('can set nested fields without repeating pending operations on toJSON (regression test for #1452)', async () => {
const a = new Parse.Object('MyObject');
a.set('obj', {});
await a.save();
a.set('obj.a', 0);
const json = a.toJSON();
expect(json.obj).toEqual({ a: 0 });
expect(new Set(Object.keys(json))).toEqual(
new Set(['objectId', 'createdAt', 'updatedAt', 'obj'])
);
});

it('can increment nested field and retain full object', async () => {
const obj = new Parse.Object('TestIncrementObject');
obj.set('objectField', { number: 5, letter: 'a' });
Expand Down
4 changes: 0 additions & 4 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,6 @@ class ParseObject {
json[attr] = encode(attrs[attr], false, false, seen, offline);
}
}
const pending = this._getPendingOps();
for (const attr in pending[0]) {
json[attr] = pending[0][attr].toJSON(offline);
}

if (this.id) {
json.objectId = this.id;
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,14 @@ describe('ParseObject', () => {
'objectField.number': 20,
otherField: { hello: 'world' },
});
expect(o.toJSON()).toEqual({
objectField: {
number: 20,
letter: 'a',
},
otherField: { hello: 'world' },
objectId: 'setNested',
});
});

it('can set multiple nested fields (regression test for #1450)', () => {
Expand Down

0 comments on commit 23cc573

Please sign in to comment.