converter = options.ExpandConverterFactory(converter, typeToConvert);
if (!converter.TypeToConvert.IsInSubtypeRelationshipWith(typeToConvert))
{
- ThrowHelper.ThrowInvalidOperationException_SerializationConverterNotCompatible(converter.GetType(), converter.TypeToConvert);
+ ThrowHelper.ThrowInvalidOperationException_SerializationConverterNotCompatible(converter.GetType(), typeToConvert);
}
JsonSerializerOptions.CheckConverterNullabilityIsSameAsPropertyType(converter, typeToConvert);
{
Assert.Throws<ArgumentNullException>(() => (new JsonSerializerOptions()).GetConverter(typeToConvert: null!));
}
+
+ [Fact]
+ public static void ErrorMessageContainsExpectedType()
+ {
+ JsonSerializerOptions options = new();
+ options.Converters.Add(new InvalidJsonConverterFactory());
+ var ex = Assert.Throws<InvalidOperationException>(() =>
+ JsonSerializer.Serialize(new InvalidTestInfo("Hello"), options));
+ Assert.Contains(typeof(InvalidTestInfo).Name, ex.Message);
+ }
+
+ private sealed record InvalidTestInfo(string Name);
+
+ private sealed class InvalidJsonConverterFactory : JsonConverterFactory
+ {
+ public override bool CanConvert(Type typeToConvert) => true;
+
+ public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
+ => new MyBoolEnumConverter();
+ }
}
}