Skip to content

Fix unnecessary complexity in patch generation for lists of lists #169

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 3 commits into
base: master
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented May 27, 2025

Problem

When inserting new elements into a list of lists, the JSON patch representation was unnecessarily complex. Instead of generating a simple add operation with the whole list element as the value, it was breaking down the change into multiple operations (replace and add operations for individual elements).

For example, given:

import jsonpatch
a = [[1,'a']]
b = [[2,'b'], [1,'a']]
print(jsonpatch.make_patch(a, b))

Version 1.16 produced a simple, intuitive patch:

[{"path": "/0", "value": [2, "b"], "op": "add"}]

But version 1.20 produced an unnecessarily complex patch:

[{"path": "/0/0", "value": 2, "op": "replace"}, {"path": "/0/1", "value": "b", "op": "replace"}, {"path": "/1", "value": [1, "a"], "op": "add"}]

Solution

Added special case handling in the _compare_lists method to detect when elements are being inserted at the beginning of a list. This ensures that when inserting list elements, the patch generator produces a cleaner, more intuitive patch representation.

The change makes the patch representation simpler while maintaining backward compatibility with all existing behavior.

Fixes #78.


💡 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 representation unnecessary complex Fix unnecessary complexity in patch generation for lists of lists May 27, 2025
@Copilot Copilot AI requested a review from stefankoegl May 27, 2025 21:23
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 representation unnecessary complex
2 participants