Fix dotnet/corefx#37506: System.Text.Json fails to parse enums with negative values...
authorPaul Buonopane <Zenexer@users.noreply.github.com>
Thu, 9 May 2019 03:45:57 +0000 (23:45 -0400)
committerAhson Khan <ahkha@microsoft.com>
Thu, 9 May 2019 03:45:57 +0000 (20:45 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/870cb984d5e3024fcf6ad67ff363ec6be1bff476

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterEnum.cs
src/libraries/System.Text.Json/tests/Serialization/EnumTests.cs
src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClass.cs
src/libraries/System.Text.Json/tests/Serialization/TestClasses.cs

index b99dcc9..3778957 100644 (file)
@@ -33,15 +33,28 @@ namespace System.Text.Json.Serialization.Converters
                 return Enum.TryParse(enumString, out value);
             }
 
-            if (reader.TokenType != JsonTokenType.Number ||
-                !reader.TryGetUInt64(out ulong ulongValue))
+            if (reader.TokenType != JsonTokenType.Number)
             {
                 value = default;
                 return false;
             }
 
-            value = (TValue)Enum.ToObject(valueType, ulongValue);
-            return true;
+            if (s_isUint64)
+            {
+                if (reader.TryGetUInt64(out ulong ulongValue))
+                {
+                    value = (TValue)Enum.ToObject(valueType, ulongValue);
+                    return true;
+                }
+            }
+            else if (reader.TryGetInt64(out long longValue))
+            {
+                value = (TValue)Enum.ToObject(valueType, longValue);
+                return true;
+            }
+
+            value = default;
+            return false;
         }
 
         public override void Write(TValue value, Utf8JsonWriter writer)
index d541456..79a376a 100644 (file)
@@ -18,6 +18,21 @@ namespace System.Text.Json.Serialization.Tests
                 @"""MyEnum"" : 2" +
                 @"}";
 
+        private static readonly string s_jsonInt64EnumMin =
+                @"{" +
+                @"""MyInt64Enum"" : " + long.MinValue +
+                @"}";
+
+        private static readonly string s_jsonInt64EnumMax =
+                @"{" +
+                @"""MyInt64Enum"" : " + long.MaxValue +
+                @"}";
+
+        private static readonly string s_jsonUInt64EnumMax =
+                @"{" +
+                @"""MyUInt64Enum"" : " + ulong.MaxValue +
+                @"}";
+
         [Fact]
         public static void EnumAsStringFail()
         {
@@ -30,5 +45,26 @@ namespace System.Text.Json.Serialization.Tests
             SimpleTestClass obj = JsonSerializer.Parse<SimpleTestClass>(s_jsonIntEnum);
             Assert.Equal(SampleEnum.Two, obj.MyEnum);
         }
+
+        [Fact]
+        public static void EnumAsInt64Min()
+        {
+            SimpleTestClass obj = JsonSerializer.Parse<SimpleTestClass>(s_jsonInt64EnumMin);
+            Assert.Equal(SampleInt64Enum.Min, obj.MyInt64Enum);
+        }
+
+        [Fact]
+        public static void EnumAsInt64Max()
+        {
+            SimpleTestClass obj = JsonSerializer.Parse<SimpleTestClass>(s_jsonInt64EnumMax);
+            Assert.Equal(SampleInt64Enum.Max, obj.MyInt64Enum);
+        }
+
+        [Fact]
+        public static void EnumAsUInt64Max()
+        {
+            SimpleTestClass obj = JsonSerializer.Parse<SimpleTestClass>(s_jsonUInt64EnumMax);
+            Assert.Equal(SampleUInt64Enum.Max, obj.MyUInt64Enum);
+        }
     }
 }
index d568644..8edbc63 100644 (file)
@@ -28,6 +28,8 @@ namespace System.Text.Json.Serialization.Tests
         public DateTime MyDateTime { get; set; }
         public DateTimeOffset MyDateTimeOffset { get; set; }
         public SampleEnum MyEnum { get; set; }
+        public SampleInt64Enum MyInt64Enum { get; set; }
+        public SampleUInt64Enum MyUInt64Enum { get; set; }
         public short[] MyInt16Array { get; set; }
         public int[] MyInt32Array { get; set; }
         public long[] MyInt64Array { get; set; }
index cb7c4e2..380fbab 100644 (file)
@@ -20,6 +20,17 @@ namespace System.Text.Json.Serialization.Tests
         Two = 2
     }
 
+    public enum SampleInt64Enum : long
+    {
+        Min = long.MinValue,
+        Max = long.MaxValue
+    }
+
+    public enum SampleUInt64Enum : ulong
+    {
+        Max = ulong.MaxValue
+    }
+
     public class TestClassWithNull
     {
         public string MyString { get; set; }