Fix #84053. (#84070)
authorEirik Tsarpalis <eirik.tsarpalis@gmail.com>
Wed, 29 Mar 2023 14:26:53 +0000 (15:26 +0100)
committerGitHub <noreply@github.com>
Wed, 29 Mar 2023 14:26:53 +0000 (15:26 +0100)
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs
src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.Dictionary.KeyPolicy.cs
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/CollectionTests.cs

index fe4779f..4a5edd1 100644 (file)
@@ -5,7 +5,6 @@ using System.Buffers;
 using System.Collections.Concurrent;
 using System.Diagnostics;
 using System.Globalization;
-using System.Runtime.CompilerServices;
 using System.Text.Encodings.Web;
 
 namespace System.Text.Json.Serialization.Converters
@@ -482,6 +481,10 @@ namespace System.Text.Json.Serialization.Converters
             if (!value.Contains(ValueSeparator))
             {
                 converted = namingPolicy.ConvertName(value);
+                if (converted == null)
+                {
+                    ThrowHelper.ThrowInvalidOperationException_NamingPolicyReturnNull(namingPolicy);
+                }
             }
             else
             {
index 30b88f8..ed7aad9 100644 (file)
@@ -195,7 +195,7 @@ namespace System.Text.Json.Serialization.Tests
         }
       
         [Fact]
-        public void EnumSerialization_DictionaryPolicy_Honored_CamelCase()
+        public async Task EnumSerialization_DictionaryPolicy_Honored_CamelCase()
         {
             var options = new JsonSerializerOptions
             {
@@ -203,31 +203,31 @@ namespace System.Text.Json.Serialization.Tests
             };
 
             Dictionary<ETestEnum, ETestEnum> dict = new Dictionary<ETestEnum, ETestEnum> { [ETestEnum.TestValue1] = ETestEnum.TestValue1 };
-            string value = JsonSerializer.Serialize(dict, options);
+            string value = await Serializer.SerializeWrapper(dict, options);
             Assert.Equal("{\"testValue1\":1}", value);
 
             dict = new Dictionary<ETestEnum, ETestEnum> { [ETestEnum.TestValue2] = ETestEnum.TestValue2 };
-            value = JsonSerializer.Serialize(dict, options);
+            value = await Serializer.SerializeWrapper(dict, options);
             Assert.Equal("{\"testValue2\":2}", value);
 
             dict = new Dictionary<ETestEnum, ETestEnum> { [ETestEnum.TestValue1] = ETestEnum.TestValue1, [ETestEnum.TestValue2] = ETestEnum.TestValue2 };
-            value = JsonSerializer.Serialize(dict, options);
+            value = await Serializer.SerializeWrapper(dict, options);
             Assert.Equal("{\"testValue1\":1,\"testValue2\":2}", value);
         }
 
         [Fact]
-        public void EnumSerializationAsDictKey_NoDictionaryKeyPolicy()
+        public async Task EnumSerializationAsDictKey_NoDictionaryKeyPolicy()
         {
             Dictionary<ETestEnum, ETestEnum> dict = new Dictionary<ETestEnum, ETestEnum> { [ETestEnum.TestValue1] = ETestEnum.TestValue1 };
-            string value = JsonSerializer.Serialize(dict);
+            string value = await Serializer.SerializeWrapper(dict);
             Assert.Equal("{\"TestValue1\":1}", value);
 
             dict = new Dictionary<ETestEnum, ETestEnum> { [ETestEnum.TestValue2] = ETestEnum.TestValue2 };
-            value = JsonSerializer.Serialize(dict);
+            value = await Serializer.SerializeWrapper(dict);
             Assert.Equal("{\"TestValue2\":2}", value);
 
             dict = new Dictionary<ETestEnum, ETestEnum> { [ETestEnum.TestValue1] = ETestEnum.TestValue1, [ETestEnum.TestValue2] = ETestEnum.TestValue2 };
-            value = JsonSerializer.Serialize(dict);
+            value = await Serializer.SerializeWrapper(dict);
             Assert.Equal("{\"TestValue1\":1,\"TestValue2\":2}", value);
         }
 
@@ -238,27 +238,27 @@ namespace System.Text.Json.Serialization.Tests
         }
 
         [Fact]
