diff --git a/AutoMapper.AspNetCore.OData.EF6/QueryableExtensions.cs b/AutoMapper.AspNetCore.OData.EF6/QueryableExtensions.cs index 5f9c0c8..02d08ba 100644 --- a/AutoMapper.AspNetCore.OData.EF6/QueryableExtensions.cs +++ b/AutoMapper.AspNetCore.OData.EF6/QueryableExtensions.cs @@ -169,7 +169,7 @@ private static IQueryable GetQueryable(this IQueryable> filter) where TModel : class { - var expansions = options.SelectExpand.GetExpansions(typeof(TModel)); + var expansions = options.SelectExpand.GetExpansions(typeof(TModel), options.Context.Model); return query.GetQuery ( diff --git a/AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs b/AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs index cb6178b..a2e627f 100644 --- a/AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs +++ b/AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs @@ -11,6 +11,9 @@ namespace AutoMapper.AspNet.OData { + using Microsoft.OData.Edm; + using Microsoft.OData.ModelBuilder; + public static class LinqExtensions { /// @@ -498,23 +501,32 @@ private static List GetIncludes(this IEnumerable selectedIte }); } - public static List> GetExpansions(this SelectExpandQueryOption clause, Type parentType) + public static List> GetExpansions( + this SelectExpandQueryOption clause, + Type parentType, + IEdmModel edmModel) { if (clause?.SelectExpandClause == null) return new List>(); - return clause.SelectExpandClause.SelectedItems.GetExpansions(parentType); + return clause.SelectExpandClause.SelectedItems.GetExpansions(parentType, edmModel); } - private static List> GetNestedExpansions(this ExpandedNavigationSelectItem node, Type type) + private static List> GetNestedExpansions( + this ExpandedNavigationSelectItem node, + Type type, + IEdmModel edmModel) { if (node == null) return new List>(); - return node.SelectAndExpand.SelectedItems.GetExpansions(type); + return node.SelectAndExpand.SelectedItems.GetExpansions(type, edmModel); } - private static List> GetExpansions(this IEnumerable selectedItems, Type parentType) + private static List> GetExpansions( + this IEnumerable selectedItems, + Type parentType, + IEdmModel edmModel) { if (selectedItems == null) return new List>(); @@ -524,6 +536,13 @@ private static List> GetExpansions(this IEnumerable< string path = next.PathToNavigationProperty.FirstSegment.Identifier;//Only first segment is necessary because of the new syntax $expand=Builder($expand=City) vs $expand=Builder/City Type currentParentType = parentType.GetCurrentType(); + + if (next.PathToNavigationProperty.FirstSegment is TypeSegment) { //Handle cast in $expand + path = next.PathToNavigationProperty.LastSegment.Identifier; + ClrTypeAnnotation annotation = edmModel.GetAnnotationValue(next.PathToNavigationProperty.FirstSegment.EdmType); + currentParentType = annotation.ClrType; + } + Type memberType = currentParentType.GetMemberInfo(path).GetMemberType(); Type elementType = memberType.GetCurrentType(); @@ -537,7 +556,7 @@ private static List> GetExpansions(this IEnumerable< Selects = next.SelectAndExpand.GetSelects() }; - List> navigationItems = next.GetNestedExpansions(elementType).Select + List> navigationItems = next.GetNestedExpansions(elementType, edmModel).Select ( expansions => { diff --git a/AutoMapper.AspNetCore.OData.EFCore/QueryableExtensions.cs b/AutoMapper.AspNetCore.OData.EFCore/QueryableExtensions.cs index 6bc72d7..3095af0 100644 --- a/AutoMapper.AspNetCore.OData.EFCore/QueryableExtensions.cs +++ b/AutoMapper.AspNetCore.OData.EFCore/QueryableExtensions.cs @@ -126,8 +126,8 @@ private static IQueryable GetQueryable(this IQueryable> filter) where TModel : class { - - var expansions = options.SelectExpand.GetExpansions(typeof(TModel)); + + var expansions = options.SelectExpand.GetExpansions(typeof(TModel), options.Context.Model); return query.GetQuery (