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

Maintain case for parameter alias in generated nextlink #740

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
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ internal static Uri GetNextPageLink(Uri requestUri, IEnumerable<KeyValuePair<str
case "$skiptoken":
continue;
default:
key = kvp.Key; // Leave parameters that are not OData query options in initial form
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -47,4 +48,23 @@ public IActionResult Get()
return Ok(_serverSidePagingCustomers);
}
}

public class ServerSidePagingEmployeesController : ODataController
{
private static List<ServerSidePagingEmployee> employees = new List<ServerSidePagingEmployee>(
Enumerable.Range(1, 13).Select(idx => new ServerSidePagingEmployee
{
Id = idx,
HireDate = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(2022, 11, 07).AddMonths(idx), TimeZoneInfo.Local)
}));

[HttpGet]
[EnableQuery(PageSize = 3)]
public IActionResult GetEmployeesHiredInPeriod([FromRoute] DateTime fromDate, [FromRoute] DateTime toDate)
{
var hiredInPeriod = employees.Where(d => d.HireDate >= fromDate && d.HireDate <= toDate);

return Ok(hiredInPeriod);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

Expand All @@ -25,4 +26,10 @@ public class ServerSidePagingOrder
public decimal Amount { get; set; }
public ServerSidePagingCustomer ServerSidePagingCustomer { get; set; }
}

public class ServerSidePagingEmployee
{
public int Id { get; set; }
public DateTime HireDate { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
Expand All @@ -27,7 +28,7 @@ public ServerSidePagingTests(WebApiTestFixture<ServerSidePagingTests> fixture)
protected static void UpdateConfigureServices(IServiceCollection services)
{
IEdmModel edmModel = GetEdmModel();
services.ConfigureControllers(typeof(ServerSidePagingCustomersController));
services.ConfigureControllers(typeof(ServerSidePagingCustomersController), typeof(ServerSidePagingEmployeesController));
services.AddControllers().AddOData(opt => opt.Expand().AddRouteComponents("{a}", edmModel));
}

Expand All @@ -36,6 +37,13 @@ protected static IEdmModel GetEdmModel()
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<ServerSidePagingOrder>("ServerSidePagingOrders").EntityType.HasRequired(d => d.ServerSidePagingCustomer);
builder.EntitySet<ServerSidePagingCustomer>("ServerSidePagingCustomers").EntityType.HasMany(d => d.ServerSidePagingOrders);

var getEmployeesHiredInPeriodFunction = builder.EntitySet<ServerSidePagingEmployee>(
"ServerSidePagingEmployees").EntityType.Collection.Function("GetEmployeesHiredInPeriod");
getEmployeesHiredInPeriodFunction.Parameter(typeof(DateTime), "fromDate");
getEmployeesHiredInPeriodFunction.Parameter(typeof(DateTime), "toDate");
getEmployeesHiredInPeriodFunction.ReturnsCollectionFromEntitySet<ServerSidePagingEmployee>("ServerSidePagingEmployees");

return builder.GetEdmModel();
}

Expand Down Expand Up @@ -84,5 +92,27 @@ public async Task ValidNextLinksGenerated()
Assert.Equal("http://localhost/prefix/ServerSidePagingCustomers?$expand=ServerSidePagingOrders&$skip=5", nextLink.GetString());
}
}

[Fact]
public async Task VerifyParametersInNextPageLinkInEdmFunctionResponseBodyAreInSameCaseAsInRequestUrl()
{
// Arrange
var requestUri = "/prefix/ServerSidePagingEmployees/" +
"GetEmployeesHiredInPeriod(fromDate=@fromDate,toDate=@toDate)" +
"?@fromDate=2023-01-07T00:00:00%2B00:00&@toDate=2023-05-07T00:00:00%2B00:00";
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this change affect query options?

Copy link
Contributor Author

@gathogojr gathogojr Nov 9, 2022

Choose a reason for hiding this comment

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

@habbes It doesn't affect OData query options. This means that if the there was $ORDERBY on the request URL (I don't know if it's allowed), it'll be returned as $orderby on the @odata.nextLink in the response body. I think that should be okay

var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
var client = CreateClient();

// Act
var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();

// Assert
Assert.Contains("\"@odata.nextLink\":", content);
Assert.Contains(
"/prefix/ServerSidePagingEmployees/GetEmployeesHiredInPeriod(fromDate=@fromDate,toDate=@toDate)" +
"?%40fromDate=2023-01-07T00%3A00%3A00%2B00%3A00&%40toDate=2023-05-07T00%3A00%3A00%2B00%3A00&$skip=3",
content);
}
}
}