Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix expand with nested filer on model without default constructor #217

Merged
merged 1 commit into from
Jul 6, 2024
Merged
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
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
Loading