OData / AspNetCoreOData

ASP.NET Core OData: A server library built upon ODataLib and ASP.NET Core
Other
457 stars 156 forks source link

OData, Net5: How to implement $count when controllers return entities of multiple types #351

Open mperdeck opened 2 years ago

mperdeck commented 2 years ago

I am adding OData to an existing large API in .Net5, where the action methods in each controller return different types (such as OrganizationFull, OrganizationList, etc.)

This is working, except that the responses do not contain OData meta data, such as $count. So when a consumer calls /api/OrganizationList?$count=true, it doesn't get the count.

I understand that to get this meta data, I have to add an EDM model, like so:

// Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers()
                .AddOData(opt => opt.Select().Filter().SetMaxTop(10).AddRouteComponents("odata", GetEdmModel()))

        IEdmModel GetEdmModel()
        {
            var odataBuilder = new ODataConventionModelBuilder();

            odataBuilder.EntitySet<OrganizationFull>(nameof(Organization));
            odataBuilder.EntitySet<OrganizationList>(nameof(Organization)); // throws
            return odataBuilder.GetEdmModel();
        }

Here I specified 2 EntitySets, one for each entity type returned from the OrganizationController.

However, this throws an exception "The entity set 'Organization' was already configured with a different EntityType ('OrganizationFull').". And if I remove the EntitySet for OrganizationList, when I hit the List end point, I get a 404.

How can I get my end points to return the $count meta data, while keeping my API (which returns multiple entity types from one controller)?

gathogojr commented 2 years ago

@mperdeck On the AddOData method call, enable support for Count as follows:

                .AddOData(opt => opt.Select().Filter().Count().SetMaxTop(10).AddRouteComponents("odata", GetEdmModel()))
mperdeck commented 2 years ago

You're right, I forgot to include that. However, this fix doesn't solve my problem - I need to retrieve the count somehow, but I understand the only way to do that is to provide an EDM model. And to provide an EDM model, I understand that each controller can return only one model. To me (and I guess many others), this would mean breaking changes in my API.

If I'm getting this wrong, please let me know.

I created a pull request with a possible solution at https://github.com/OData/AspNetCoreOData/pull/355

mperdeck commented 2 years ago

Found a solution, which however if very dependent on the current version of this library. Added this to the same issue that I raised on Stackoverflow: https://stackoverflow.com/questions/69776758/odata-net5-how-to-implement-count-when-controllers-return-entities-of-multipl/69879343#69879343