Repository navigation
Closed
Labels
area-System.Text.Jsonblocking-releasebugin-prThere is an active PR which will close this issue when it is merged
Milestone
Description
The code
using System.Text.Json;
using System.Text.Json.Serialization;
JsonSerializer.Serialize(new MyClass(), MyJsonContext.Default.MyClass);
[JsonSerializable(typeof(MyClass))]
public partial class MyJsonContext : JsonSerializerContext
{
}
public class MyClass
{
public MyEnum Value { get; set; }
}
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum MyEnum
{
X = 0
}Fails with an InvalidOperationException. This is because the the generated JsonTypeInfo<MyEnum> constructor is attempting to pass the custom converter without checking if it's a factory.
Generated code
// <auto-generated/>
#nullable enable
public partial class MyJsonContext
{
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::MyEnum>? _MyEnum;
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::MyEnum> MyEnum
{
get
{
if (_MyEnum == null)
{
global::System.Text.Json.Serialization.JsonConverter? customConverter;
if (Options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(typeof(global::MyEnum))) != null)
{
_MyEnum = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::MyEnum>(Options, customConverter);
}
else
{
global::System.Text.Json.Serialization.JsonConverter converter = new global::System.Text.Json.Serialization.JsonStringEnumConverter();
global::System.Type typeToConvert = typeof(global::MyEnum);
if (!converter.CanConvert(typeToConvert))
{
global::System.Type? underlyingType = global::System.Nullable.GetUnderlyingType(typeToConvert);
if (underlyingType != null && converter.CanConvert(underlyingType))
{
global::System.Text.Json.Serialization.JsonConverter? actualConverter = converter;
if (converter is global::System.Text.Json.Serialization.JsonConverterFactory converterFactory)
{
actualConverter = converterFactory.CreateConverter(underlyingType, Options);
if (actualConverter == null || actualConverter is global::System.Text.Json.Serialization.JsonConverterFactory)
{
throw new global::System.InvalidOperationException($"JsonConverterFactory '{converter} cannot return a 'null' or 'JsonConverterFactory' value.");
}
}
// Allow nullable handling to forward to the underlying type's converter.
converter = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.GetNullableConverter<global::MyEnum>(this.MyEnum);
}
else
{
throw new global::System.InvalidOperationException($"The converter '{converter.GetType()}' is not compatible with the type '{typeToConvert}'.");
}
}
_MyEnum = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::MyEnum>(Options, converter);
}
}
return _MyEnum;
}
}
}Metadata
Metadata
Assignees
Labels
area-System.Text.Jsonblocking-releasebugin-prThere is an active PR which will close this issue when it is merged
Activity
eiriktsarpalis commentedon Aug 27, 2021
Related to #58119 and #56864.
eiriktsarpalis commentedon Aug 27, 2021
Similarly when attaching the attribute on the property level
I'm getting a compiler error:
from the generated code
Again, this happens because we're trying to cast the converter factory to
JsonConverter<MyEnum>.layomia commentedon Sep 8, 2021
Fixed for 6.0 in #58652 cc @steveharter, @eiriktsarpalis.