-        public void EnumSerialization_DictionaryPolicy_NotApplied_WhenEnumsAreSerialized()
+        public async Task EnumSerialization_DictionaryPolicy_NotApplied_WhenEnumsAreSerialized()
         {
             var options = new JsonSerializerOptions
             {
                 DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
             };
 
-            string value = JsonSerializer.Serialize(DayOfWeek.Friday, options);
+            string value = await Serializer.SerializeWrapper(DayOfWeek.Friday, options);
 
             Assert.Equal("5", value);
 
-            value = JsonSerializer.Serialize(ETestEnum.TestValue2, options);
+            value = await Serializer.SerializeWrapper(ETestEnum.TestValue2, options);
 
             Assert.Equal("2", value);
 
 
-            value = JsonSerializer.Serialize(new ClassWithEnumProperties(), options);
+            value = await Serializer.SerializeWrapper(new ClassWithEnumProperties(), options);
 
             Assert.Equal("{\"TestEnumProperty1\":2,\"TestEnumProperty2\":1}", value);
 
-            value = JsonSerializer.Serialize(new List<DayOfWeek> { DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday}, options);
+            value = await Serializer.SerializeWrapper(new List<DayOfWeek> { DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday }, options);
 
             Assert.Equal("[0,1,2,3,4,5,6]", value);
         }
@@ -268,9 +268,8 @@ namespace System.Text.Json.Serialization.Tests
             public override string ConvertName(string name) => null;
         }
 
-        [ActiveIssue("https://github.com/dotnet/runtime/issues/84053")]
         [Fact]
-        public void EnumSerialization_DictionaryPolicy_ThrowsException_WhenNamingPolicyReturnsNull()
+        public async Task EnumSerialization_DictionaryPolicy_ThrowsException_WhenNamingPolicyReturnsNull()
         {
             var options = new JsonSerializerOptions
             {
@@ -279,7 +278,7 @@ namespace System.Text.Json.Serialization.Tests
 
             Dictionary<ETestEnum, ETestEnum> dict = new Dictionary<ETestEnum, ETestEnum> { [ETestEnum.TestValue1] = ETestEnum.TestValue1 };
 
-            InvalidOperationException ex = Assert.Throws<InvalidOperationException>(() => JsonSerializer.Serialize(dict, options));
+            InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(dict, options));
 
             Assert.Contains(typeof(CustomJsonNamingPolicy).ToString(), ex.Message);
         }
index 3baeb31..1f82ada 100644 (file)
@@ -368,6 +368,10 @@ namespace System.Text.Json.SourceGeneration.Tests
         [JsonSerializable(typeof(SimpleTestClassWithStringIReadOnlyListWrapper))]
         [JsonSerializable(typeof(SimpleTestClassWithStringToStringIReadOnlyDictionaryWrapper))]
         [JsonSerializable(typeof(Dictionary<string, int?>))]
+        [JsonSerializable(typeof(Dictionary<ETestEnum, ETestEnum>))]
+        [JsonSerializable(typeof(ClassWithEnumProperties))]
+        [JsonSerializable(typeof(DayOfWeek))]
+        [JsonSerializable(typeof(List<DayOfWeek>))]
         [JsonSerializable(typeof(GenericICollectionWrapper<GenericICollectionWrapper<string>>))]
         [JsonSerializable(typeof(GenericIEnumerableWrapperPrivateConstructor<string>))]
         [JsonSerializable(typeof(GenericIEnumerableWrapperInternalConstructor<string>))]
@@ -766,6 +770,10 @@ namespace System.Text.Json.SourceGeneration.Tests
         [JsonSerializable(typeof(SimpleTestClassWithStringIReadOnlyListWrapper))]
         [JsonSerializable(typeof(SimpleTestClassWithStringToStringIReadOnlyDictionaryWrapper))]
         [JsonSerializable(typeof(Dictionary<string, int?>))]
+        [JsonSerializable(typeof(Dictionary<ETestEnum, ETestEnum>))]
+        [JsonSerializable(typeof(ClassWithEnumProperties))]
+        [JsonSerializable(typeof(DayOfWeek))]
+        [JsonSerializable(typeof(List<DayOfWeek>))]
         [JsonSerializable(typeof(GenericICollectionWrapper<GenericICollectionWrapper<string>>))]
         [JsonSerializable(typeof(GenericIEnumerableWrapperPrivateConstructor<string>))]
         [JsonSerializable(typeof(GenericIEnumerableWrapperInternalConstructor<string>))]