AspNetCoreOData icon indicating copy to clipboard operation
AspNetCoreOData copied to clipboard

Group by a property of a derived type is not supported

Open clemvnt opened this issue 1 year ago • 4 comments

Hello,

I have a case where I want to group by a property of a derived type :

/products?apply=groupby((category/ODataGroupByDerivedTypeIssue.Models.Category/categoryName)) 

An error indicating that this is not supported is thrown.

I would have expected it to be possible. Is it a lack or are there reasons why it's not possible?

Knowing that using a derived type in a filter clause works :

/products?filter=category/ODataGroupByDerivedTypeIssue.Models.Category/CategoryName eq 'Category 1'

Assemblies affected

Microsoft.AspNetCore.OData 8.2.4

Reproduce steps

  1. Clone the repository : https://github.com/clemvnt/ODataGroupByDerivedTypeIssue
  2. Run

It should open the /products?apply=groupby((category/ODataGroupByDerivedTypeIssue.Models.Category/categoryName)) URL.

Expected result

[
    { "category": { "categoryName": "Category 1" } },
    { "category": { "categoryName": "Category 2" } }
]

Actual result

The following error :

System.NotSupportedException: The query specified in the URI is not valid. Binding OData QueryNode of kind 'SingleResourceCast' is not supported by 'AggregationBinder'.

Stack trace :

at Microsoft.AspNetCore.OData.Query.Expressions.TransformationBinderBase.BindAccessor(QueryNode node, Expression baseElement)
at Microsoft.AspNetCore.OData.Query.Expressions.TransformationBinderBase.BindAccessor(QueryNode node, Expression baseElement)
at Microsoft.AspNetCore.OData.Query.Expressions.AggregationBinder.CreateGroupByMemberAssignments(IEnumerable`1 nodes)
at Microsoft.AspNetCore.OData.Query.Expressions.AggregationBinder.BindGroupBy(IQueryable query)
at Microsoft.AspNetCore.OData.Query.Expressions.AggregationBinder.Bind(IQueryable query)
at Microsoft.AspNetCore.OData.Query.ApplyQueryOption.ApplyTo(IQueryable query, ODataQuerySettings querySettings)
at Microsoft.AspNetCore.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)
at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)
at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.ExecuteQuery(Object responseValue, IQueryable singleResultCollection, ControllerActionDescriptor actionDescriptor, HttpRequest request)
at Microsoft.AspNetCore.OData.Query.EnableQueryAttribute.OnActionExecuted(ActionExecutedContext actionExecutedContext, Object responseValue, IQueryable singleResultCollection, ControllerActionDescriptor actionDescriptor, HttpRequest request)

clemvnt avatar Feb 29 '24 20:02 clemvnt

I tried to look to support this case.

I succeeded by making the following changes: clemvnt/odata.net@674ed16 clemvnt/AspNetCoreOData@45b7a57

Let me know if it sounds good. If so, I'll look into adding the tests.

clemvnt avatar Feb 29 '24 23:02 clemvnt

I tried to look to support this case.

I succeeded by making the following changes: clemvnt/odata.net@674ed16 clemvnt/AspNetCoreOData@45b7a57

Let me know if it sounds good. If so, I'll look into adding the tests.

Thanks for your inputs. Both changes look good to me. Looking forward to your contribution by sharing us the complete Pull Request. Thanks

xuzhg avatar Mar 01 '24 19:03 xuzhg

I have created both PRs : https://github.com/OData/odata.net/pull/2879 https://github.com/OData/AspNetCoreOData/pull/1182

The second one is in draft because it depends on the first.

clemvnt avatar Mar 02 '24 00:03 clemvnt

I will take the review of the ODL change, @xuzhg to work through the PR in this repo.

corranrogue9 avatar Mar 05 '24 17:03 corranrogue9