fix: Do not run ForceEmptySequences on string types

This happens when there's an empty `base_url:` in YAML. It comes in as a
blank string.
pull/231/head
Robert Dailey 11 months ago
parent 258949c0aa
commit 1fc041253e

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

@ -4,4 +4,7 @@
<PackageReference Include="JetBrains.Annotations" />
<PackageReference Include="YamlDotNet" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Recyclarr.Common\Recyclarr.Common.csproj" />
</ItemGroup>
</Project>

@ -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<NodeEvent>(out var evt) || !NodeIsNull(evt))
if (!IsList(expectedType) || !reader.Accept<NodeEvent>(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<>));
}
}

Loading…
Cancel
Save