Skip to content

Commit

Permalink
Fix expand with nested filer on model without default constructor (Au…
Browse files Browse the repository at this point in the history
  • Loading branch information
TimS95 authored Jul 6, 2024
1 parent d091a40 commit 675de06
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<MemberAssignment>().Aggregate
(
new List<MemberBinding>(),
Expand Down
13 changes: 13 additions & 0 deletions AutoMapper.OData.EFCore.Tests/Data/DataClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ public class Category
public IQueryable<Product> QueryableProducts { get; set; }
}

public class SuperCategory
{
public SuperCategory(string superCategoryName)
{
SuperCategoryName = superCategoryName;
}

public int SuperCategoryID { get; set; }
public string SuperCategoryName { get; set; }

public IEnumerable<Category> Categories { get; set; }
}

public class CompositeKey
{
public int ID1 { get; set; }
Expand Down
45 changes: 45 additions & 0 deletions AutoMapper.OData.EFCore.Tests/GetQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,51 @@ static void Test(ICollection<CategoryModel> collection)
}
}

private IQueryable<SuperCategory> GetSuperCategories()
=> new SuperCategory[]
{
new SuperCategory("SuperCategoryOne")
{
SuperCategoryID = 1,
Categories = GetCategories()
},
new SuperCategory("SuperCategoryTwo")
{
SuperCategoryID = 2,
Categories = Enumerable.Empty<Category>()
}
}.AsQueryable();

[Fact]
public async void ExpandChildCollectionWithNoDefaultConstructorNoFilter()
{
const string query = "/SuperCategoryModel?$expand=Categories";
Test(await GetAsync<SuperCategoryModel, SuperCategory>(query, GetSuperCategories()));
Test(await GetUsingCustomNameSpace<SuperCategoryModel, SuperCategory>(query, GetSuperCategories()));
Test(Get<SuperCategoryModel, SuperCategory>(query, GetSuperCategories()));

static void Test(ICollection<SuperCategoryModel> 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<SuperCategoryModel, SuperCategory>(query, GetSuperCategories()));
Test(await GetUsingCustomNameSpace<SuperCategoryModel, SuperCategory>(query, GetSuperCategories()));
Test(Get<SuperCategoryModel, SuperCategory>(query, GetSuperCategories()));

static void Test(ICollection<SuperCategoryModel> collection)
{
Assert.Single(collection.First().Categories);
Assert.Empty(collection.Last().Categories);
}
}

[Fact]
public async Task CancellationThrowsException()
{
Expand Down
2 changes: 2 additions & 0 deletions AutoMapper.OData.EFCore.Tests/Mappings/ObjectMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public ObjectMappings()
.ForAllMembers(o => o.ExplicitExpansion());
CreateMap<Category, CategoryModel>()
.ForAllMembers(o => o.ExplicitExpansion());
CreateMap<SuperCategory, SuperCategoryModel>()
.ForAllMembers(o => o.ExplicitExpansion());
CreateMap<DataTypes, DataTypesModel>()
.ForAllMembers(o => o.ExplicitExpansion());
CreateMap<DerivedCategory, DerivedCategoryModel>()
Expand Down
13 changes: 13 additions & 0 deletions AutoMapper.OData.EFCore.Tests/Model/ModelClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ public class CategoryModel
public IQueryable<ProductModel> 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<CategoryModel> Categories { get; set; }
}

public class CompositeKeyModel
{
[Key]
Expand Down

0 comments on commit 675de06

Please sign in to comment.