AspNetCoreOData icon indicating copy to clipboard operation
AspNetCoreOData copied to clipboard

AllowedQueryOptions confusion

Open dnperfors opened this issue 3 years ago • 2 comments

When using AspNetCore.OData 8.x I got a bit confused on how the different query options are supposed to work. And since there is no documentation for 8.x I thought I'd ask it here. The confusion came when I tried out the AspNet.Api.Versioning to generate the api documentation and I have an issue there as well: dotnet/aspnet-api-versioning#853.

When configuring OData in the Startup.cs/Program.cs, you can set the (what I presume) default settings for supported the query options, which apply to all endpoints:

.AddOData(options => options.Count().Select().OrderBy())

Then on the controller you can also set the AllowedQueryOptions on the [EnableQuery]:

[EnableQuery( MaxTop = 100, AllowedQueryOptions = Select | Top | Skip | Count | Filter )]

When using the $orderby I get a 400 Bad Request with the message: " Query option 'OrderBy' is not allowed". I kind of understand that, since it is not configured on the EnableQuery. When using the $filter I get an error with the message: "The property 'customer' cannot be used in the $filter query option.". The $filter only works when I enabled it in the Startup.cs/Program.cs, although I would expect it to work since I explicitly enable it on the controller.

So my question is how does this supposed to work?

  • What do you configure in the Startup.cs/Program.cs
  • How does the AllowedQueryOptions on [EnableQuery] affect the configured values in the Startup.cs/Program.cs?

dnperfors avatar Aug 18 '22 08:08 dnperfors

You should think of the settings in startup as "what can ever be used at all", and the other places where this can be configured as "local overrides", which can be more restrictive, but never less restrictive.

You can configure the allowed actions either in the EnableQuery attribute, or in the EDM model itself.

Also, this should be moved to discussions.

julealgon avatar Aug 18 '22 12:08 julealgon

a old doc about the query setting can be found at https://github.com/OData/WebApi/blob/gh-pages/_posts/2016-08-05-13-01-modelbound-attribute.md

xuzhg avatar Aug 23 '22 16:08 xuzhg