Ensure JsonException is thrown parsing invalid DateTime, DateTimeOffset, and Guid...
authorChristopher Watford <christopher.watford@ge.com>
Mon, 20 May 2019 19:11:17 +0000 (15:11 -0400)
committerAhson Khan <ahkha@microsoft.com>
Mon, 20 May 2019 19:11:17 +0000 (12:11 -0700)
* Add tests to cover dotnet/corefx#36901 and dotnet/corefx#37807

* Ensure DateTime(Offset) and Guid throw JsonException dotnet/corefx#37807

- The original JsonValueConverterXXX code for these allowed an InvalidOperationException
to be raised from Utf8JsonReader.TryGetXXX, instead of returning false so a JsonException
could be thrown.

Commit migrated from https://github.com/dotnet/corefx/commit/3ff5c4a6eb6315c61cfb0078245c31b59f798290

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterDateTime.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterDateTimeOffset.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterGuid.cs
src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.cs

index f15470c..9abbfa7 100644 (file)
@@ -10,6 +10,12 @@ namespace System.Text.Json.Serialization.Converters
     {
         public override bool TryRead(Type valueType, ref Utf8JsonReader reader, out DateTime value)
         {
+            if (reader.TokenType != JsonTokenType.String)
+            {
+                value = default;
+                return false;
+            }
+
             return reader.TryGetDateTime(out value);
         }
 
index 0f9d6d3..0567bfb 100644 (file)
@@ -10,6 +10,12 @@ namespace System.Text.Json.Serialization.Converters
     {
         public override bool TryRead(Type valueType, ref Utf8JsonReader reader, out DateTimeOffset value)
         {
+            if (reader.TokenType != JsonTokenType.String)
+            {
+                value = default;
+                return false;
+            }
+
             return reader.TryGetDateTimeOffset(out value);
         }
 
index 5e5809c..c249e36 100644 (file)
@@ -10,6 +10,12 @@ namespace System.Text.Json.Serialization.Converters
     {
         public override bool TryRead(Type valueType, ref Utf8JsonReader reader, out Guid value)
         {
+            if (reader.TokenType != JsonTokenType.String)
+            {
+                value = default;
+                return false;
+            }
+
             return reader.TryGetGuid(out value);
         }
 
index ae223fc..a7fe869 100644 (file)
@@ -74,6 +74,32 @@ namespace System.Text.Json.Serialization.Tests
             Assert.Throws<JsonException>(() => JsonSerializer.Parse<int>(@""""""));
         }
 
+        [Theory]
+        [InlineData(typeof(bool))]
+        [InlineData(typeof(byte))]
+        [InlineData(typeof(char))]
+        [InlineData(typeof(DateTime))]
+        [InlineData(typeof(DateTimeOffset))]
+        [InlineData(typeof(decimal))]
+        [InlineData(typeof(double))]
+        [InlineData(typeof(JsonTokenType))]
+        [InlineData(typeof(Guid))]
+        [InlineData(typeof(short))]
+        [InlineData(typeof(int))]
+        [InlineData(typeof(long))]
+        [InlineData(typeof(sbyte))]
+        [InlineData(typeof(float))]
+        [InlineData(typeof(string))]
+        [InlineData(typeof(ushort))]
+        [InlineData(typeof(uint))]
+        [InlineData(typeof(ulong))]
+        public static void PrimitivesShouldFailWithArrayOrObjectAssignment(Type primitiveType)
+        {
+            // This test lines up with the built in JsonValueConverters
+            Assert.Throws<JsonException>(() => JsonSerializer.Parse(@"[]", primitiveType));
+            Assert.Throws<JsonException>(() => JsonSerializer.Parse(@"{}", primitiveType));
+        }
+
         [Fact]
         public static void ReadPrimitiveArray()
         {