You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varvalue=newMyPoco{Value=MyEnum.A};Console.WriteLine(JsonSerializer.Serialize(value));// works as expectedConsole.WriteLine(JsonSerializer.Serialize(value,MyContext.Default.MyPoco));// System.ArgumentException: GenericArguments[0], 'System.Nullable`1[MyEnum]', on 'System.Text.Json.Serialization.Converters.EnumConverter`1[T]' violates the constraint of type 'T'.// at System.RuntimeTypeHandle.Instantiate(RuntimeType inst)// at System.RuntimeType.MakeGenericType(Type[] instantiation)// -- - End of inner exception stack trace ---// at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)// at System.RuntimeType.MakeGenericType(Type[] instantiation)// at System.Text.Json.Serialization.Converters.EnumConverterFactory.Create(Type enumType, EnumConverterOptions converterOptions, JsonNamingPolicy namingPolicy, JsonSerializerOptions options)// at MyContext.GetConverterFromFactory(JsonSerializerOptions options, Type type, JsonConverterFactory factory) in C: \Users\eitsarpa\source\repos\ConsoleApp1\ConsoleApp1\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyContext.g.cs:line 77// at MyContext.GetConverterFromFactory[T] (JsonSerializerOptions options, JsonConverterFactory factory) in C: \Users\eitsarpa\source\repos\ConsoleApp1\ConsoleApp1\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyContext.g.cs:line 72publicclassMyPoco{[JsonConverter(typeof(JsonStringEnumConverter))]publicMyEnum?Value{get;set;}}publicenumMyEnum{A,B,C};
[JsonSerializable(typeof(MyPoco))]publicpartialclassMyContext:JsonSerializerContext{}
By convention, the reflection serializer automatically wraps custom property converters on nullable types. We should update the source generator to do the same.
The text was updated successfully, but these errors were encountered:
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
Consider the following code:
varvalue=newMyPoco{Value=MyEnum.A};Console.WriteLine(JsonSerializer.Serialize(value));// works as expectedConsole.WriteLine(JsonSerializer.Serialize(value,MyContext.Default.MyPoco));// System.ArgumentException: GenericArguments[0], 'System.Nullable`1[MyEnum]', on 'System.Text.Json.Serialization.Converters.EnumConverter`1[T]' violates the constraint of type 'T'.// at System.RuntimeTypeHandle.Instantiate(RuntimeType inst)// at System.RuntimeType.MakeGenericType(Type[] instantiation)// -- - End of inner exception stack trace ---// at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)// at System.RuntimeType.MakeGenericType(Type[] instantiation)// at System.Text.Json.Serialization.Converters.EnumConverterFactory.Create(Type enumType, EnumConverterOptions converterOptions, JsonNamingPolicy namingPolicy, JsonSerializerOptions options)// at MyContext.GetConverterFromFactory(JsonSerializerOptions options, Type type, JsonConverterFactory factory) in C: \Users\eitsarpa\source\repos\ConsoleApp1\ConsoleApp1\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyContext.g.cs:line 77// at MyContext.GetConverterFromFactory[T] (JsonSerializerOptions options, JsonConverterFactory factory) in C: \Users\eitsarpa\source\repos\ConsoleApp1\ConsoleApp1\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyContext.g.cs:line 72publicclassMyPoco{[JsonConverter(typeof(JsonStringEnumConverter))]publicMyEnum?Value{get;set;}}publicenumMyEnum{A,B,C};
[JsonSerializable(typeof(MyPoco))]publicpartialclassMyContext:JsonSerializerContext{}
By convention, the reflection serializer automatically wraps custom property converters on nullable types. We should update the source generator to do the same.
Consider the following code:
By convention, the reflection serializer automatically wraps custom property converters on nullable types. We should update the source generator to do the same.
The text was updated successfully, but these errors were encountered: