Skip to content

Include propertyName in KeyNotFoundException thrown by JsonElement.GetProperty#124965

Merged
eiriktsarpalis merged 3 commits intomainfrom
copilot/include-property-name-in-exception
Mar 16, 2026
Merged

Include propertyName in KeyNotFoundException thrown by JsonElement.GetProperty#124965
eiriktsarpalis merged 3 commits intomainfrom
copilot/include-property-name-in-exception

Conversation

Copy link
Contributor

Copilot AI commented Feb 27, 2026

JsonElement.GetProperty threw KeyNotFoundException with no message, making it impossible to identify which property caused the failure in chained calls.

Changes

  • JsonElement.cs: All three GetProperty overloads now include the property name in the exception message using the existing (previously unused) Arg_KeyNotFoundWithKey resource string:
    • GetProperty(string) — passes name directly
    • GetProperty(ReadOnlySpan<char>) — converts via .ToString()
    • GetProperty(ReadOnlySpan<byte>) — decodes via Encoding.UTF8.GetString
  • JsonDocumentTests.cs: Updated TryGetProperty_NoProperty to assert exception messages contain the property name across all three overloads.

Before / After

// Before
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

// After
System.Collections.Generic.KeyNotFoundException: The given key 'dateRange' was not present in the dictionary.
Original prompt

This section details on the original issue you should resolve

<issue_title>Include propertyName when KeyNotFoundException is thrown in JsonElement.GetProperty</issue_title>
<issue_description>### Suggestion
Add property name in exception message when KeyNotFoundException is thrown:
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs#L142

Current behavior

No property can be found in logs.

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.Text.Json.JsonElement.GetProperty(String propertyName)
   at ...

Proposed behavior

Property name can be found in logs.

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. Property name is 'dateRange'.
   at System.Text.Json.JsonElement.GetProperty(String propertyName)
   at ...

Reasoning

When having chained methods sometimes it is hard to understand what property caused the issue.
Sample code:

var dateEnd = jsonElement.EnumerateArray()
    .First()
    .GetProperty("periods")
    .EnumerateArray()
    .First()
    .GetProperty(“dateRange”)
    .GetProperty("dateEnd")
    .GetDateTime()
    .Date;

Proposed implementation

// Strings.KeyNotFoundException = "The given key was not present in the dictionary. Property name is '{0}'."
throw new KeyNotFoundException(SR.Format(Strings.KeyNotFoundException, propertyName));
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

Copilot AI changed the title [WIP] Include propertyName in KeyNotFoundException for JsonElement.GetProperty Include propertyName in KeyNotFoundException thrown by JsonElement.GetProperty Feb 27, 2026
@eiriktsarpalis eiriktsarpalis marked this pull request as ready for review February 27, 2026 15:51
Copilot AI review requested due to automatic review settings February 27, 2026 15:51
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 GetProperty overloads in JsonElement.cs to include the property name in KeyNotFoundException messages using the existing Arg_KeyNotFoundWithKey resource string
  • Enhanced test coverage in JsonDocumentTests.cs to 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
Copy link
Member

/ba-g the test failures are unrelated.

@eiriktsarpalis eiriktsarpalis merged commit 85b61ab into main Mar 16, 2026
87 of 90 checks passed
@eiriktsarpalis eiriktsarpalis deleted the copilot/include-property-name-in-exception branch March 16, 2026 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Include propertyName when KeyNotFoundException is thrown in JsonElement.GetProperty

4 participants