Skip to content

Commit

Permalink
Fix CanConvert
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasweimann committed Jan 1, 2024
1 parent de0e96a commit 88288d0
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/RxTelegram.Bot/Utils/Converter/MultiTypeClassConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
CheckForValidInterfaces(objectType);
var distinguishingProperty = GetNameOfDistinguishingProperty(objectType);
var jsonObject = JObject.Load(reader);
var valueToDefineResultType =
jsonObject.GetValue(distinguishingProperty) ?? throw new InvalidOperationException("The property is not found");
var valueToDefineResultType = jsonObject.GetValue(distinguishingProperty) ??
throw new InvalidOperationException("The property is not found");
var resultTypeEnum = GetEnumOfGenericInterface(objectType);
var propertyName = ToSnakeCase(valueToDefineResultType.Value<string>());

// Get Attribute for enum entry
var attributeOfEnumValue = resultTypeEnum.GetField(propertyName)
.GetCustomAttributes(typeof(ImplementationTypeAttribute), false)
.Cast<ImplementationTypeAttribute>()
.FirstOrDefault();
.GetCustomAttributes(typeof(ImplementationTypeAttribute), false)
.Cast<ImplementationTypeAttribute>()
.FirstOrDefault();
if (attributeOfEnumValue == null)
{
throw new InvalidOperationException($"The specified type {valueToDefineResultType} is not supported by this converter.");
Expand All @@ -43,17 +43,17 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
private static Type GetEnumOfGenericInterface(Type objectType)
{
var interfaces = objectType.GetInterfaces();
var valueToDefineResultType = interfaces.FirstOrDefault(x => x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(IMultiTypeClassBySource<>)) ??
interfaces.FirstOrDefault(x => x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(IMultiTypeClassByType<>));
var valueToDefineResultType =
interfaces.FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IMultiTypeClassBySource<>)) ??
interfaces.FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IMultiTypeClassByType<>));

var resultTypeEnum = valueToDefineResultType?.GetGenericArguments()
.FirstOrDefault();
if (resultTypeEnum == null)
{
throw new NotSupportedException($"The specified type {valueToDefineResultType} is not supported by this converter.");
}

return resultTypeEnum;
}

Expand Down Expand Up @@ -108,8 +108,11 @@ private static string ToSnakeCase(string str)
return string.Concat(words);
}

public override bool CanConvert(Type objectType) => objectType.GetInterfaces()
public override bool CanConvert(Type objectType) => objectType.IsAbstract &&
objectType.GetInterfaces()
.Any(x => x.IsGenericType &&
x.GetGenericTypeDefinition() ==
typeof(IMultiTypeClassBySource<>));
(x.GetGenericTypeDefinition() ==
typeof(IMultiTypeClassBySource<>) ||
x.GetGenericTypeDefinition() ==
typeof(IMultiTypeClassByType<>)));
}

0 comments on commit 88288d0

Please sign in to comment.