Fix JsonNode.GetPath() to properly escape characters in property names#122841
Merged
stephentoub merged 7 commits intomainfrom Jan 13, 2026
Merged
Fix JsonNode.GetPath() to properly escape characters in property names#122841stephentoub merged 7 commits intomainfrom
stephentoub merged 7 commits intomainfrom
Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix JsonNode.GetPath method for character escaping
Fix JsonNode.GetPath() to properly escape characters in property names
Jan 3, 2026
3 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes JsonNode.GetPath() to produce valid JSON Path syntax when property names contain special characters requiring escaping. The fix addresses two specific issues: (1) property names starting with $ now use bracket notation instead of invalid dot notation, and (2) single quotes and backslashes in property names are now properly escaped within bracket notation.
Key Changes:
- Added
$to the set of special characters that trigger bracket notation in JSON Path - Implemented
AppendEscapedPropertyNamehelper methods to escape'as\'and\as\\within bracket notation - Updated all JSON Path generation sites (
JsonObject.GetPath,WriteStack.AppendPropertyName,ReadStack.AppendPropertyName) to use the new escaping
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| JsonReaderHelper.cs | Added $ to special characters set; implemented two overloads of AppendEscapedPropertyName for escaping quotes and backslashes |
| JsonObject.cs | Updated GetPath to call AppendEscapedPropertyName when using bracket notation |
| WriteStack.cs | Updated AppendPropertyName to call AppendEscapedPropertyName when using bracket notation |
| ReadStack.cs | Updated AppendPropertyName to call AppendEscapedPropertyName when using bracket notation |
| ParentPathRootTests.cs | Added comprehensive test cases covering $-prefixed names, quotes, backslashes, and nested scenarios |
| PolymorphicTests.CustomTypeHierarchies.cs | Updated test expectations from dot notation (e.g., $.$type) to bracket notation (e.g., $['$type']) |
| ReferenceHandlerTests.Deserialize.cs | Updated test expectations from dot notation to bracket notation for $-prefixed metadata properties |
| ConstructorTests.Exceptions.cs | Updated test expectations from dot notation to bracket notation for $-prefixed properties |
This was referenced Jan 4, 2026
stephentoub
reviewed
Jan 8, 2026
src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs
Show resolved
Hide resolved
…acter iteration for prefix Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
This was referenced Jan 8, 2026
Open
src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs
Outdated
Show resolved
Hide resolved
…thod Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
eiriktsarpalis
approved these changes
Jan 12, 2026
stephentoub
reviewed
Jan 13, 2026
src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
akoeplinger
added a commit
to dotnet/aspnetcore
that referenced
this pull request
Jan 15, 2026
This was an intentional behavioral change to System.Text.Json, see dotnet/runtime#122841
dotnet-maestro bot
added a commit
to dotnet/aspnetcore
that referenced
this pull request
Jan 15, 2026
[main] Source code updates from dotnet/dotnet - Update .devcontainer/devcontainer.json - Fix more 10.0 references - Fix more 10.0 references - Suppress EF8001 for now, see #65018 - Update SDK - Fix JSON - Merge branch 'main' into darc-main-62ab566a-e4e9-4ec8-b67d-2074a5ad3974 - Merge branch 'main' into darc-main-62ab566a-e4e9-4ec8-b67d-2074a5ad3974 # Conflicts: # src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Data/SqlLite/00000000000000_CreateIdentitySchema.Designer.cs # src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Data/SqlLite/ApplicationDbContextModelSnapshot.cs # src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Data/SqlServer/00000000000000_CreateIdentitySchema.Designer.cs # src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Data/SqlServer/ApplicationDbContextModelSnapshot.cs - Fix JsonFormatter_EscapedKeys_SingleQuote test This was an intentional behavioral change to System.Text.Json, see dotnet/runtime#122841
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Description
JsonNode.GetPath()produces invalid JSON Path syntax when property names contain characters requiring escaping. Two issues:$(e.g.,$defs,$ref) used dot notation ($.$defs) instead of bracket notation ($['$defs'])Before:
After:
// Returns: $['$defs']['foo[\'bar'] (valid JSON Path)Changes:
$to special characters triggering bracket notation inJsonReaderHelper.csAppendEscapedPropertyNamehelper methods that escape'→\'and\→\\, optimized usingIndexOfAnyto append prefix slices directly instead of per-character iterationStringBuilder.Append(ReadOnlySpan<char>)polyfill inCommon/src/System/Text/StringBuilderExtensions.csfor .NET Standard 2.0 and .NET Framework supportJsonObject.GetPath,WriteStack.AppendPropertyName, andReadStack.AppendPropertyNameto use escapingCustomer Impact
Users receiving invalid JSON Path strings from
GetPath()that cannot be parsed or used with JSON Path tools.Regression
No. This behavior has existed since the feature was introduced.
Testing
$-prefixed names, single quotes, backslashes, and combinationsRisk
Low. The change makes output spec-compliant. Existing consumers expecting the old (invalid) format may need adjustment.
Package authoring no longer needed in .NET 9
IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.
Original prompt
JsonNode.GetPath()doesn't consider character escaping #83547💡 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.