diff --git a/AutoMapper.AspNetCore.OData.EFCore/Visitors/ProjectionVisitor.cs b/AutoMapper.AspNetCore.OData.EFCore/Visitors/ProjectionVisitor.cs index 4b4eed0..e536d13 100644 --- a/AutoMapper.AspNetCore.OData.EFCore/Visitors/ProjectionVisitor.cs +++ b/AutoMapper.AspNetCore.OData.EFCore/Visitors/ProjectionVisitor.cs @@ -26,7 +26,7 @@ protected override Expression VisitMemberInit(MemberInitExpression node) return Expression.MemberInit ( - Expression.New(node.Type), + Expression.New(node.NewExpression.Constructor, node.NewExpression.Arguments), node.Bindings.OfType().Aggregate ( new List(), diff --git a/AutoMapper.OData.EFCore.Tests/Data/DataClasses.cs b/AutoMapper.OData.EFCore.Tests/Data/DataClasses.cs index 7737e89..4904064 100644 --- a/AutoMapper.OData.EFCore.Tests/Data/DataClasses.cs +++ b/AutoMapper.OData.EFCore.Tests/Data/DataClasses.cs @@ -69,6 +69,19 @@ public class Category public IQueryable QueryableProducts { get; set; } } + public class SuperCategory + { + public SuperCategory(string superCategoryName) + { + SuperCategoryName = superCategoryName; + } + + public int SuperCategoryID { get; set; } + public string SuperCategoryName { get; set; } + + public IEnumerable Categories { get; set; } + } + public class CompositeKey { public int ID1 { get; set; } diff --git a/AutoMapper.OData.EFCore.Tests/GetQueryTests.cs b/AutoMapper.OData.EFCore.Tests/GetQueryTests.cs index 7e6f8c5..d477374 100644 --- a/AutoMapper.OData.EFCore.Tests/GetQueryTests.cs +++ b/AutoMapper.OData.EFCore.Tests/GetQueryTests.cs @@ -1334,6 +1334,51 @@ static void Test(ICollection collection) } } + private IQueryable GetSuperCategories() + => new SuperCategory[] + { + new SuperCategory("SuperCategoryOne") + { + SuperCategoryID = 1, + Categories = GetCategories() + }, + new SuperCategory("SuperCategoryTwo") + { + SuperCategoryID = 2, + Categories = Enumerable.Empty() + } + }.AsQueryable(); + + [Fact] + public async void ExpandChildCollectionWithNoDefaultConstructorNoFilter() + { + const string query = "/SuperCategoryModel?$expand=Categories"; + Test(await GetAsync(query, GetSuperCategories())); + Test(await GetUsingCustomNameSpace(query, GetSuperCategories())); + Test(Get(query, GetSuperCategories())); + + static void Test(ICollection collection) + { + Assert.NotEmpty(collection.First().Categories); + Assert.Empty(collection.Last().Categories); + } + } + + [Fact] + public async void ExpandChildCollectionWithNoDefaultConstructorNestedFilter() + { + const string query = "/SuperCategoryModel?$expand=Categories($filter=CategoryName eq 'CategoryOne')"; + Test(await GetAsync(query, GetSuperCategories())); + Test(await GetUsingCustomNameSpace(query, GetSuperCategories())); + Test(Get(query, GetSuperCategories())); + + static void Test(ICollection collection) + { + Assert.Single(collection.First().Categories); + Assert.Empty(collection.Last().Categories); + } + } + [Fact] public async Task CancellationThrowsException() { diff --git a/AutoMapper.OData.EFCore.Tests/Mappings/ObjectMappings.cs b/AutoMapper.OData.EFCore.Tests/Mappings/ObjectMappings.cs index ae6f858..155eb66 100644 --- a/AutoMapper.OData.EFCore.Tests/Mappings/ObjectMappings.cs +++ b/AutoMapper.OData.EFCore.Tests/Mappings/ObjectMappings.cs @@ -15,6 +15,8 @@ public ObjectMappings() .ForAllMembers(o => o.ExplicitExpansion()); CreateMap() .ForAllMembers(o => o.ExplicitExpansion()); + CreateMap() + .ForAllMembers(o => o.ExplicitExpansion()); CreateMap() .ForAllMembers(o => o.ExplicitExpansion()); CreateMap() diff --git a/AutoMapper.OData.EFCore.Tests/Model/ModelClasses.cs b/AutoMapper.OData.EFCore.Tests/Model/ModelClasses.cs index 8ce21ad..84a529f 100644 --- a/AutoMapper.OData.EFCore.Tests/Model/ModelClasses.cs +++ b/AutoMapper.OData.EFCore.Tests/Model/ModelClasses.cs @@ -69,6 +69,19 @@ public class CategoryModel public IQueryable QueryableProducts { get; set; } } + public class SuperCategoryModel + { + public SuperCategoryModel(string superCategoryName) + { + SuperCategoryName = superCategoryName; + } + + [Key] + public int SuperCategoryID { get; set; } + public string SuperCategoryName { get; set; } + public IEnumerable Categories { get; set; } + } + public class CompositeKeyModel { [Key]