Skip to content

Fix patch order for interdependent move operations #163

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented May 27, 2025

Issue

When jsonpatch.make_patch() creates patches with multiple interdependent move operations, the operations are sometimes ordered incorrectly, resulting in incorrect patch application. This happened because the order in which operations were generated didn't account for dependencies between operations.

The issue occurs when:

  1. One move operation moves a value from location A to location B
  2. Another move operation takes from location B (which now contains the value from A)

If these operations are executed in the wrong order, they can produce incorrect results.

Solution

The solution implements a dependency analysis for move operations to ensure proper order:

  1. Collect all operations from the linked list in DiffBuilder.execute()
  2. Identify move operations and analyze their dependencies
  3. Build a dependency graph where:
    • If operation A moves a value to a location that operation B then takes from, B depends on A
  4. Perform a topological sort to determine the correct execution order
  5. Reorder the operations accordingly before returning them

This approach ensures that interdependent move operations are consistently ordered for proper execution.

Example of the issue (now fixed):

src_obj = {'a': [{'id': [1]}, {'id': [2]}], 'b': [{'id': 5}]}
tgt_obj = {'a': [{'id': []}, {'id': [1]}], 'b': [{'id': 5, 'newKey': 2}]}

# Before: move operations could be in either order, sometimes causing errors
# After: operations are always ordered correctly, ensuring successful patch application
patch = jsonpatch.make_patch(src_obj, tgt_obj)

Fixes #160.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Patch order sometimes wrong Fix patch order for interdependent move operations May 27, 2025
@Copilot Copilot AI requested a review from stefankoegl May 27, 2025 21:22
Copilot finished work on behalf of stefankoegl May 27, 2025 21:23
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.

Patch order sometimes wrong
2 participants