Skip to content

Using Skip Token Handlers independently #2875

Open

Description

@JoseLimaIVD

Hi,

Please forgive me if the question is too simple, as I'm trying to understand the functionalities.

I have a scenario where I need to implement specific query parameters (mostly skiptoken and filter) and I was looking into using this library. However, the project is big enough that conforming to the EDM model/odatacontrollers is quite a bit of work. I was wondering if there is a simple way to reuse the handlers without needing to register/confirm to EDM models?

for example, for skip token, I have a custom logic for producing the skip token and fetching the next page from it, but if I were to override DefaultTokenHandler, that would require me to create an ODataSerializerContext and have it register with an edm routeprefix in the hostbuilder. ODataQueryOptions seems to correctly parse the queries so I think it would be nice if:

  1. I can configure services directly in the options (without needing to pass a routePrefix, modelBuilder)
  2. ApplyTo for SkipTokenHandler to allow me to pass a custom page fetching logic while reusing the uri building mechanisms in place (top/pagesize handling, conflict with other query parameters, etc)
  3. GenerateNextPageLink to don't depend on ODataSerializerContext

Does this makes sense? please let me know if there is a better way

Activity

  1. WanjohiSammy commented on Jun 24, 2025

    @WanjohiSammy
    Member

    IMPORTANT: Please note that the response you get back is not an OData response

    @JoseLimaIVD I believe you can create a minimal EDM model that only contains model definition of a specific entity type. ODataQueryOptions can parse query parameters like $filter, $orderby, $top, and $skiptoken without requiring a full EDM model registration. However, some advanced features like SkipTokenHandler and GenerateNextPageLink are tightly coupled with the OData pipeline and ODataSerializerContext.

    Here’s a simple example of how to parse $filter and $top using ODataQueryOptions with a minimal EDM model, without needing to fully adopt OData controllers or route prefixes.

    [Route("api/[controller]")]
    [ApiController]
    public class ProductsController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetFilteredProducts()
        {
            var edmModel = GetEdmModel();
            var context = new ODataQueryContext(edmModel, typeof(Product), new ODataPath());
            var queryOptions = new ODataQueryOptions<Product>(context, Request);
    
            IQueryable<Product>? products = GetProducts();
    
            // Apply $filter and $top
            if (queryOptions.Filter != null)
            {
                products = queryOptions.Filter.ApplyTo(products, new ODataQuerySettings()) as IQueryable<Product>;
            }
    
            // Apply $top if specified
            if (queryOptions.Top != null)
            {
                products = queryOptions.Top.ApplyTo(products, new ODataQuerySettings());
            }
    
            return Ok(products);
        }
    
        // This model is just enough to allow parsing of OData queries for the Product type.
        private static IEdmModel GetEdmModel()
        {
            var builder = new ODataConventionModelBuilder();
            builder.EntitySet<Product>("Products");
    
            return builder.GetEdmModel();
        }
    
        // Simulate a data source
        private static IQueryable<Product> GetProducts()
        {
            return new List<Product>
            {
                ...
            }.AsQueryable();
        }
    }

    Sample Requests

    GET /api/products?$filter=Price gt 500&$top=2
    GET /api/products?$filter=Category eq 'Category3'&$top=2
    

    Non-OData response:

    [
      {
        "id": 3,
        "name": "Product3",
        "price": 30,
        "category": "Category3"
      },
      {
        "id": 6,
        "name": "Product6",
        "price": 60,
        "category": "Category3"
      }
    ]
  2. self-assigned this
    on Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions