Fix JsonException message property type info in class parameterized constructor#126575
Open
XeronOwO wants to merge 3 commits intodotnet:mainfrom
Open
Fix JsonException message property type info in class parameterized constructor#126575XeronOwO wants to merge 3 commits intodotnet:mainfrom
XeronOwO wants to merge 3 commits intodotnet:mainfrom
Conversation
be4ce8b to
c7c0874
Compare
This was referenced Apr 11, 2026
Member
|
Does this need a test? |
Author
|
@dotnet-policy-service agree |
Author
|
@danmoseley Yes, this needs a test. I've added a regression test to ConstructorTests.Exceptions.cs that verifies the JsonException message contains the correct property type (System.String) rather than the declaring class type (ParameterizedNormalClass / RecordClass) when deserialization fails for classes with parameterized constructors. |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes JsonException default message type reporting during deserialization failures for object types using parameterized constructors by resolving the failing member type from the active constructor parameter (when JsonPropertyInfo is unavailable).
Changes:
- Update
ThrowHelper.AddJsonExceptionInformationto preferstate.Current.CtorArgumentState?.JsonParameterInfo?.ParameterTypewhenJsonPropertyInfois null. - Add a regression test ensuring parameterized class/record constructor failures report the property/parameter type (
System.String) rather than the declaring type.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs | Adjusts default exception type resolution to use active constructor parameter type when applicable. |
| src/libraries/System.Text.Json/tests/Common/ConstructorTests/ConstructorTests.Exceptions.cs | Adds coverage validating the corrected exception message type for parameterized constructors. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 0
…tructors This test verifies that when deserialization fails for classes with parameterized constructors, the JsonException message reports the correct property type (e.g., System.String) rather than the declaring class type. Covers both parameterized classes and records to ensure the fix in ThrowHelper.Serialization.cs works correctly across different scenarios.
…essage test Make test classes public and register them in source generation contexts to fix compilation errors in System.Text.Json.SourceGeneration.Tests. The test classes need to be public (not private) so they can be referenced by the JsonSerializable attributes in the source generation test contexts.
13c27a4 to
a14616c
Compare
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.
This pull request fixes an incorrect type hint in the thrown
JsonExceptionwhen deserialization fails for classes with parameterized constructors.Reproduction
Expected Output
Actual Output
Root Cause
For classes with parameterized constructors,
state.Current.CtorArgumentState.JsonParameterInfowas used instead ofstate.Current.JsonPropertyInfo.(All structs, including parameterized structs and record structs, work correctly without this issue.)
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.cs
Lines 636 to 642 in 3f14c31
As a result, the exception message fell back to
state.Current.JsonTypeInfo.Typefor affected classes.runtime/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs
Lines 542 to 544 in 3f14c31
Fix
Added logic to retrieve type information from
state.Current.CtorArgumentState.JsonParameterInfo.ParameterType, ensuring correct type resolution for classes with parameterized constructors.