OData / OData/AspNetCoreOData

Using $top in an $expand with PageSize set results in bad SQL

Open
#301 1 comment 0 reactions 1 assignee View on GitHub

@xuzhg is already working on this.

Since Sep 16, 2021.

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

Description

Suppose the following query:

School?$expand=Students($top=1)

With the following model:

public class School
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Student> Students { get; set; }
}

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int SchoolId { get; set; }
    public School School { get; set; }
}

And on our GET we have PageSize = 50 (or any number).

The generated SQL will contain the following error:

Invalid column name 'SchoolId'.

For a clone-and-run example, see here:

https://github.com/joshcomley/ODataTopError

Or download:

ODataTopError.zip

Interestingly if you remove the PageSize and use the following (technically equivalent) query, it works (the generated SQL is quite different):

School?$expand=Students($top=1)&$top=50

Of course this is no true work-around, neither is setting MaxTop, as clients could still request an entire table of data if they simply omit the $top value.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.