Skip to content

Commit 123828f

Browse files
committed
prevent conversion of array to object
1 parent 4e19dc5 commit 123828f

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

spec/ParseObject.spec.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,15 +572,35 @@ describe('Parse.Object testing', () => {
572572
const obj = new TestObject();
573573
obj.set('items', [ { value: 'a', count: 5 }, { value: 'b', count: 1 } ]);
574574
await obj.save();
575-
576575
obj.increment('items.0.count', 15);
577576
obj.increment('items.1.count', 4);
578577
await obj.save();
579-
578+
expect(obj.toJSON().items[0].value).toBe('a');
579+
expect(obj.toJSON().items[1].value).toBe('b');
580+
expect(obj.toJSON().items[0].count).toBe(20);
581+
expect(obj.toJSON().items[1].count).toBe(5);
580582
const query = new Parse.Query(TestObject);
581583
const result = await query.get(obj.id);
584+
expect(result.get('items')[0].value).toBe('a');
585+
expect(result.get('items')[1].value).toBe('b');
582586
expect(result.get('items')[0].count).toBe(20);
583587
expect(result.get('items')[1].count).toBe(5);
588+
expect(result.get('items')).toEqual(obj.get('items'));
589+
});
590+
591+
it_only_db('mongo')('can increment array nested fields missing index', async () => {
592+
const obj = new TestObject();
593+
obj.set('items', []);
594+
await obj.save();
595+
obj.increment('items.1.count', 15);
596+
await obj.save();
597+
expect(obj.toJSON().items[0]).toBe(null);
598+
expect(obj.toJSON().items[1].count).toBe(15);
599+
const query = new Parse.Query(TestObject);
600+
const result = await query.get(obj.id);
601+
expect(result.get('items')[0]).toBe(null);
602+
expect(result.get('items')[1].count).toBe(15);
603+
expect(result.get('items')).toEqual(obj.get('items'));
584604
});
585605

586606
it('addUnique with object', function (done) {

src/Controllers/DatabaseController.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,13 @@ class DatabaseController {
18511851
// only valid ops that produce an actionable result
18521852
// the op may have happened on a keypath
18531853
this._expandResultOnKeyPath(response, key, result);
1854+
// Revert array to object conversion on dot notation for arrays (e.g. "field.0.key")
1855+
if (key.includes('.')) {
1856+
const [field, index] = key.split('.');
1857+
if (!isNaN(index) && Array.isArray(result[field]) && !Array.isArray(response[field])) {
1858+
response[field] = result[field];
1859+
}
1860+
}
18541861
}
18551862
});
18561863
return Promise.resolve(response);

0 commit comments

Comments
 (0)