diff --git a/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataSerializerContext.cs b/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataSerializerContext.cs index adf68946f2..df36f83c3e 100644 --- a/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataSerializerContext.cs +++ b/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataSerializerContext.cs @@ -2,7 +2,9 @@ // Licensed under the MIT License. See License.txt in the project root for license information. using System; +using System.Collections; using System.Collections.Generic; +using System.Linq; using Microsoft.AspNet.OData.Common; using Microsoft.AspNet.OData.Interfaces; using Microsoft.OData.Edm; @@ -161,6 +163,29 @@ internal IEdmTypeReference GetEdmType(object instance, Type type) typeof(IEdmObject).Name); } } + else if (typeof(IEnumerable).IsAssignableFrom(type) && + typeof(IEdmObject).IsAssignableFrom(type.GetGenericArguments().FirstOrDefault())) + { + edmType = null; + IEnumerable list = instance as IEnumerable; + if (list != null) + { + IEnumerator enumerator = list.GetEnumerator(); + if (enumerator.MoveNext()) + { + IEdmObject edo = (IEdmObject)enumerator.Current; + IEdmCollectionType edmCollection = new EdmCollectionType(edo.GetEdmType()); + edmType = new EdmCollectionTypeReference(edmCollection); + } + } + + if (edmType == null) + { + Type innerType = type.GetGenericArguments().FirstOrDefault(); + string innerTypeName = innerType == null ? null : innerType.Name; + throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, type.FullName, innerTypeName); + } + } else { if (Model == null)