Skip to content

Commit

Permalink
fixes #986: add Order for conventional, fixes the failing test, updat…
Browse files Browse the repository at this point in the history
…e the sample
  • Loading branch information
xuzhg committed Aug 9, 2023
1 parent 8569df8 commit 32b9b0a
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public IActionResult Post([FromBody]Customer c)
[HttpGet("odata/Customers(CoreSN={ssn})")] // use core alternate key
public IActionResult GetCustomerBySSN(string ssn)
{
ssn = ssn.Replace("%", "%25");
var c = _repository.GetCustomers().FirstOrDefault(c => c.SSN == ssn);
if (c == null)
{
Expand Down
6 changes: 3 additions & 3 deletions sample/ODataAlternateKeySample/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ Send one of the following requests

```C#
GET http://localhost:5219/odata/Customers(2)
GET http://localhost:5219/odata/Customers(SSN='SSN-2-102')
GET http://localhost:5219/odata/Customers(CoreSN='SSN-2-102')
GET http://localhost:5219/odata/Customers(SSN='SSN-%25-2-102')
GET http://localhost:5219/odata/Customers(CoreSN='SSN-%25-2-102')
```

you can get the following result:
Expand All @@ -217,7 +217,7 @@ you can get the following result:
"@odata.context": "http://localhost:5219/odata/$metadata#Customers/$entity",
"Id": 2,
"Name": "Jerry",
"SSN": "SSN-2-102",
"SSN": "SSN-%25-2-102",
"Titles": [
"abc",
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public static void AddSelector(this ActionModel action, string httpMethods, stri
// OData convention route template doesn't get combined with the route template applied to the controller.
// Route templates applied to an action that begin with / or ~/ don't get combined with route templates applied to the controller.
Template = $"/{templateStr}",
Order = options?.Order,
Name = templateStr // do we need this?
};

Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13953,6 +13953,12 @@
Initializes a new instance of the <see cref="T:Microsoft.AspNetCore.OData.Routing.ODataRouteOptions" /> class.
</summary>
</member>
<member name="P:Microsoft.AspNetCore.OData.Routing.ODataRouteOptions.Order">
<summary>
Gets or sets the route order in conventional routing.
By default, move the conventional routing later as much as possible.
</summary>
</member>
<member name="P:Microsoft.AspNetCore.OData.Routing.ODataRouteOptions.EnableDollarCountRouting">
<summary>
Gets/sets a value indicating whether to enable $count in conventional routing.
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.AspNetCore.OData/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,8 @@ Microsoft.AspNetCore.OData.Routing.ODataRouteOptions.EnableQualifiedOperationCal
Microsoft.AspNetCore.OData.Routing.ODataRouteOptions.EnableUnqualifiedOperationCall.get -> bool
Microsoft.AspNetCore.OData.Routing.ODataRouteOptions.EnableUnqualifiedOperationCall.set -> void
Microsoft.AspNetCore.OData.Routing.ODataRouteOptions.ODataRouteOptions() -> void
Microsoft.AspNetCore.OData.Routing.ODataRouteOptions.Order.get -> int?
Microsoft.AspNetCore.OData.Routing.ODataRouteOptions.Order.set -> void
Microsoft.AspNetCore.OData.Routing.ODataRoutingMetadata
Microsoft.AspNetCore.OData.Routing.ODataRoutingMetadata.IsConventional.get -> bool
Microsoft.AspNetCore.OData.Routing.ODataRoutingMetadata.IsConventional.set -> void
Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.AspNetCore.OData/Routing/ODataRouteOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public ODataRouteOptions()
_enableUnqualifiedOperationCall = true;
}

/// <summary>
/// Gets or sets the route order in conventional routing.
/// </summary>
public int? Order { get; set; }

/// <summary>
/// Gets/sets a value indicating whether to enable $count in conventional routing.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public IActionResult Get(int key)
}

// alternate key: SSN
[HttpGet("Customers(SSN={ssn})", Order = 2)]
// why set Order = -2 (any number less than 0)? it is because 'Get' method has 'catch-all' template, we should move this template ahead
// Small order goes first.
// We can also leave order value unset, same as 'Get' method and 'PatchCustomerBySSN' method without setting the order value.
// Without setting the order value makes all routes with same order value and catch-all goes latter
[HttpGet("Customers(SSN={ssn})", Order = -2)]
public IActionResult GetCustomerBySSN(string ssn)
{
// for special test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,7 @@ public class Microsoft.AspNetCore.OData.Routing.ODataRouteOptions {
bool EnablePropertyNameCaseInsensitive { public get; public set; }
bool EnableQualifiedOperationCall { public get; public set; }
bool EnableUnqualifiedOperationCall { public get; public set; }
System.Nullable`1[[System.Int32]] Order { public get; public set; }
}

public sealed class Microsoft.AspNetCore.OData.Routing.ODataRoutingMetadata : IODataRoutingMetadata {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,7 @@ public class Microsoft.AspNetCore.OData.Routing.ODataRouteOptions {
bool EnablePropertyNameCaseInsensitive { public get; public set; }
bool EnableQualifiedOperationCall { public get; public set; }
bool EnableUnqualifiedOperationCall { public get; public set; }
System.Nullable`1[[System.Int32]] Order { public get; public set; }
}

public sealed class Microsoft.AspNetCore.OData.Routing.ODataRoutingMetadata : IODataRoutingMetadata {
Expand Down

0 comments on commit 32b9b0a

Please sign in to comment.