Commit 3915396
authored
Fix JsonException message property type info in class parameterized constructor (#126575)
This pull request fixes an **incorrect type hint** in the thrown
`JsonException` when deserialization fails for **classes with
parameterized constructors**.
## Reproduction
```csharp
using System.Text.Json;
Test<NormalClass>();
Test<ParameterizedNormalClass>();
Test<RecordClass>();
Test<NormalStruct>();
Test<ParameterizedNormalStruct>();
Test<RecordStruct>();
static void Test<T>()
{
try
{
JsonSerializer.Deserialize<T>("{\"Text\":{}}"); // create json convert exception manually
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
class NormalClass
{
public string Text { get; set; } = null!;
}
class ParameterizedNormalClass(string text)
{
public string Text { get; set; } = text;
}
record RecordClass(string Text);
struct NormalStruct()
{
public string Text { get; set; } = null!;
}
struct ParameterizedNormalStruct(string text)
{
public string Text { get; set; } = text;
}
record struct RecordStruct(string Text);
```
## Expected Output
```txt
The JSON value could not be converted to System.String. Path: $.Text | LineNumber: 0 | BytePositionInLine: 9.
The JSON value could not be converted to System.String. Path: $.Text | LineNumber: 0 | BytePositionInLine: 9.
The JSON value could not be converted to System.String. Path: $.Text | LineNumber: 0 | BytePositionInLine: 9.
The JSON value could not be converted to System.String. Path: $.Text | LineNumber: 0 | BytePositionInLine: 9.
The JSON value could not be converted to System.String. Path: $.Text | LineNumber: 0 | BytePositionInLine: 9.
The JSON value could not be converted to System.String. Path: $.Text | LineNumber: 0 | BytePositionInLine: 9.
```
## Actual Output
```txt
The JSON value could not be converted to System.String. Path: $.Text | LineNumber: 0 | BytePositionInLine: 9.
The JSON value could not be converted to ParameterizedNormalClass. Path: $.Text | LineNumber: 0 | BytePositionInLine: 9.
The JSON value could not be converted to RecordClass. Path: $.Text | LineNumber: 0 | BytePositionInLine: 9.
The JSON value could not be converted to System.String. Path: $.Text | LineNumber: 0 | BytePositionInLine: 9.
The JSON value could not be converted to System.String. Path: $.Text | LineNumber: 0 | BytePositionInLine: 9.
The JSON value could not be converted to System.String. Path: $.Text | LineNumber: 0 | BytePositionInLine: 9.
```
## Root Cause
For classes with parameterized constructors,
`state.Current.CtorArgumentState.JsonParameterInfo` was used instead of
`state.Current.JsonPropertyInfo`.
(All structs, including parameterized structs and record structs, work
correctly without this issue.)
https://github.com/dotnet/runtime/blob/3f14c31b74e2324a72aec383e92b9aabf65d1f22/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.cs#L636-L642
As a result, the exception message fell back to
`state.Current.JsonTypeInfo.Type` for affected classes.
https://github.com/dotnet/runtime/blob/3f14c31b74e2324a72aec383e92b9aabf65d1f22/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs#L542-L544
## Fix
Added logic to retrieve type information from
`state.Current.CtorArgumentState.JsonParameterInfo.ParameterType`,
ensuring correct type resolution for classes with parameterized
constructors.1 parent 17f52df commit 3915396
File tree
3 files changed
+33
-1
lines changed- src/libraries/System.Text.Json
- src/System/Text/Json
- tests
- Common/ConstructorTests
- System.Text.Json.SourceGeneration.Tests/Serialization
3 files changed
+33
-1
lines changedLines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
540 | 540 | | |
541 | 541 | | |
542 | 542 | | |
543 | | - | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
544 | 546 | | |
545 | 547 | | |
546 | 548 | | |
| |||
Lines changed: 26 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
350 | 350 | | |
351 | 351 | | |
352 | 352 | | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
353 | 379 | | |
354 | 380 | | |
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
| 180 | + | |
| 181 | + | |
180 | 182 | | |
181 | 183 | | |
182 | 184 | | |
| |||
349 | 351 | | |
350 | 352 | | |
351 | 353 | | |
| 354 | + | |
| 355 | + | |
352 | 356 | | |
353 | 357 | | |
354 | 358 | | |
| |||
0 commit comments