Skip to content

Commit

Permalink
Improve MultiTypeClassConverter to support null types and multi word …
Browse files Browse the repository at this point in the history
…enum values
  • Loading branch information
niklasweimann committed May 19, 2024
1 parent 466902c commit 5c6507d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/RxTelegram.Bot/Utils/Converter/MultiTypeClassConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
CheckForValidInterfaces(objectType);
var distinguishingProperty = GetNameOfDistinguishingProperty(objectType);
var jsonObject = JObject.Load(reader);
if (!jsonObject.HasValues)
{
return null;
}
jsonObject[distinguishingProperty] = ToPascalCaseIfNecessary(jsonObject[distinguishingProperty]
?.ToString());
var valueToDefineResultType = jsonObject.GetValue(distinguishingProperty) ??
throw new InvalidOperationException("The property is not found");
var resultTypeEnum = GetEnumOfGenericInterface(objectType);
Expand All @@ -40,6 +46,15 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
return objectInstance;
}

private static JToken ToPascalCaseIfNecessary(string toString)
{
var words = toString.Split(new[] { '-', '_' }, StringSplitOptions.RemoveEmptyEntries)
.Select(word => word.Substring(0, 1).ToUpper() +
word.Substring(1).ToLower());

return string.Concat(words);
}

private static Type GetEnumOfGenericInterface(Type objectType)
{
var interfaces = objectType.GetInterfaces();
Expand Down
56 changes: 56 additions & 0 deletions src/UnitTests/JsonConverters/BackgroundTypeConverterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using Newtonsoft.Json;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using RxTelegram.Bot.Interface.ChatBackground;
using RxTelegram.Bot.Utils.Converter;

namespace RxTelegram.Bot.UnitTests.JsonConverters;

[TestFixture]
public class BackgroundTypeConverterTest
{
private readonly JsonSerializerSettings _jsonSettings = new()
{
Converters = { new MultiTypeClassConverter() }
};

[Test]
[TestCase("chat_theme", typeof(BackgroundTypeChatTheme))]
[TestCase("fill", typeof(BackgroundTypeFill))]
[TestCase("pattern", typeof(BackgroundTypePattern))]
[TestCase("wallpaper", typeof(BackgroundTypeWallpaper))]
public void CanRead(string type, Type expectedType)
{
var json = $"{{\"type\":\"{type}\"}}";
var actual = JsonConvert.DeserializeObject<BackgroundType>(json, _jsonSettings);
Assert.That(actual, Is.Not.Null);
Assert.That(actual.GetType(), Is.EqualTo(expectedType));
}

[Test]
public void CanReadNull()
{
const string json = "{}";
var actual = JsonConvert.DeserializeObject<BackgroundType>(json, _jsonSettings);
Assert.That(actual, Is.Null);
}

[Test]
[TestCase(typeof(BackgroundFillFreeformGradient), 0)]
[TestCase(typeof(BackgroundFillGradient), 1)]
[TestCase(typeof(BackgroundFillSolid), 2)]
public void CanWrite(Type type, int expectedType)
{
var instance = Activator.CreateInstance(type);
var json = JsonConvert.SerializeObject(instance, _jsonSettings);
Assert.That(json, new StartsWithConstraint($"{{\"Type\":{expectedType}"));
}

[Test]
public void CanWriteNull()
{
var json = JsonConvert.SerializeObject(null, _jsonSettings);
Assert.That(json, Is.EqualTo("null"));
}
}

0 comments on commit 5c6507d

Please sign in to comment.