Skip to content

Preserve removal order when factoring JSON patch moves - #171

Open
PHJ2000 wants to merge 1 commit into
java-json-tools:masterfrom
PHJ2000:fix-array-removal-move-factorization
Open

PHJ2000 wants to merge 1 commit into
java-json-tools:masterfrom
PHJ2000:fix-array-removal-move-factorization

Conversation

@PHJ2000

@PHJ2000 PHJ2000 commented Oct 22, 2026

Copy link
Copy Markdown

A generated patch can silently move the wrong array element when move factorization postpones a removal past another removal. For example, diffing {"a":[1,2],"b":[]} to {"a":[],"b":[1]} currently generates remove /a/0 followed by move /a/0 -> /b/-, producing {"a":[],"b":[2]}.

Only factor the immediately preceding removal into a move. Earlier removals keep their original position because intervening operations may depend on their effect on array indices. This trades some patch compactness for preserving the generated patch's meaning; adjacent removal/addition pairs still use move operations.

Fixes #155. The patch generated from the original A and B attachments now produces B correctly.

Validation:

  • Added four focused regression fixtures; three fail on the original implementation, and one checks a safe adjacent move.
  • Added exhaustive round-trip checks for 2,401 source/target pairs containing small arrays, including duplicates.
  • All 203 TestNG tests, including JsonPatchTestSuite, pass on JDK 8 and JDK 11.
  • The repository's Gradle build cannot configure locally because repo.springsource.org no longer resolves for propdeps-plugin:0.0.7. The tests were run with a temporary Maven harness using the dependencies in project.gradle, Java 7 source/target levels, and the repository's actual source/test/resource directories. The Gradle build and Error Prone checks remain unverified; no alternative build files are included in this PR.
  • git diff --check passes.
Signed-off-by: Jaehong Park <105151063+PHJ2000@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant