Fix JSON issue (InvalidCastException) with ctor nullable enum ctor params having default values
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
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
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis See info in area-owners.md if you want to be subscribed.
Issue Details
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
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
| Author: | ttv86 |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
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
Moving to .NET 8 milestone.
Marking up-for-grabs. The site of the exception shown above is likely where a fix here would go.
[Triage] @eiriktsarpalis validate if this still should be open
Issue still occurs in .NET 7, it is common enough that we should try to fix this during .NET 8 given available cycles.