Issue 83667 (#83820)
authorcareri <carl.ericsson@gmail.com>
Mon, 27 Mar 2023 17:06:09 +0000 (19:06 +0200)
committerGitHub <noreply@github.com>
Mon, 27 Mar 2023 17:06:09 +0000 (18:06 +0100)
* Fixed devcontainer

* Issue-83667 Fixed misleading exception message

* issue-83667 Fixed test

* issue-83667 Reverted .devcontainer fix

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Converters.cs
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CustomConverterTests/CustomConverterTests.cs

index 152b84e..b19fdbc 100644 (file)
@@ -156,7 +156,7 @@ namespace System.Text.Json.Serialization.Metadata
             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);
index 51c8904..fd85163 100644 (file)
@@ -221,5 +221,25 @@ namespace System.Text.Json.Serialization.Tests
         {
             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();
+        }
     }
 }