Skip to content

Conversation

@WanjohiSammy
Copy link
Member

@WanjohiSammy WanjohiSammy commented Oct 22, 2025

Description

fixes #1531

This PR introduces a new constructor to both ODataQueryOptions and ODataQueryOptions<TEntity> that allows initialization directly from an IDictionary<string, string> and an optional IEdmModel/ODataPath, removing the requirement for an HttpRequest or ASP.NET Core infrastructure. This enables easier instantiation of OData query options in scenarios such as deserialization from JSON, tool integration, and testing.

Key changes:

  • Added ODataQueryOptions(IDictionary<string, string> queryParameters, ODataQueryContext context) constructor to support dictionary-based initialization.
  • Added ODataQueryOptions(IEdmModel model, IDictionary<string, string> queryParameters, ODataPath path = null) constructor for generic scenarios.

/// <param name="model">The EDM model (can be null for non-model scenarios).</param>
/// <param name="queryParameters">The OData query parameters as a dictionary.</param>
/// <param name="path">The ODataPath (optional, can be null).</param>
public ODataQueryOptions(IEdmModel model, IDictionary<string, string> queryParameters, ODataPath path = null)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add another constructor without the IEdmModel model parameter to support non-model scenarios better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wertzui I have added it. Could you please take a look

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now :)

Copy link
Member Author

@WanjohiSammy WanjohiSammy Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wertzui Could you try to use it with your scenario and share a sample project that can be used as a testing

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a nightly NuGet feed or something similar where I can get this specific version? You can find my current implementation without these changes at https://github.com/wertzui/AutoMCP

Copy link
Member Author

@WanjohiSammy WanjohiSammy Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wertzui I have generated a nightly that you can use. Here is the link:
Microsoft.AspNetCore.OData 9.4.1-Nightly202511050747

Copy link

@wertzui wertzui Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it, you can get the source here.
However I got this exception:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'request')
   at Microsoft.AspNetCore.OData.Extensions.HttpRequestExtensions.ODataFeature(HttpRequest request)
   at Microsoft.AspNetCore.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)
   at Microsoft.AspNetCore.OData.Query.ODataQueryOptions`1.ApplyTo(IQueryable query, ODataQuerySettings querySettings)
   at Microsoft.AspNetCore.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query)
   at Microsoft.AspNetCore.OData.Query.ODataQueryOptions`1.ApplyTo(IQueryable query)
   at AutoMcp.Example.Controllers.WeatherForecastController.GetMultiple(ODataQueryOptions`1 options)

I think, the problem is the kind of "late-binding" which is used.
Instead of having something like a factory which creates an ODataQueryOptions without any dependencies, the dependencies are only resolved when ApplyTo() is called and only then the actual expressions are created.

@WanjohiSammy
Copy link
Member Author

/AzurePipelines run

@azure-pipelines
Copy link

No pipelines are associated with this pull request.

@WanjohiSammy WanjohiSammy linked an issue Oct 31, 2025 that may be closed by this pull request
@WanjohiSammy WanjohiSammy marked this pull request as ready for review October 31, 2025 12:17
@WanjohiSammy
Copy link
Member Author

/AzurePipelines run

@azure-pipelines
Copy link

No pipelines are associated with this pull request.

@WanjohiSammy
Copy link
Member Author

/AzurePipelines run

@azure-pipelines
Copy link

No pipelines are associated with this pull request.

@WanjohiSammy
Copy link
Member Author

/AzurePipelines run

@azure-pipelines
Copy link

No pipelines are associated with this pull request.

@WanjohiSammy WanjohiSammy changed the title Add Constructor to ODataQueryOptions and ODataQueryOptions<TEntity> for Dictionary-Based Initialization Without HttpRequest Add Constructor to ODataQueryOptions and ODataQueryOptions<TEntity> for Dictionary-Based Initialization Without HttpRequest and optional IEdmModel Nov 5, 2025
context.ElementType,
context.NavigationSource,
queryParameters,
context.RequestContainer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where do you get the RequestContainer? you use 'context.RequestContainer', is it valid?

/// the given <paramref name="elementClrType"/>.</param>
/// <param name="elementClrType">The CLR type of the element of the collection being queried.</param>
/// <param name="path">The parsed <see cref="ODataPath"/>.</param>
public ODataQueryOptions(IDictionary<string, string> queryParameters, Type elementClrType, IEdmModel model = null, ODataPath path = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion is to accept

  1. IserviceProvider or
  2. other required services?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which services are required by ODataQueryOptions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow simpler creation of ODataQueryOptions<TEntity>

4 participants