AutoMapper / AutoMapper.Extensions.OData

Creates LINQ expressions from ODataQueryOptions and executes the query.
MIT License
140 stars 38 forks source link

$expand with count #137

Open cheng93 opened 2 years ago

cheng93 commented 2 years ago

The expanded@odata.count properties are missing.

In the following sample, there are two examples

Sample https://github.com/cheng93/AutoMapper.OData/blob/master/ApiController.cs

http://localhost:5238/odata/categories?$expand=products($count=true)

expected

{
 "@odata.context": "http://localhost:5238/odata/$metadata#categories(Products())",
    "value": [
        {
            "CategoryId": 1,
            "CategoryName": "CategoryOne",
            "Products@odata.count": 3, <-- visible
            "Products": [
                {
                    "ProductId": 1,
                    "ProductName": "ProductOne"
                },
                {
                    "ProductId": 2,
                    "ProductName": "ProductTwo"
                },
                {
                    "ProductId": 3,
                    "ProductName": "ProductThree"
                }
            ]
        },
        {
            "CategoryId": 2,
            "CategoryName": "CategoryTwo",
            "Products@odata.count": 2, <-- visible
            "Products": [
                {
                    "ProductId": 4,
                    "ProductName": "ProductFour"
                },
                {
                    "ProductId": 5,
                    "ProductName": "ProductFive"
                }
            ]
        }
    ]
}

actual

{
    "@odata.context": "http://localhost:5238/odata/$metadata#categories(Products())",
    "value": [
        {
            "CategoryId": 1,
            "CategoryName": "CategoryOne",
            "Products": [
                {
                    "ProductId": 1,
                    "ProductName": "ProductOne"
                },
                {
                    "ProductId": 2,
                    "ProductName": "ProductTwo"
                },
                {
                    "ProductId": 3,
                    "ProductName": "ProductThree"
                }
            ]
        },
        {
            "CategoryId": 2,
            "CategoryName": "CategoryTwo",
            "Products": [
                {
                    "ProductId": 4,
                    "ProductName": "ProductFour"
                },
                {
                    "ProductId": 5,
                    "ProductName": "ProductFive"
                }
            ]
        }
    ]
}
BlaiseD commented 2 years ago

You are correct - that is not currently supported. There is a CountOption property in the expansion.

At the root level it is being applied by setting a property as implemented here.

Still - Ok to create a PR to return count for expansions if you have a solution.

NikAtIdeally commented 9 months ago

Hi, any progress with this? Also any chance of supporting filter on expand? So something like :

api/Entity?$expand=Child($filter=name eq "Bob")

BlaiseD commented 9 months ago

The tests show examples of filter with expand. Ok to submit a PR on the count feature.