flipkart-incubator / flipkart-incubator/zjsonpatch

Remove operation builds an incorrect path with duplicate indexes for arrays

Open
#121 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
586
Forks
153
PR merge metrics
No merged PRs in 30d

Description

## Steps to Reproduce the Problem
Build diff using JsonDiff.asJson() with the following jsons:

1. Current
```
{
"id": "Contract 1",
"documents": [
{
"id": "1",
"name": "First document"
},
{
"id": "2",
"name": "Second document"
},
{
"id": "3",
"name": "Third document"
}
]
}
```

2. Modified
```
{
"id": "Contract 1",
"documents": [
{
"id": "1",
"name": "First document"
}
]
}
```

Removed last two objects from array

## Expected Behavior
Json Patch document
```
[
{
"op": "remove",
"path": "/documents/2"
},
{
"op": "remove",
"path": "/documents/1"
}
]
```
## Actual Behavior
Json Patch document
```
[
{
"op": "remove",
"path": "/documents/1"
},
{
"op": "remove",
"path": "/documents/1"
}
]
```

## Specifications
http://jsonpatch.com/
for jsonpatch used recommended https://json-patch-builder-online.github.io/

Library Version:
0.4.11

Notes:
private void removeRemaining(JsonPointer path, int pos, int srcIdx, int srcSize, JsonNode source) {
while (srcIdx < srcSize) {
JsonPointer currPath = path.append(pos);
if (flags.contains(DiffFlags.EMIT_TEST_OPERATIONS))
diffs.add(new Diff(Operation.TEST, currPath, source.get(srcIdx)));
diffs.add(Diff.generateDiff(Operation.REMOVE, currPath, source.get(srcIdx)));
srcIdx++;
}
}

this function build diffs from starting pos, but not from exact index.

Contributor guide

Open the contributing guide

Research direction

Start at the removeRemaining function identified in the issue and trace how its pos and srcIdx values are used to build JsonPointer paths. Reproduce the JsonDiff.asJson() example, then verify that removing multiple trailing array elements produces paths /documents/2 and /documents/1 in the expected order.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.