diff --git a/NzbDrone.Core.Test/Datastore/MappingExtentionFixture.cs b/NzbDrone.Core.Test/Datastore/MappingExtentionFixture.cs new file mode 100644 index 000000000..4324e5aef --- /dev/null +++ b/NzbDrone.Core.Test/Datastore/MappingExtentionFixture.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.Test.Datastore +{ + [TestFixture] + public class MappingExtensionFixture + { + + public class EmbeddedType : IEmbeddedDocument + { + + } + + public class TypeWithAllMappableProperties + { + public string PropString { get; set; } + public int PropInt { get; set; } + public bool PropBool { get; set; } + public Nullable PropNullable { get; set; } + public EmbeddedType Embedded { get; set; } + public List EmbeddedList { get; set; } + } + + public class TypeWithNoMappableProperties + { + public Series Series { get; set; } + public List ListOfStrings { get; set; } + + public int ReadOnly { get; private set; } + public int WriteOnly { private get; set; } + } + + + [Test] + public void test_mappable_types() + { + var properties = typeof(TypeWithAllMappableProperties).GetProperties(); + properties.Should().NotBeEmpty(); + properties.Should().OnlyContain(c => MappingExtensions.IsMappableProperty(c)); + } + + [Test] + public void test_un_mappable_types() + { + var properties = typeof(TypeWithNoMappableProperties).GetProperties(); + properties.Should().NotBeEmpty(); + properties.Should().NotContain(c => MappingExtensions.IsMappableProperty(c)); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index e7fadaa39..e9ff43b01 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -139,6 +139,7 @@ + diff --git a/NzbDrone.Core/Datastore/Converters/EmbeddedDocumentConverter.cs b/NzbDrone.Core/Datastore/Converters/EmbeddedDocumentConverter.cs index 4e72af814..e71fb3954 100644 --- a/NzbDrone.Core/Datastore/Converters/EmbeddedDocumentConverter.cs +++ b/NzbDrone.Core/Datastore/Converters/EmbeddedDocumentConverter.cs @@ -28,11 +28,6 @@ namespace NzbDrone.Core.Datastore.Converters { if (clrValue == null) return null; - if (clrValue as IEmbeddedDocument == null) - { - throw new InvalidOperationException("Attempted to embedded an object not marked with IEmbeddedDocument"); - } - var json = JsonConvert.SerializeObject(clrValue); return json; } diff --git a/NzbDrone.Core/Datastore/MappingExtensions.cs b/NzbDrone.Core/Datastore/MappingExtensions.cs index bb5cfb3e8..7dc1d69c4 100644 --- a/NzbDrone.Core/Datastore/MappingExtensions.cs +++ b/NzbDrone.Core/Datastore/MappingExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using Marr.Data.Mapping; @@ -29,7 +30,14 @@ namespace NzbDrone.Core.Datastore return true; } - return propertyInfo.CanRead && propertyInfo.CanWrite && IsSimpleType(propertyInfo.PropertyType); + if (typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType)) + { + return true; + } + + var result = propertyInfo.IsReadable() && propertyInfo.IsWritable() && IsSimpleType(propertyInfo.PropertyType); + + return result; } public static bool IsSimpleType(Type type) @@ -45,5 +53,15 @@ namespace NzbDrone.Core.Datastore || type == typeof(DateTime) || type == typeof(Decimal); } + + private static bool IsReadable(this PropertyInfo propertyInfo) + { + return propertyInfo.CanRead && propertyInfo.GetGetMethod(false) != null; + } + + private static bool IsWritable(this PropertyInfo propertyInfo) + { + return propertyInfo.CanWrite && propertyInfo.GetSetMethod(false) != null; + } } } \ No newline at end of file