Skip to content

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

Closed

Description

@ttv86

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

  1. ghost added
    untriaged
    on Apr 28, 2022
  2. added this to the 7.0.0 milestone on Apr 28, 2022
  3. ghost removed
    untriaged
    on Apr 28, 2022
  4. eiriktsarpalis commented on Apr 28, 2022

    @eiriktsarpalis
    Member

    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

  5. eiriktsarpalis commented on Jul 19, 2022

    @eiriktsarpalis
    Member

    Moving to .NET 8 milestone.

  6. modified the milestones: 7.0.0, 8.0.0 on Jul 19, 2022
  7. layomia commented on Dec 2, 2022

    @layomia
    Contributor

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

  8. added
    help wanted
    on Dec 2, 2022
  9. changed the title [-]JSON serialization of nullable enum fields[/-] [+]Fix JSON issue (InvalidCastException) with ctor nullable enum ctor params having default values[/+] on Dec 2, 2022
  10. krwq commented on Jan 25, 2023

    @krwq
    Member

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

  11. eiriktsarpalis commented on Jan 27, 2023

    @eiriktsarpalis
    Member

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

  12. ghost added
    in-pr
    on Feb 8, 2023
  13. ghost removed
    in-pr
    on Feb 9, 2023
  14. ghost locked as resolved and limited conversation to collaborators on Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-System.Text.Jsonbughelp wanted

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions