diff --git a/src/Recyclarr.Common/Extensions/TypeExtensions.cs b/src/Recyclarr.Common/Extensions/TypeExtensions.cs new file mode 100644 index 00000000..2fd9c219 --- /dev/null +++ b/src/Recyclarr.Common/Extensions/TypeExtensions.cs @@ -0,0 +1,16 @@ +namespace Recyclarr.Common.Extensions; + +public static class TypeExtensions +{ + public static bool IsGenericTypeOf(this Type type, Type genericType) + { + return type is {IsGenericType: true} && type.GetGenericTypeDefinition() == genericType; + } + + public static bool IsImplementationOf(this Type type, Type collectionType) + { + return + type is {IsInterface: true} && type.IsGenericTypeOf(collectionType) || + type.GetInterfaces().Any(i => i.IsGenericTypeOf(typeof(ICollection<>))); + } +} diff --git a/src/Recyclarr.Yaml/Recyclarr.Yaml.csproj b/src/Recyclarr.Yaml/Recyclarr.Yaml.csproj index 229ab5f6..67d5bbca 100644 --- a/src/Recyclarr.Yaml/Recyclarr.Yaml.csproj +++ b/src/Recyclarr.Yaml/Recyclarr.Yaml.csproj @@ -4,4 +4,7 @@ + + + diff --git a/src/Recyclarr.Yaml/YamlDotNet/ForceEmptySequences.cs b/src/Recyclarr.Yaml/YamlDotNet/ForceEmptySequences.cs index aa1c6a60..d91c4978 100644 --- a/src/Recyclarr.Yaml/YamlDotNet/ForceEmptySequences.cs +++ b/src/Recyclarr.Yaml/YamlDotNet/ForceEmptySequences.cs @@ -1,4 +1,4 @@ -using System.Collections; +using Recyclarr.Common.Extensions; using YamlDotNet.Core; using YamlDotNet.Core.Events; using YamlDotNet.Serialization; @@ -16,7 +16,7 @@ public sealed class ForceEmptySequences(IObjectFactory objectFactory) : INodeDes { value = null; - if (!IsEnumerable(expectedType) || !reader.Accept(out var evt) || !NodeIsNull(evt)) + if (!IsList(expectedType) || !reader.Accept(out var evt) || !NodeIsNull(evt)) { return false; } @@ -44,8 +44,10 @@ public sealed class ForceEmptySequences(IObjectFactory objectFactory) : INodeDes return value is "" or "~" or "null" or "Null" or "NULL"; } - private static bool IsEnumerable(Type type) + private static bool IsList(Type type) { - return typeof(IEnumerable).IsAssignableFrom(type); + return + type.IsImplementationOf(typeof(ICollection<>)) || + type.IsImplementationOf(typeof(IReadOnlyCollection<>)); } }