Skip to content

Move _path_join to changable argument - #189

Closed
we1come32 wants to merge 1 commit into
stefankoegl:masterfrom
we1come32:master
Closed

we1come32 wants to merge 1 commit into
stefankoegl:masterfrom
we1come32:master

Conversation

@we1come32

Copy link
Copy Markdown

I need to add xpath support for my project. It can add support to another json path style

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Regression tests are needed to validate custom path generation and list-root replacement.

2 open issues
What changed in this PR

Adds configurable path generation to support alternative path syntaxes alongside custom pointer classes.

Changes:

  • Exposes path_join through patch-generation APIs and uses it throughout diff construction.
  • Allows root-level add operations to replace list documents.
File Description
jsonpatch.py Adds configurable path joining and handles list-root replacement.

Review effort: Balanced

Comment thread jsonpatch.py
Comment on lines +292 to +293
if part is None:
obj = value # we're replacing the root
Comment thread jsonpatch.py
def from_diff(
cls, src, dst, optimization=True, dumps=None,
pointer_cls=JsonPointer,
pointer_cls=JsonPointer, path_join=_path_join,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, but I don’t think this is appropriate for this PR. Properly testing a custom path_join would require introducing another path format and an additional dependency, which is not justified for the current scope of the project.

The path_join hook is an extension point, and I’d rather not add a new format/dependency just to satisfy this coverage request. If maintainers explicitly want coverage, I can add a minimal in-test stub joiner/pointer class without a new dependency, but I don’t plan to add a new format as part of this change. Resolving as out of scope for now.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I don't see it as necessary to add a whole format with new dependencies. However it would be helpful to have at least a bare minimum example, so see whether the function is correctly called in all required places.

stefankoegl pushed a commit that referenced this pull request Nov 6, 2026
JsonPatch.from_diff and make_patch accepted a pointer_cls, but always
wrote RFC 6901 paths and then parsed them with pointer_cls. A pointer
class with another path syntax therefore produced patches that could not
be applied, even to the source document.

DiffBuilder now writes paths with path_join(parts, pointer_cls). This
covers the use case of #189 (other path styles in generated patches)
without a separate path_join argument that would have to be kept in sync
with pointer_cls.

path_join no longer uses JsonPointer.from_parts, which always builds an
RFC 6901 string and parses it with pointer_cls. Instead it sets the parts
of a pointer_cls instance and returns its path, so a pointer class with
another syntax only has to override __init__ and path. JsonPointer itself
keeps the previous fast path.

Refs #95, #189

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PewzphboJ9XrLY2h8AJ9iN
stefankoegl pushed a commit that referenced this pull request Nov 6, 2026
PatchOperation.path and MoveOperation.from_path joined the parts before
the key with a plain '/': without a leading '/', without escaping keys
that contain '/' or '~', and ignoring the syntax of pointer_cls. For
'/a~1b/0/c' they returned 'a/b/0', which is a different location.

They now return the location of the parent as a path, written with
path_join in the syntax of pointer_cls: '/a~1b/0' for the example above,
and '' for a target at the top level, as before. This changes the result
for all nested paths, e.g. 'a/0' becomes '/a/0'.

Refs #95, #189

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PewzphboJ9XrLY2h8AJ9iN
@stefankoegl

Copy link
Copy Markdown
Owner

Closing as #256 was just released in 1.45. https://python-json-patch.readthedocs.io/en/latest/tutorial.html#custom-path-syntax should cover your use case.

@stefankoegl stefankoegl closed this Nov 9, 2026
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.

3 participants