Repository navigation
Description
Description
When serializing JSON using System.Text.Json.JsonSerializer an object that has a constructor with some parameter that is nullable, but has non-null default value, JsonSerializer crashes.
The error seems to be that JsonTypeInfo sees that there is a constructor that has parameters. Then it goes to
System.Text.Json.Serialization.Metadata.JsonParameterInfo.InitializeDefaultValue that sees default value as int and tries to cast it as nullable enum type that fails.
Reproduction Steps
var ss = System.Text.Json.JsonSerializer.Serialize(new MyClass(), options);
public sealed class MyClass
{
public MyClass(MyEnum? value = MyEnum.Value)
{
this.Value = value;
}
public MyEnum? Value { get; }
}
public enum MyEnum
{
Value = 1,
}Expected behavior
Succesful serialization into following string: {"Value":1}
Actual behavior
Crashed with following exception:
InvalidCastException
"Unable to cast object of type 'System.Int32' to type 'System.Nullable`1[MyEnum]'."
at System.Text.Json.Serialization.Metadata.JsonParameterInfo`1.InitializeDefaultValue(JsonPropertyInfo matchingProperty)
at System.Text.Json.Serialization.Metadata.JsonParameterInfo`1.Initialize(JsonParameterInfoValues parameterInfo, JsonPropertyInfo matchingProperty, JsonSerializerOptions options)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo.InitializeConstructorParameters(JsonParameterInfoValues[] jsonParameters, Boolean sourceGenMode)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo..ctor(Type type, JsonConverter converter, Type runtimeType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.<InitializeForReflectionSerializer>g__CreateJsonTypeInfo|112_0(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetClassFromContextOrCreate(Type type)
at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type type)
at System.Text.Json.JsonSerializer.GetTypeInfo(JsonSerializerOptions options, Type runtimeType)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
at JsonCrashTest.Program.Main() in Program.cs:line 15
Regression?
No response
Known Workarounds
Workaround seems to have constructor parameter names not to match property names.
Configuration
Using .NET 6.0.3 on Windows 10 x64
Other information
No response
Activity
eiriktsarpalis commentedon Apr 28, 2022
I can reproduce -- it appears to be a bug specific to constructor parameter metadata initialization when resolving the default value for nullable enum parameters. We should fix this in .NET 7
eiriktsarpalis commentedon Jul 19, 2022
Moving to .NET 8 milestone.
layomia commentedon Dec 2, 2022
Marking up-for-grabs. The site of the exception shown above is likely where a fix here would go.
[-]JSON serialization of nullable enum fields[/-][+]Fix JSON issue (InvalidCastException) with ctor nullable enum ctor params having default values[/+]krwq commentedon Jan 25, 2023
[Triage] @eiriktsarpalis validate if this still should be open
eiriktsarpalis commentedon Jan 27, 2023
Issue still occurs in .NET 7, it is common enough that we should try to fix this during .NET 8 given available cycles.