-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix missing Blockquote enum value ( #31 ) & Add handling for missing …
…enum values in the future
- Loading branch information
1 parent
620eb47
commit 384ba33
Showing
6 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/RxTelegram.Bot/Utils/Converter/UnknownStringEnumConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
namespace RxTelegram.Bot.Utils.Converter; | ||
|
||
public class UnknownStringEnumConverter(NamingStrategy namingStrategy) : StringEnumConverter(namingStrategy) | ||
{ | ||
public override object ReadJson(JsonReader reader, Type enumType, object existingValue, JsonSerializer serializer) | ||
{ | ||
try | ||
{ | ||
return base.ReadJson(reader, enumType, existingValue, serializer); | ||
} | ||
catch (Exception) when (enumType.IsEnum || | ||
enumType.IsGenericType && | ||
enumType.GetGenericTypeDefinition() == typeof(Nullable<>) && | ||
enumType.GenericTypeArguments[0].IsEnum) | ||
{ | ||
// Return null if it is a nullable enum and -1 if it is an enum to ensure that new values do not brake the bot | ||
return reader.TokenType switch | ||
{ | ||
JsonToken.Integer => Enum.ToObject(enumType, -1), | ||
JsonToken.Null => null, | ||
JsonToken.None => null, | ||
JsonToken.String when enumType.IsGenericType && enumType.GetGenericTypeDefinition() == typeof(Nullable<>) => null, | ||
JsonToken.String => Enum.ToObject(enumType, -1), | ||
_ => throw new ArgumentOutOfRangeException() | ||
}; | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/UnitTests/JsonConverters/UnknownStringEnumConverterTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
using NUnit.Framework; | ||
using RxTelegram.Bot.Interface.BaseTypes.Enums; | ||
using RxTelegram.Bot.Utils.Converter; | ||
|
||
namespace RxTelegram.Bot.UnitTests.JsonConverters; | ||
|
||
[TestFixture] | ||
public class UnknownStringEnumConverterTest | ||
{ | ||
private readonly JsonSerializerSettings _jsonSettings = new() | ||
{ | ||
Converters = | ||
{ | ||
new | ||
UnknownStringEnumConverter(new | ||
SnakeCaseNamingStrategy()) | ||
} | ||
}; | ||
|
||
[Test] | ||
[TestCase(@"""blockquote""", MessageEntityType.Blockquote)] | ||
[TestCase(@"""Pre""", MessageEntityType.Pre)] | ||
[TestCase(@"""code""", MessageEntityType.Code)] | ||
[TestCase(@"""mention""", MessageEntityType.Mention)] | ||
[TestCase(@"""url""", MessageEntityType.Url)] | ||
[TestCase(@"""email""", MessageEntityType.Email)] | ||
[TestCase(@"""bold""", MessageEntityType.Bold)] | ||
[TestCase(@"""italic""", MessageEntityType.Italic)] | ||
[TestCase(@"""spoiler""", MessageEntityType.Spoiler)] | ||
[TestCase(@"""hashtag""", MessageEntityType.Hashtag)] | ||
[TestCase(@"""bot_command""", MessageEntityType.BotCommand)] | ||
[TestCase(@"""custom_emoji""", MessageEntityType.CustomEmoji)] | ||
[TestCase(@"""text_link""", MessageEntityType.TextLink)] | ||
[TestCase(@"""text_mention""", MessageEntityType.TextMention)] | ||
[TestCase(@"""unknown""", MessageEntityType.Unknown)] | ||
[TestCase(@"""doesnt_exist""", null)] | ||
[TestCase("", null)] | ||
[TestCase(" ", null)] | ||
public void ReadJson_WithUnknownStringEnum_ReturnsEnumValueOrNull(string json, MessageEntityType? expected) | ||
{ | ||
// Act | ||
var result = JsonConvert.DeserializeObject<MessageEntityType?>(json, _jsonSettings); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expected)); | ||
} | ||
|
||
[Test] | ||
[TestCase(@"""blockquote""", (int)MessageEntityType.Blockquote)] | ||
[TestCase(@"""Pre""", (int)MessageEntityType.Pre)] | ||
[TestCase(@"""code""", (int)MessageEntityType.Code)] | ||
[TestCase(@"""mention""", (int)MessageEntityType.Mention)] | ||
[TestCase(@"""url""", (int)MessageEntityType.Url)] | ||
[TestCase(@"""email""", (int)MessageEntityType.Email)] | ||
[TestCase(@"""bold""", (int)MessageEntityType.Bold)] | ||
[TestCase(@"""italic""", (int)MessageEntityType.Italic)] | ||
[TestCase(@"""spoiler""", (int)MessageEntityType.Spoiler)] | ||
[TestCase(@"""hashtag""", (int)MessageEntityType.Hashtag)] | ||
[TestCase(@"""bot_command""", (int)MessageEntityType.BotCommand)] | ||
[TestCase(@"""custom_emoji""", (int)MessageEntityType.CustomEmoji)] | ||
[TestCase(@"""text_link""", (int)MessageEntityType.TextLink)] | ||
[TestCase(@"""text_mention""", (int)MessageEntityType.TextMention)] | ||
[TestCase(@"""unknown""", (int)MessageEntityType.Unknown)] | ||
[TestCase(@"""doesnt_exist""", -1)] | ||
public void ReadJson_WithUnknownStringEnum_ReturnsEnumValueOrNegativeValue(string json, int expected) | ||
{ | ||
// Act | ||
var result = JsonConvert.DeserializeObject<MessageEntityType>(json, _jsonSettings); | ||
|
||
// Assert | ||
Assert.That((int)result, Is.EqualTo(expected)); | ||
} | ||
} |