-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.Text.Json deserialisation does not respect "required" keyword when used in base class #86031
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionWhen I use System.Text.Json to deserialise to a class which derives from a base class using Reproduction StepsCode snippet showing the unexpected behavior (case 1) and also a working example (case 2): using System;
using System.Text.Json;
public class Program
{
public static void Main()
{
// Problematic test case
Console.WriteLine("Case 1: Derived class");
try
{
var obj = JsonSerializer.Deserialize("{}", typeof(DerivedTestClass));
Console.WriteLine("obj: " + obj.ToString());
}
catch (JsonException e)
{
// Is not been thrown
Console.WriteLine(e.Message);
}
// Second test case for reference
Console.WriteLine("\nCase 2: Base class");
try
{
var obj = JsonSerializer.Deserialize("{}", typeof(TestClass));
Console.WriteLine("obj: " + obj.ToString());
}
catch (JsonException e)
{
// Is thrown as expected
Console.WriteLine(e.Message);
}
}
private class TestClass
{
public required string StringValue { get; set; }
public required int IntegerValue { get; set; }
}
private class DerivedTestClass : TestClass
{
public override string ToString()
{
return $"StringValue: '{StringValue}', IntegerValue: '{IntegerValue}'";
}
}
} Expected behavior
Actual behaviorThe deserialised class is instantiated with default values for the properties. Regression?Not applicable, new feature in .NET 7 Known WorkaroundsThe example works as expected when I used the Configuration.NET 7.0.5 Other informationNo response
|
cc @krwq |
The issue appears to be caused by this logic: Lines 79 to 83 in 5e1608d
The |
I should add that the issue only impacts the reflection serializer, it works as expected if you try the same repro in the source generator. |
Description
When I use System.Text.Json to deserialise to a class which derives from a base class using
required
keyword in a property, the keyword has no effect.Reproduction Steps
Code snippet showing the unexpected behavior (case 1) and also a working example (case 2):
Expected behavior
JsonSerializer.Deserialize
should throw aJsonException
for the required (and not set) properties.Actual behavior
The deserialised class is instantiated with default values for the properties.
Regression?
Not applicable, new feature in .NET 7
Known Workarounds
The example works as expected when I used the
[JsonRequired]
attribute instead of therequired
keyword.Configuration
.NET 7.0.5
Also on local machine (windows, x64) and in .NET Fiddle: https://dotnetfiddle.net/ffvMGn
Other information
No response
The text was updated successfully, but these errors were encountered: