OData / OData/AspNetCoreOData

Conventional routing to sub collections not working in 8.0.x

Open
#249 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

Let's take these objects:

public class SalesOrder
{
    public int Id { get; set; }
    public int CustomerId { get; set; }
    public List<SalesOrderLine> Lines { get; set; } = new();
}

public class SalesOrderLine
{
    public int Id { get; set; }
    public int SalesOrderId { get; set; }
    public int ItemId { get; set; }
    public double Quantity { get; set; }
}

Using conventional routing, I used to be able to set up a controller like this:

public class SalesOrdersController : ODataController
{
    private MyDbContext Context { get; }

    public SalesOrdersController(MyDbContext context)
    {
        Context = context ?? throw new ArgumentNullException(nameof(context));
    }

    // Sample request: SalesOrders?$filter=customerId eq 1
    [EnableQuery]
    public IQueryable<SalesOrder> Get() => Context.SalesOrders;

    // Sample request: SalesOrders(1)?$expand=lines
    [EnableQuery]
    public SingleResult<SalesOrder> Get(int key) => SingleResult.Create(Context.SalesOrders.Where(o => o.Id == key));

    // Sample request: SalesOrders(1)/Lines?$filter=quantity gt 0
    [EnableQuery]
    public IQueryable<SalesOrderLine> GetLines(int key) => Context.SalesOrderLines.Where(l => l.SalesOrderId == key);
}

All three endpoints used to return proper OData responses, wrapped as one object including @odata.context. However, with the 8.0.1 package on .NET 5, I cannot get the third endpoint to work.

If left as above, it simply returns 404 not found.
The only way I manage to get a response, is by adding [HttpGet("SalesOrders({key})/Lines")], but then it appears to ignore the EDM model, so camelcasing doesn't work and the response isn't wrapped - it just returns a flat array, not an OData response.

I've tried with different combinations of attributes, but so far I haven't been able to get the expected response.
I would have expected the 'conventional' way to just work, but it doesn't.
Is this a bug, or do we need to do this differently with 8.x?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SalesOrdersController example and the GetLines(int key) endpoint, then reproduce the 404 using the 8.0.1 package on .NET 5. Compare the conventional route with the explicit [HttpGet("SalesOrders({key})/Lines")] route. Done means the subcollection route resolves and returns the expected OData response with EDM-based camelcasing and wrapping.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.