How to add OData parameters to your ASP.Net Core API

How to add OData parameters to your ASP.Net Core API

Bridge the gap between Microsoft.AspNetCore.OData and Swashbuckle.AspNetCore

My team and I have worked with the OData nuget package from Microsoft to add some OData magic to a normal REST API based on ASP.Net Core 3.1 (that setup is a blog post for another day 😅).

But along the way we found, that there were no out-of-the box solution to get the OData parameters visible in our normal OpenApi spec (generated by Swashbuckle ).

So today I learned (#TIL) that its quite easy to inject some custom properties into the OpenAPI model trough a IOperationFilter.

To utilize the above code, you have to add the following to the AddSwaggerGen builder (in Startup.cs or similar).

services.AddSwaggerGen(c =>
{
...
    // Add support for OData-like REST endpoint with [EnableQuery]
    c.OperationFilter<ODataOperationFilter>();
...
});

This enables all the OData parameters on the API methods which have the [EnableQuery] attribute.