fix: escape JSON pointer tokens in generated paths#22
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR implements RFC 6901-compliant JSON Pointer path generation by escaping reserved characters (~ and /) in object keys. Previously, keys containing these characters generated invalid JSON Pointer paths that could break patch operations.
Changes:
- Added
escapeReferenceToken()andbuildPath()helper functions to centralize and standardize path construction with proper character escaping - Replaced inline path construction logic with calls to
buildPath()to ensure consistent escaping across add/replace/remove operations - Added comprehensive test coverage for escape scenarios including slash-containing keys, tilde tokens, empty strings, RFC 6901 ordering, move operations, and maxDepth behavior
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/index.ts | Adds JSON Pointer token escaping functions and refactors path construction to use centralized buildPath() helper |
| src/index.spec.ts | Adds test cases covering various escape scenarios, edge cases, and integration with existing features |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
github-actions Bot
pushed a commit
that referenced
this pull request
Feb 12, 2026
## [1.2.6](v1.2.5...v1.2.6) (2026-02-12) ### Bug Fixes * escape JSON pointer tokens in generated paths ([#22](#22)) ([ff12c21](ff12c21))
Owner
Author
|
@BrianHuf fix is part of generate-json-patch@1.2.8 (took a phew turns to switch to trusted publishing 😆) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes JSON Pointer path generation to be RFC 6901-compliant when object keys contain reserved characters.
Previously, generated patch paths used raw object keys, which produced invalid/ambiguous pointers for keys containing
/,~, or empty string keys. This affected generated JSON Patch operations (pathandfrom) and could break downstream patch application.Closes #21.
cc @BrianHuf
What changed
Added JSON Pointer token escaping in path construction:
~->~0/->~1Tests added/updated
/and~~1token ->~01)/and nested trailing/)moveoperations (fromandpath)maxDepthbehavior with slash-containing keys