Include propertyName in KeyNotFoundException thrown by JsonElement.GetProperty#124965
Merged
eiriktsarpalis merged 3 commits intomainfrom Mar 16, 2026
Merged
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-text-json |
…GetProperty Co-authored-by: eiriktsarpalis <[email protected]>
Copilot
AI
changed the title
[WIP] Include propertyName in KeyNotFoundException for JsonElement.GetProperty
Include propertyName in KeyNotFoundException thrown by JsonElement.GetProperty
Feb 27, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR enhances the diagnostic experience for JsonElement.GetProperty by including the missing property name in the KeyNotFoundException message. This addresses a common pain point where chained property access makes it difficult to identify which property lookup failed.
Changes:
- Updated all three
GetPropertyoverloads inJsonElement.csto include the property name inKeyNotFoundExceptionmessages using the existingArg_KeyNotFoundWithKeyresource string - Enhanced test coverage in
JsonDocumentTests.csto verify the exception messages contain the property name
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs |
Added property name to KeyNotFoundException messages in all three GetProperty overloads; added using System.Text; for UTF-8 decoding |
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonDocumentTests.cs |
Updated TryGetProperty_NoProperty test to assert exception messages contain property names for all three overloads |
Comments suppressed due to low confidence (1)
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonDocumentTests.cs:1981
- For consistency with the assertions on lines 1984-1989, consider also verifying the exception message for the long property name test on line 1981. This would ensure all GetProperty overloads have their exception messages validated.
Assert.Throws<KeyNotFoundException>(() => root.GetProperty(new string('z', 512)));
eiriktsarpalis
approved these changes
Feb 27, 2026
danmoseley
approved these changes
Mar 14, 2026
Member
|
/ba-g the test failures are unrelated. |
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.
JsonElement.GetPropertythrewKeyNotFoundExceptionwith no message, making it impossible to identify which property caused the failure in chained calls.Changes
JsonElement.cs: All threeGetPropertyoverloads now include the property name in the exception message using the existing (previously unused)Arg_KeyNotFoundWithKeyresource string:GetProperty(string)— passes name directlyGetProperty(ReadOnlySpan<char>)— converts via.ToString()GetProperty(ReadOnlySpan<byte>)— decodes viaEncoding.UTF8.GetStringJsonDocumentTests.cs: UpdatedTryGetProperty_NoPropertyto assert exception messages contain the property name across all three overloads.Before / After
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.