From 66db88cbc7da29d023220c421dba0a622a8739c2 Mon Sep 17 00:00:00 2001 From: Michael Roth Date: Tue, 4 Sep 2018 11:15:08 -0700 Subject: [PATCH 1/2] Handle untyped collections in context's getedmtype method --- .../Serialization/ODataSerializerContext.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataSerializerContext.cs b/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataSerializerContext.cs index adf68946f2..384fbb253f 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,22 @@ internal IEdmTypeReference GetEdmType(object instance, Type type) typeof(IEdmObject).Name); } } + else if (typeof(IEnumerable).IsAssignableFrom(type) && + typeof(IEdmObject).IsAssignableFrom(type.GetGenericArguments().FirstOrDefault())) + { + IEnumerable list = instance as IEnumerable; + IEnumerator enumerator = list.GetEnumerator(); + if (enumerator.MoveNext()) + { + IEdmObject edo = (IEdmObject)enumerator.Current; + IEdmCollectionType edmCollection = new EdmCollectionType(edo.GetEdmType()); + edmType = new EdmCollectionTypeReference(edmCollection); + } + else + { + edmType = null; + } + } else { if (Model == null) From c211f84b82cb8de092f1e971672d2a4f5b86dfb3 Mon Sep 17 00:00:00 2001 From: Michael Roth Date: Wed, 5 Sep 2018 08:49:08 -0700 Subject: [PATCH 2/2] handle for null reference --- .../Serialization/ODataSerializerContext.cs | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataSerializerContext.cs b/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataSerializerContext.cs index 384fbb253f..df36f83c3e 100644 --- a/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataSerializerContext.cs +++ b/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataSerializerContext.cs @@ -166,17 +166,24 @@ internal IEdmTypeReference GetEdmType(object instance, Type type) else if (typeof(IEnumerable).IsAssignableFrom(type) && typeof(IEdmObject).IsAssignableFrom(type.GetGenericArguments().FirstOrDefault())) { + edmType = null; IEnumerable list = instance as IEnumerable; - IEnumerator enumerator = list.GetEnumerator(); - if (enumerator.MoveNext()) + if (list != null) { - IEdmObject edo = (IEdmObject)enumerator.Current; - IEdmCollectionType edmCollection = new EdmCollectionType(edo.GetEdmType()); - edmType = new EdmCollectionTypeReference(edmCollection); + IEnumerator enumerator = list.GetEnumerator(); + if (enumerator.MoveNext()) + { + IEdmObject edo = (IEdmObject)enumerator.Current; + IEdmCollectionType edmCollection = new EdmCollectionType(edo.GetEdmType()); + edmType = new EdmCollectionTypeReference(edmCollection); + } } - else + + if (edmType == null) { - edmType = null; + Type innerType = type.GetGenericArguments().FirstOrDefault(); + string innerTypeName = innerType == null ? null : innerType.Name; + throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, type.FullName, innerTypeName); } } else