diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Converters.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Converters.cs index 152b84e984931..b19fdbc54b855 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Converters.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Converters.cs @@ -156,7 +156,7 @@ internal static JsonConverter GetConverterForType(Type typeToConvert, JsonSerial 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); diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CustomConverterTests/CustomConverterTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CustomConverterTests/CustomConverterTests.cs index 51c8904f16cfa..fd85163b6cd17 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CustomConverterTests/CustomConverterTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CustomConverterTests/CustomConverterTests.cs @@ -221,5 +221,25 @@ public static void GetConverterTypeToConvertNull() { Assert.Throws(() => (new JsonSerializerOptions()).GetConverter(typeToConvert: null!)); } + + [Fact] + public static void ErrorMessageContainsExpectedType() + { + JsonSerializerOptions options = new(); + options.Converters.Add(new InvalidJsonConverterFactory()); + var ex = Assert.Throws(() => + 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(); + } } }