-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ASP.NET Core OData 8.x does not work with Minimal API #578
Comments
@Lonli-Lokli could you please provide us with more information on any problems you are getting with using the Minimal Api with OData |
@ElizabethOkerio Basically I would like to use OData with only Minial Api, without enabling Controllers on MVC (.AddControllers) or writing OData Controllers with EnableQuery. I want to use OData with pure Minimal API endpoint - specify [EnableQuery] and make it works same as with Controller. Now it does not work |
So basically this is the problem ... public static void AddApi(this IServiceCollection services)
{
_ = services.AddControllers()
.AddOData(opt =>
{
opt.RouteOptions.EnableQualifiedOperationCall = false;
opt.EnableAttributeRouting = true;
_ = opt.Expand().Count().Filter().Select().OrderBy().SetMaxTop(1000);
opt.AddRouteComponents($"/Odata", new MyEdmModel());
});
} ... the setup for odata is dependant on this it seems ... Random thought though ... In a controller situation it would look like this ... public class FooController
{
[HttpGet]
[EnableQuery]
public virtual IActionResult Get(ODataQueryOptions<Foo> queryOptions)
=> Ok(queryOptions.ApplyTo(service.GetAll()));
} I can't see any reason why that couldn't be done in a minimal API sitation unless the above MUST be made in order for some DI thing to be configured. So this makes your potential solution something like ... app.MapGet("/WeatherForecast", [EnableQuery] (ODataQueryOptions<WetherData> options) =>
options.ApplyTo(
Enumerable.Range(1, 5).Select((int index) => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = new[] {
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
}
})
) .ToArray());
app.Run(); |
I was able to call AddOdata without AddControllers with Reflection
But your approach with ODataQueryOptions will not work as ODataQueryOptions are not registered as service. As an addition, AttributeRouting or ConventionRouting doesnt make much sense with Minimal Api. The best use case is as I described above - non-edm way with EnableQuery |
Just took a look at the code ... public class ODataQueryOptions<TEntity> : ODataQueryOptions
{
public ODataQueryOptions(ODataQueryContext context, HttpRequest request) ... so you're gonna need to do this ... static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<WeatherForecast>("WeatherForecasts");
return builder.GetEdmModel();
}
var edmModel = GetEdmModel();
builder.Services.AddScoped<ODataQueryOptions<WeatherForcast>>(ctx =>
new ODataQueryContext(edmModel, typeof(WeatherForecast)); ... OData is dependent on EDM Models it seems at the moment, there's no way to escape that. |
Even according to your sample it's clear that ODataQueryOptions cannot be added during application building as they requires HttpRequest. Also
will not compile because thi constructor is internal, also ODataQueryContext cannot be casted ODataQueryOptions.
It will cycled data instead of similar code with controllers |
It can using factory-based registrations with |
Just to show how it looks like with pure Contollers (and to show that EdmModel is NOT required), here is the sample - https://github.com/Lonli-Lokli/net6-conrtoller-odata
And as a result with |
@julealgon Thanks, forget about it. With current snippet I am getting
|
Weird.... there are no cyclic relationships in the model. I wonder what it is trying to do. |
Hi, is there any more information about this issue? Looking for a way to integrate this in my current minimap API project. |
This is not possible yet because we need a different input/output formatter i made a project and got pretty far but the only issue is that you can't parse input Delta in .net 7 route filter handlers will be added that will maybe solve that issue. |
@Gigabyte0x1337 but it will not work without EDM model as with Controllers, right? Also I think Batch will not be able to handle this as well |
I haven't tried maybe i can get those working too. I don't like the input parameters wrappers that can maybe be fixed in .NET 7. When I have some time i could try to make it work without edm and with the batch functionality. |
@xuzhg / @habbes do you have a ballpark estimate on when this will be looked into? .NET is pushing minimal API heavily lately, especially now with the launch of .NET 7. The fact that OData just doesn't work with it out of the box is a glaring omission at this point. Not being able to leverage all the benefits of minimal APIs is pretty significant at the moment, and I'll include myself in that sentiment. |
I created a proof of concept with new .NET features which helps with some ideas on the possibilities for implementation. There is some extra code that doesn't need to be there, some tiny things that can be fixed/changed. Not everything is implemented of course but I hope I can help to move this issue forward. https://github.com/Gigabyte0x1337/ODataMinimalApiExample |
Hello, do you know if any progress have been made on that, now that .NET 8 has come out ? |
I am also interested in the progress here, any news with .NET8? |
@cilerler what does this have to do with AspNetCore OData and Minimal APIs? Maybe I'm missing something obvious here. |
@julealgon, apologies for the confusion—I was browsing the Restier repository and got sidetracked, leading to a mix-up. Please ignore my previous comment; it was not intended for this issue. Thank you for seeking clarification. |
@cilerler @cilerler @Lonli-Lokli @TehWardy Any one can download the 'miniApi' branch and give it a try and share your thoughts? |
@xuzhg Does this mean we lose the model validation that is inside the mvc formatter. In mvc you have model state errors how does that work in this scenario? Also do all odata features still work like navigation parameter binding with @ in post for example? |
Nice work @xuzhg, 👏🏻 |
I believe the way I assume that controller would also need to be converted to support Minimal API. |
@cilerler Thanks for your feedback. I enabled the metadata in the PR, please take a look and share your comments. Thanks. Merry Christmas! and Happy New Year! |
@julealgon You are always RIGHT! :) please take a look the PR and share your comments. Merry Christmas! and Happy New Year! |
I've been out of the loop for a while is this solved now? |
What I'd like to see is that we can just do AddOData and the inject whatever the class that has the Filter, Sort and Select in it into the minimal api endpoint, and then use that to apply it ourselves. I.e. the Filter would be Expression<Func<T, bool>>, the Sort similar, and the Select similar as well along with Top and Skip. And that would end the dependency on entities entirely, which would be highly desirable to work against DTOs correctly etc. You could sort of hack this up with controllers, but it wasn't pretty. |
@jhancock-taxa I believe what you are describing would be completely out of scope for a Minimal API support effort. My suggestion would be for you to create a separate issue to propose/discuss that. |
Hi, is there an official plan support for OData via WebApi minimal? |
Assemblies affected
ASP.NET Core OData 8.x
Describe the bug
OData does not work with Minimal API, ie without Controllers
Reproduce steps
Expected behavior
I would like to use OData without Controllers at all
The text was updated successfully, but these errors were encountered: