Skip to content

Reject empty reference tokens in JsonPatch - #68613

Open
Youssef1313 wants to merge 1 commit into
mainfrom
dev/ygerges/jsonpatch-rfc
Open

Reject empty reference tokens in JsonPatch#68613
Youssef1313 wants to merge 1 commit into
mainfrom
dev/ygerges/jsonpatch-rfc

Conversation

@Youssef1313

Copy link
Copy Markdown
Member

Fixes #67819

Copilot AI lite review requested due to automatic review settings August 18, 2026 08:32

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.

Pull request overview

Updates ParsedPath parsing in both JsonPatch implementations to reject empty JSON Pointer reference tokens (e.g., //, trailing /), aligning behavior with the intended “no empty tokens” policy and preventing silent path collapsing.

Changes:

  • Add explicit rejection of empty reference tokens during path parsing in ParsedPath.
  • Treat empty string ("") as a valid “root pointer” path (returns no segments).
  • Expand unit tests to cover leading / paths, empty path, and empty-token rejection cases.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/Features/JsonPatch/src/Internal/ParsedPath.cs Rejects empty reference tokens and handles empty path early.
src/Features/JsonPatch/test/Internal/ParsedPathTests.cs Adds coverage for leading /, empty string, and empty-token failures.
src/Features/JsonPatch.SystemTextJson/src/Internal/ParsedPath.cs Mirrors the same empty-token rejection + empty-path handling for STJ.
src/Features/JsonPatch.SystemTextJson/test/Internal/ParsedPathTests.cs Mirrors the same test coverage changes for STJ.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +44 to +47
if (path.Length == 0)
{
return Array.Empty<string>();
}
Comment on lines +40 to +43
if (path.Length == 0)
{
return Array.Empty<string>();
}
Comment on lines +45 to +52
[Theory]
[InlineData("//isSmth")]
[InlineData("//")]
[InlineData("/foo/")]
[InlineData("/foo//bar")]
[InlineData("foo//bar")]
[InlineData("foo/")]
public void PathWithEmptyReferenceTokenShouldFail(string path)
Comment on lines +45 to +52
[Theory]
[InlineData("//isSmth")]
[InlineData("//")]
[InlineData("/foo/")]
[InlineData("/foo//bar")]
[InlineData("foo//bar")]
[InlineData("foo/")]
public void PathWithEmptyReferenceTokenShouldFail(string path)
}
else
{
throw new JsonPatchException(Resources.FormatInvalidValueForPath(path), null);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

per RFC 6901 section 5 both "" and "/" are valid and we accept the first one and reject the latter. Should we acccept both instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@DeagleGross From the same spec // is also valid but we are rejecting it.

Maybe we should try to be fully compliant then?

The reason I rejected / is that it essentially means a key which is empty string, which is very similar to // (just that an extra level of nesting).

So I would say we either handle empty keys everywhere, or we don't handle empty keys.

For "" it means whole document, not empty key, which is why I viewed it differently.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe we should try to be fully compliant then?

Ye, I imagined we started to treat none of these as errors.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Okay. I'll look more into the implementation and the RFC to get it right.

[InlineData("/foo//bar")]
[InlineData("foo//bar")]
[InlineData("foo/")]
public void PathWithEmptyReferenceTokenShouldFail(string path)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

could we add an E2E tests for such scenario to not only asssert the exception, but validate what's the HTTP response?


[Theory]
[InlineData("//isSmth")]
[InlineData("//")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

test [InlineData("/")]?

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.

JsonPatch: ParsedPath drops empty JSON Pointer tokens (RFC 6901)

3 participants