-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Closed
Labels
area-System.Text.Jsonbugsource-generatorIndicates an issue with a source generator featureIndicates an issue with a source generator feature
Milestone
Description
Consider the following code:
var value = new MyPoco { Value = MyEnum.A };
Console.WriteLine(JsonSerializer.Serialize(value)); // works as expected
Console.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 72
public class MyPoco
{
[JsonConverter(typeof(JsonStringEnumConverter))]
public MyEnum? Value { get; set; }
}
public enum MyEnum { A, B, C };
[JsonSerializable(typeof(MyPoco))]
public partial class MyContext : JsonSerializerContext
{ }By convention, the reflection serializer automatically wraps custom property converters on nullable types. We should update the source generator to do the same.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area-System.Text.Jsonbugsource-generatorIndicates an issue with a source generator featureIndicates an issue with a source generator feature