From 0a851c145e5bb090fcaaf53a19a43d7ddcc1f77f Mon Sep 17 00:00:00 2001 From: Serge Aradj Date: Wed, 11 Oct 2023 07:30:32 +0000 Subject: [PATCH 1/2] Fix $expand with cast --- AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs b/AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs index cb6178b..392f250 100644 --- a/AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs +++ b/AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs @@ -524,6 +524,12 @@ 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) { + path = next.PathToNavigationProperty.LastSegment.Identifier; + currentParentType = next.PathToNavigationProperty.FirstSegment.EdmType.GetType(); + } + Type memberType = currentParentType.GetMemberInfo(path).GetMemberType(); Type elementType = memberType.GetCurrentType(); From 7259e1830d11593023c30b91ecbf626a4a73850f Mon Sep 17 00:00:00 2001 From: Serge Aradj Date: Wed, 11 Oct 2023 11:01:00 +0200 Subject: [PATCH 2/2] Fix for cast in $expand --- .../QueryableExtensions.cs | 2 +- .../LinqExtensions.cs | 29 ++++++++++++++----- .../QueryableExtensions.cs | 4 +-- 3 files changed, 24 insertions(+), 11 deletions(-) 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 392f250..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>(); @@ -525,9 +537,10 @@ private static List> GetExpansions(this IEnumerable< Type currentParentType = parentType.GetCurrentType(); - if (next.PathToNavigationProperty.FirstSegment is TypeSegment) { + if (next.PathToNavigationProperty.FirstSegment is TypeSegment) { //Handle cast in $expand path = next.PathToNavigationProperty.LastSegment.Identifier; - currentParentType = next.PathToNavigationProperty.FirstSegment.EdmType.GetType(); + ClrTypeAnnotation annotation = edmModel.GetAnnotationValue(next.PathToNavigationProperty.FirstSegment.EdmType); + currentParentType = annotation.ClrType; } Type memberType = currentParentType.GetMemberInfo(path).GetMemberType(); @@ -543,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 (