Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AutoMapper.AspNetCore.OData.EF6/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private static IQueryable<TModel> GetQueryable<TModel, TData>(this IQueryable<TD
Expression<Func<TModel, bool>> filter)
where TModel : class
{
var expansions = options.SelectExpand.GetExpansions(typeof(TModel));
var expansions = options.SelectExpand.GetExpansions(typeof(TModel), options.Context.Model);

return query.GetQuery
(
Expand Down
31 changes: 25 additions & 6 deletions AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace AutoMapper.AspNet.OData
{
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;

public static class LinqExtensions
{
/// <summary>
Expand Down Expand Up @@ -498,23 +501,32 @@ private static List<string> GetIncludes(this IEnumerable<SelectItem> selectedIte
});
}

public static List<List<ODataExpansionOptions>> GetExpansions(this SelectExpandQueryOption clause, Type parentType)
public static List<List<ODataExpansionOptions>> GetExpansions(
this SelectExpandQueryOption clause,
Type parentType,
IEdmModel edmModel)
{
if (clause?.SelectExpandClause == null)
return new List<List<ODataExpansionOptions>>();

return clause.SelectExpandClause.SelectedItems.GetExpansions(parentType);
return clause.SelectExpandClause.SelectedItems.GetExpansions(parentType, edmModel);
}

private static List<List<ODataExpansionOptions>> GetNestedExpansions(this ExpandedNavigationSelectItem node, Type type)
private static List<List<ODataExpansionOptions>> GetNestedExpansions(
this ExpandedNavigationSelectItem node,
Type type,
IEdmModel edmModel)
{
if (node == null)
return new List<List<ODataExpansionOptions>>();

return node.SelectAndExpand.SelectedItems.GetExpansions(type);
return node.SelectAndExpand.SelectedItems.GetExpansions(type, edmModel);
}

private static List<List<ODataExpansionOptions>> GetExpansions(this IEnumerable<SelectItem> selectedItems, Type parentType)
private static List<List<ODataExpansionOptions>> GetExpansions(
this IEnumerable<SelectItem> selectedItems,
Type parentType,
IEdmModel edmModel)
{
if (selectedItems == null)
return new List<List<ODataExpansionOptions>>();
Expand All @@ -524,6 +536,13 @@ private static List<List<ODataExpansionOptions>> 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<ClrTypeAnnotation>(next.PathToNavigationProperty.FirstSegment.EdmType);
currentParentType = annotation.ClrType;
}

Type memberType = currentParentType.GetMemberInfo(path).GetMemberType();
Type elementType = memberType.GetCurrentType();

Expand All @@ -537,7 +556,7 @@ private static List<List<ODataExpansionOptions>> GetExpansions(this IEnumerable<
Selects = next.SelectAndExpand.GetSelects()
};

List<List<ODataExpansionOptions>> navigationItems = next.GetNestedExpansions(elementType).Select
List<List<ODataExpansionOptions>> navigationItems = next.GetNestedExpansions(elementType, edmModel).Select
(
expansions =>
{
Expand Down
4 changes: 2 additions & 2 deletions AutoMapper.AspNetCore.OData.EFCore/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ private static IQueryable<TModel> GetQueryable<TModel, TData>(this IQueryable<TD
Expression<Func<TModel, bool>> filter)
where TModel : class
{
var expansions = options.SelectExpand.GetExpansions(typeof(TModel));

var expansions = options.SelectExpand.GetExpansions(typeof(TModel), options.Context.Model);

return query.GetQuery
(
Expand Down