Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Pending updates to nested field causes ParseObject.toJSON() to return incorrect object #1453

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading