Skip to content

Commit 675de06

Browse files
authored
Fix expand with nested filer on model without default constructor (#217)
1 parent d091a40 commit 675de06

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

AutoMapper.AspNetCore.OData.EFCore/Visitors/ProjectionVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected override Expression VisitMemberInit(MemberInitExpression node)
2626

2727
return Expression.MemberInit
2828
(
29-
Expression.New(node.Type),
29+
Expression.New(node.NewExpression.Constructor, node.NewExpression.Arguments),
3030
node.Bindings.OfType<MemberAssignment>().Aggregate
3131
(
3232
new List<MemberBinding>(),

AutoMapper.OData.EFCore.Tests/Data/DataClasses.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ public class Category
6969
public IQueryable<Product> QueryableProducts { get; set; }
7070
}
7171

72+
public class SuperCategory
73+
{
74+
public SuperCategory(string superCategoryName)
75+
{
76+
SuperCategoryName = superCategoryName;
77+
}
78+
79+
public int SuperCategoryID { get; set; }
80+
public string SuperCategoryName { get; set; }
81+
82+
public IEnumerable<Category> Categories { get; set; }
83+
}
84+
7285
public class CompositeKey
7386
{
7487
public int ID1 { get; set; }

AutoMapper.OData.EFCore.Tests/GetQueryTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,51 @@ static void Test(ICollection<CategoryModel> collection)
13341334
}
13351335
}
13361336

1337+
private IQueryable<SuperCategory> GetSuperCategories()
1338+
=> new SuperCategory[]
1339+
{
1340+
new SuperCategory("SuperCategoryOne")
1341+
{
1342+
SuperCategoryID = 1,
1343+
Categories = GetCategories()
1344+
},
1345+
new SuperCategory("SuperCategoryTwo")
1346+
{
1347+
SuperCategoryID = 2,
1348+
Categories = Enumerable.Empty<Category>()
1349+
}
1350+
}.AsQueryable();
1351+
1352+
[Fact]
1353+
public async void ExpandChildCollectionWithNoDefaultConstructorNoFilter()
1354+
{
1355+
const string query = "/SuperCategoryModel?$expand=Categories";
1356+
Test(await GetAsync<SuperCategoryModel, SuperCategory>(query, GetSuperCategories()));
1357+
Test(await GetUsingCustomNameSpace<SuperCategoryModel, SuperCategory>(query, GetSuperCategories()));
1358+
Test(Get<SuperCategoryModel, SuperCategory>(query, GetSuperCategories()));
1359+
1360+
static void Test(ICollection<SuperCategoryModel> collection)
1361+
{
1362+
Assert.NotEmpty(collection.First().Categories);
1363+
Assert.Empty(collection.Last().Categories);
1364+
}
1365+
}
1366+
1367+
[Fact]
1368+
public async void ExpandChildCollectionWithNoDefaultConstructorNestedFilter()
1369+
{
1370+
const string query = "/SuperCategoryModel?$expand=Categories($filter=CategoryName eq 'CategoryOne')";
1371+
Test(await GetAsync<SuperCategoryModel, SuperCategory>(query, GetSuperCategories()));
1372+
Test(await GetUsingCustomNameSpace<SuperCategoryModel, SuperCategory>(query, GetSuperCategories()));
1373+
Test(Get<SuperCategoryModel, SuperCategory>(query, GetSuperCategories()));
1374+
1375+
static void Test(ICollection<SuperCategoryModel> collection)
1376+
{
1377+
Assert.Single(collection.First().Categories);
1378+
Assert.Empty(collection.Last().Categories);
1379+
}
1380+
}
1381+
13371382
[Fact]
13381383
public async Task CancellationThrowsException()
13391384
{

AutoMapper.OData.EFCore.Tests/Mappings/ObjectMappings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public ObjectMappings()
1515
.ForAllMembers(o => o.ExplicitExpansion());
1616
CreateMap<Category, CategoryModel>()
1717
.ForAllMembers(o => o.ExplicitExpansion());
18+
CreateMap<SuperCategory, SuperCategoryModel>()
19+
.ForAllMembers(o => o.ExplicitExpansion());
1820
CreateMap<DataTypes, DataTypesModel>()
1921
.ForAllMembers(o => o.ExplicitExpansion());
2022
CreateMap<DerivedCategory, DerivedCategoryModel>()

AutoMapper.OData.EFCore.Tests/Model/ModelClasses.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ public class CategoryModel
6969
public IQueryable<ProductModel> QueryableProducts { get; set; }
7070
}
7171

72+
public class SuperCategoryModel
73+
{
74+
public SuperCategoryModel(string superCategoryName)
75+
{
76+
SuperCategoryName = superCategoryName;
77+
}
78+
79+
[Key]
80+
public int SuperCategoryID { get; set; }
81+
public string SuperCategoryName { get; set; }
82+
public IEnumerable<CategoryModel> Categories { get; set; }
83+
}
84+
7285
public class CompositeKeyModel
7386
{
7487
[Key]

0 commit comments

Comments
 (0)