Skip to content
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

Stable ordering should be applied when using the EDM model for paging (in addition to EnableQueryAttribute.PageSize) #1018

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Microsoft.AspNetCore.OData/Query/ODataQueryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ public virtual IQueryable ApplyTo(IQueryable query, ODataQuerySettings querySett
if (querySettings.EnsureStableOrdering &&
(IsAvailableODataQueryOption(Skip, querySettings, AllowedQueryOptions.Skip) ||
IsAvailableODataQueryOption(Top, querySettings, AllowedQueryOptions.Top) ||
querySettings.PageSize.HasValue))
querySettings.PageSize.HasValue ||
querySettings.ModelBoundPageSize.HasValue))
{
// If there is no OrderBy present, we manufacture a default.
// If an OrderBy is already present, we add any missing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,38 @@ public void ApplyToODataQueryOptions_Builds_Default_OrderBy_No_Keys(string oData
Assert.Equal(expectedExpression, queryExpression);
}

[Theory]
[InlineData(null, 1)]
[InlineData(1, null)]
public void ApplyToODataQueryOptions_Builds_Default_OrderBy_With_Paging(int? pageSize, int? modelBoundPageSize)
{
// Arrange
IEdmModel model = GetEdmModel(c => c.CustomerId);
HttpRequest request = RequestFactory.Create(HttpMethods.Get, "http://localhost/Customers");

var options = new ODataQueryOptions(new ODataQueryContext(model, typeof(Customer)), request);
ODataQuerySettings querySettings = new ODataQuerySettings
{
PageSize = pageSize,
ModelBoundPageSize = modelBoundPageSize
};

Customer[] customers = new[] {
new Customer() { CustomerId = 4 },
new Customer() { CustomerId = 3 },
new Customer() { CustomerId = 1 },
new Customer() { CustomerId = 2 }
};

// Act
IQueryable query = options.ApplyTo(customers.AsQueryable(), querySettings);
Customer[] results = (query as IQueryable<Customer>).ToArray();

// Assert
Assert.Equal(querySettings.PageSize ?? querySettings.ModelBoundPageSize, results.Length);
Assert.Equal(customers.OrderBy(c => c.CustomerId).First().CustomerId, results[0].CustomerId);
}

[Fact]
public void Validate_ThrowsValidationErrors_ForOrderBy()
{
Expand Down