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
{{ message }}
Repository navigation
STJ source generator does not correctly wrap custom converters on nullable properties #81833
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.
Activity
eiriktsarpalis commentedon Feb 9, 2023
Should be addressed in conjunction with #79311
eiriktsarpalis commentedon Jun 7, 2023
Fixed in #84208