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

[bug] Fix increment bug for nested array object field #1343

Open
wants to merge 2 commits into
base: alpha
Choose a base branch
from

Conversation

KaustubhDamania
Copy link

Fixes parse-community/parse-server#6687

When I ran the below script,

const Parse = require('parse');

var jsondata = {
  _id: 'someId',
  items: [ { value: 'a', count: 5 }, { value: 'b', count: 1 } ],
  className: 'bug'
}

var object = Parse.Object.fromJSON(jsondata)
object.increment('items.0.count')
console.log(object.get('items'))

I got this output:

{ '0': { value: 'a', count: 6 }, '1': { value: 'b', count: 1 } }

The array was converted into a JSON object. I fixed this behaviour so that the resulting output is as expected:

[ { value: 'a', count: 6 }, { value: 'b', count: 1 } ]

I have modified the estimateAttribute function logic in src/ObjectStateMutations.js and modified relevant tests accordingly.

Fixes parse-community/parse-server#6687 by
modifying the estimateAttribute function logic in src/ObjectStateMutations.js.
@ghost
Copy link

ghost commented Apr 13, 2021

Warnings
⚠️ Please add a changelog entry for your changes.

Generated by 🚫 dangerJS

package-lock.json Show resolved Hide resolved
integration/test/ParseObjectTest.js Show resolved Hide resolved
@KaustubhDamania KaustubhDamania changed the title 🐛 Fix increment bug for nested array object field [bug] Fix increment bug for nested array object field Apr 14, 2021
@codecov
Copy link

codecov bot commented Apr 14, 2021

Codecov Report

Merging #1343 (c3d8137) into master (4a75d97) will decrease coverage by 0.70%.
The diff coverage is 42.46%.

❗ Current head c3d8137 differs from pull request most recent head 4c374de. Consider uploading reports for the commit 4c374de to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1343      +/-   ##
==========================================
- Coverage   99.94%   99.23%   -0.71%     
==========================================
  Files          60       60              
  Lines        5847     5919      +72     
  Branches     1311     1347      +36     
==========================================
+ Hits         5844     5874      +30     
- Misses          3       44      +41     
- Partials        0        1       +1     
Impacted Files Coverage Δ
src/OfflineQuery.js 86.40% <41.66%> (-13.60%) ⬇️
src/ObjectStateMutations.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4a75d97...4c374de. Read the comment docs.

@@ -304,10 +304,7 @@ describe('Parse Object', () => {
});

obj.increment('objectField.number', 15);
Copy link
Member

@dplewis dplewis Apr 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following test fails. Can you add it to the PR?

fit('can increment array nested fields', async () => {
    const obj = new TestObject();
    obj.set('items', [ { value: 'a', count: 5 }, { value: 'b', count: 1 } ]);
    await obj.save();

    obj.increment('items.0.count', 15);
    await obj.save();

    const query = new Parse.Query(TestObject);
    const result = await query.get(obj.id);
    assert.equal(result.get('items')[0].count, 20);
  });

Copy link
Member

@mtrezza mtrezza Apr 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would we differentiate between:

{
  "a": {
    "b": [
      { "c": 0 }
    ]
  }
}

and

{
  "a": {
    "b": {
      "0": {
        "c": 0
      }
    }
  }
}

Edit: I just looked up the MongoDB docs, and it seems MongoDB does not care, it apparently interprets "0" as index if it is an array and as key if it is an object. So that logic would have to be applied internally on the Parse Object as well.

@mtrezza
Copy link
Member

mtrezza commented Apr 20, 2021

Does this PR also address calling other atomic ops on nested keys, such as add or addUnique? I would assume it touches on a common central logic? I don't want to blow up the scope of this PR, but it may be worth considering.

Also, see my analysis point 3; is there a change in Parse Server necessary that accompanies this PR?

Also, I think we need to add a test which increments a nested field for an existing object, that has not been fetched but created with createWithoutData. As far as I can tell this does not currently work, see my analysis point 2, but it should work, otherwise it is not an "atomic" operation on the DB level, which is what one would assume from the Parse docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to increment array nested object field?
3 participants