Skip to content

fix: decode escaped JSON Pointer tokens in ensurePathExists#221

Open
Yanhu007 wants to merge 1 commit intoevanphx:masterfrom
Yanhu007:fix/ensure-path-escaped-slash
Open

fix: decode escaped JSON Pointer tokens in ensurePathExists#221
Yanhu007 wants to merge 1 commit intoevanphx:masterfrom
Yanhu007:fix/ensure-path-escaped-slash

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #217

Problem

EnsurePathExistsOnAdd fails when the path contains escaped characters like ~1 (JSON Pointer encoding for /):

{"op": "add", "path": "/with~1slash/nested", "value": "baz"}

This should create {"with/slash": {"nested": "baz"}} but instead fails with:

doc is missing path: "/with~1slash/nested": missing value

Root Cause

ensurePathExists uses decodePatchKey(part) when calling get() to look up existing keys, but passes the raw (encoded) part to add() when creating new intermediate objects. The get looks for "with/slash" (decoded) but add creates "with~1slash" (encoded), so subsequent lookups fail.

Fix

Decode the path token with decodePatchKey() before passing to add(), consistent with the get() call.

Added regression test for escaped slash in nested path with EnsurePathExistsOnAdd.

All existing tests pass.

ensurePathExists used decodePatchKey for get operations but passed
the raw (encoded) path token to add operations. This caused keys
containing ~1 (escaped /) to be stored with the encoded form
instead of the decoded form, breaking nested path creation.

For example, path "/with~1slash/nested" should create a key
"with/slash" but was creating "with~1slash".

Decode the path token before passing to add. Add regression test.

Fixes evanphx#217
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.

EnsurePathExistsOnAdd not working with escaped slash

1 participant