Skip to content

Commit

Permalink
Fix #84053. (#84070)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis committed Mar 29, 2023
1 parent 7e8f438 commit 4f2397f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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
Expand Down Expand Up @@ -482,6 +481,10 @@ private static string FormatJsonName(string value, JsonNamingPolicy? namingPolic
if (!value.Contains(ValueSeparator))
{
converted = namingPolicy.ConvertName(value);
if (converted == null)
{
ThrowHelper.ThrowInvalidOperationException_NamingPolicyReturnNull(namingPolicy);
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,39 +195,39 @@ public enum ETestEnum
}

[Fact]
public void EnumSerialization_DictionaryPolicy_Honored_CamelCase()
public async Task EnumSerialization_DictionaryPolicy_Honored_CamelCase()
{
var options = new JsonSerializerOptions
{
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
};

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);
}

Expand All @@ -238,27 +238,27 @@ public class ClassWithEnumProperties
}

[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);
}
Expand All @@ -268,9 +268,8 @@ public class CustomJsonNamingPolicy : JsonNamingPolicy
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
{
Expand All @@ -279,7 +278,7 @@ public void EnumSerialization_DictionaryPolicy_ThrowsException_WhenNamingPolicyR

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ public async Task DeserializeAsyncEnumerable()
[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>))]
Expand Down Expand Up @@ -766,6 +770,10 @@ public CollectionTests_Default()
[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>))]
Expand Down

0 comments on commit 4f2397f

Please sign in to comment.