Avoid calling ctor on abstract type during deserialization (dotnet/corefx#39481)
authorSteve Harter <steveharter@users.noreply.github.com>
Mon, 15 Jul 2019 18:52:41 +0000 (11:52 -0700)
committerGitHub <noreply@github.com>
Mon, 15 Jul 2019 18:52:41 +0000 (11:52 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/a7efd2a2b3c638b5490547ad6beca128ba5d7bfe

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMemberAccessor.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMemberAccessor.cs
src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.cs

index 1bc9d97..f41f7cb 100644 (file)
@@ -16,6 +16,11 @@ namespace System.Text.Json
             Debug.Assert(type != null);
             ConstructorInfo realMethod = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, binder: null, Type.EmptyTypes, modifiers: null);
 
+            if (type.IsAbstract)
+            {
+                return null;
+            }
+
             if (realMethod == null && !type.IsValueType)
             {
                 return null;
index b152ddc..d5afac2 100644 (file)
@@ -29,6 +29,11 @@ namespace System.Text.Json
             Debug.Assert(type != null);
             ConstructorInfo realMethod = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, binder: null, Type.EmptyTypes, modifiers: null);
 
+            if (type.IsAbstract)
+            {
+                return null;
+            }
+
             if (realMethod == null && !type.IsValueType)
             {
                 return null;
index 4e5d830..2838a6e 100644 (file)
@@ -283,6 +283,7 @@ namespace System.Text.Json.Serialization.Tests
             Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<char>("1"));
             Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<char?>("1"));
 
+            // This throws because Enum is an abstract type.
             Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Enum>(unexpectedString));
         }