Skip to content

Commit d08cff4

Browse files
committed
Reflect item type when serializing empty collection
1 parent 0e16c00 commit d08cff4

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

Saleslogix.SData.Client.Test/Content/ContentHelperTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,5 +751,36 @@ public object this[string name]
751751
set { throw new NotSupportedException(); }
752752
}
753753
}
754+
755+
[Test]
756+
public void Serialize_Empty_Collection_String_Test()
757+
{
758+
var value = new {items = new List<string>()};
759+
var result = (IDictionary<string, object>) ContentHelper.Serialize(value);
760+
Assert.That(result, Is.Not.Null);
761+
Assert.That(result["items"], Is.InstanceOf<SDataCollection<string>>());
762+
}
763+
764+
[Test]
765+
public void Serialize_Empty_Collection_Object_Test()
766+
{
767+
var value = new {items = new List<object>()};
768+
var result = (IDictionary<string, object>) ContentHelper.Serialize(value);
769+
Assert.That(result, Is.Not.Null);
770+
Assert.That(result["items"], Is.InstanceOf<SDataCollection<object>>());
771+
}
772+
773+
[Test]
774+
public void Serialize_Empty_Collection_Resource_Test()
775+
{
776+
var value = new {items = new List<Serialize_Empty_Collection_Resource_Object>()};
777+
var result = (IDictionary<string, object>) ContentHelper.Serialize(value);
778+
Assert.That(result, Is.Not.Null);
779+
Assert.That(result["items"], Is.InstanceOf<SDataCollection<SDataResource>>());
780+
}
781+
782+
private class Serialize_Empty_Collection_Resource_Object
783+
{
784+
}
754785
}
755786
}

Saleslogix.SData.Client/Content/ContentHelper.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace Saleslogix.SData.Client.Content
1919
{
20-
internal static class ContentHelper
20+
public static class ContentHelper
2121
{
2222
private static readonly IDictionary<Type, IDictionary<SDataProtocolProperty, ReflectionUtils.GetDelegate>> _getProtocolValueCache = new ReflectionUtils.ThreadSafeDictionary<Type, IDictionary<SDataProtocolProperty, ReflectionUtils.GetDelegate>>(ProtocolValueGetterFactory);
2323
private static readonly IDictionary<Type, IDictionary<SDataProtocolProperty, ReflectionUtils.SetDelegate>> _setProtocolValueCache = new ReflectionUtils.ThreadSafeDictionary<Type, IDictionary<SDataProtocolProperty, ReflectionUtils.SetDelegate>>(ProtocolValueSetterFactory);
@@ -484,7 +484,22 @@ private bool TrySerializeCollection(IEnumerable<object> input, out object output
484484
results.Add(result);
485485
}
486486

487-
output = ToTypedCollection(results);
487+
if (results.Count == 0)
488+
{
489+
var itemType = input.GetType().GetInterfaces()
490+
.Where(iface => iface.GetTypeInfo().IsGenericType && iface.GetGenericTypeDefinition() == typeof (IEnumerable<>))
491+
.Select(iface => iface.GetGenericArguments()[0])
492+
.FirstOrDefault() ?? typeof (object);
493+
if (itemType != typeof (object) && IsObject(itemType))
494+
{
495+
itemType = typeof (SDataResource);
496+
}
497+
output = Activator.CreateInstance(typeof (SDataCollection<>).MakeGenericType(itemType));
498+
}
499+
else
500+
{
501+
output = ToTypedCollection(results);
502+
}
488503

489504
var prot = input as ISDataProtocolObject;
490505
if (prot != null)

0 commit comments

Comments
 (0)