Skip to content

Commit

Permalink
feat(literal-lists)including literal types for non-navigation/simple …
Browse files Browse the repository at this point in the history
…members.

AutoMapper#196
  • Loading branch information
NetanelPersikGoogle committed Oct 18, 2024
1 parent 675de06 commit c7ae880
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
6 changes: 5 additions & 1 deletion AutoMapper.AspNetCore.OData.EFCore/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ private static IQueryable<TModel> GetQueryable<TModel, TData>(this IQueryable<TD
{

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

var selects = options.SelectExpand.GetSelects();
var literalLists = typeof(TModel).GetLiteralLists();
var includes = selects.Union(literalLists).ToList();

return query.GetQuery
(
Expand All @@ -140,7 +144,7 @@ private static IQueryable<TModel> GetQueryable<TModel, TData>(this IQueryable<TD
options.GetQueryableExpression(querySettings?.ODataSettings),
expansions
.Select(list => new List<Expansion>(list))
.BuildIncludes<TModel>(options.SelectExpand.GetSelects())
.BuildIncludes<TModel>(includes)
.ToList(),
querySettings?.ProjectionSettings
).UpdateQueryableExpression(expansions, options.Context);
Expand Down
27 changes: 24 additions & 3 deletions AutoMapper.AspNetCore.OData.EFCore/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using LogicBuilder.Expressions.Utils;
using Microsoft.AspNetCore.OData.Edm;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

Expand All @@ -21,6 +18,17 @@ public static MemberInfo[] GetSelectedMembers(this Type parentType, List<string>

return selects.Select(select => parentType.GetMemberInfo(select)).ToArray();
}

public static IEnumerable<string> GetLiteralLists(this Type type)
{
foreach (var member in type.GetMemberInfos())
{
if (member.MemberType is not (MemberTypes.Field or MemberTypes.Property)) continue;

if (member.GetMemberType().IsListLiteral())
yield return member.Name;
}
}

private static MemberInfo[] GetValueTypeMembers(this Type parentType)
{
Expand All @@ -33,6 +41,19 @@ private static MemberInfo[] GetValueTypeMembers(this Type parentType)
&& info.GetMemberType().IsLiteralType()
).ToArray();
}

private static bool IsListLiteral(this Type type)
{
// Check if type is a List
if (!type.IsList()) return false;

// If not generic, check if it's a literal type (i.e. string[])
if (!type.IsGenericType) return type.GetElementType().IsLiteralType();

// Extract the type T from List<T> and check if it's a literal type
var firstGenericArgument = type.GetGenericArguments().First();
return firstGenericArgument.IsLiteralType();
}

private static MemberInfo[] GetMemberInfos(this Type parentType)
=> parentType.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase);
Expand Down

0 comments on commit c7ae880

Please sign in to comment.