runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Fix JSON issue (InvalidCastException) with ctor nullable enum ctor params having default values

Open ttv86 opened this issue 4 years ago • 6 comments

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

ttv86 avatar Apr 28 '22 11:04 ttv86

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.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

Author: ttv86
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

ghost avatar Apr 28 '22 11:04 ghost

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 avatar Apr 28 '22 13:04 eiriktsarpalis

Moving to .NET 8 milestone.

eiriktsarpalis avatar Jul 19 '22 12:07 eiriktsarpalis

Marking up-for-grabs. The site of the exception shown above is likely where a fix here would go.

layomia avatar Dec 02 '22 16:12 layomia

[Triage] @eiriktsarpalis validate if this still should be open

krwq avatar Jan 25 '23 17:01 krwq

Issue still occurs in .NET 7, it is common enough that we should try to fix this during .NET 8 given available cycles.

eiriktsarpalis avatar Jan 27 '23 10:01 eiriktsarpalis