Cannot get OrderActionsBy working correctly. I need to order actions based on HTTP methods used so that GET methods come first (then POST, PUT, DELETE etc.). I have a few API controllers with some actions (using different HTTP methods). So I tried something very simple as shown below.
This is not working (order doesn't change at all):
config.OrderActionsBy(apiDesc =>
{
return apiDesc.HttpMethod == "GET" ? "1" : "2";
});
This is working (but obviously it doesn't order actions as I want):
config.OrderActionsBy(apiDesc =>
{
return apiDesc.HttpMethod;
});
Am I doing something wrong or misunderstood something or is this a bug in Swashbuckle?
Swagger, the community-driven spec that powers Swashbuckle, does not list operations in a flat list. It dictates that operations MUST be grouped by path. So, the ordering is only applied within those groups.
Perhaps, __TagActionsBy__ is what you need as the UI supports arbitrary grouping of operations based on a tag. The default is controller name but you could change to HTTP method.
I understand your point, but what I want is to order actions within a group based on HTTP methods (GETs first and so on). I don't want to group actions by HTTP methods. Now actions seem to be ordered by paths within a group? Actually, I also tried TagActionsBy and it worked as you mentioned, but that's not what I want.
So, is it possible to order actions within a group based on HTTP methods?
Anyway, I really like Swashbuckle and have used it earlier with WebAPI 2 and now trying to adopt this Core version.
Hmmm - so in that case OrderActionsBy really should be working as you expect. There's tests around that so I'm reluctant to concede it's a big. Let me try reproduce and I'll get back to you
Any update on this?
I've used the following code:
c.OrderActionsBy(description => description.HttpMethod);
Expected:
DELETE /competitors/{id}
GET /competitors/{id}
GET /competitors
GET /competitors/search/{keyword}
POST /competitors
PUT /competitors
Actual:
DELETE /competitors/{id}
GET /competitors/{id}
GET /competitors
POST /competitors
PUT /competitors
GET /competitors/search/{keyword}`
@aboo - as per my comments above, the Swagger document structure dictates that operations are always grouped by path and therefore sorting can only be applied within the scope of each path. Therefore, the actual behavior you've described is correct. That is, operations for the "/competitors/{id}" path are displayed first, and sorted by HTTP method, and are then followed by operations for the "/competitors" path, also sorted by HTTP method. This is the correct behavior
I do not think its working
Trying to set in the following order within the same Controller
GET - POST - PUT - DELETE
// Define order of the Actions on the page
Func<ApiDescription, string> sortKeySelector = (x =>
{
return
x.HttpMethod.Equals("GET", StringComparison.InvariantCultureIgnoreCase)
? "0"
: x.HttpMethod.Equals("POST", StringComparison.InvariantCultureIgnoreCase)
? "1"
: x.HttpMethod.Equals("PUT", StringComparison.InvariantCultureIgnoreCase)
? "2"
: x.HttpMethod.Equals("DELETE", StringComparison.InvariantCultureIgnoreCase)
? "3"
: "4";
});
options.OrderActionsBy(sortKeySelector);
*I wish I can disable Ordering in the first place and just load the way its in the controller
Hey there!
Sorry to bring this closed issue up again but I'm currently trying to get the exact result @janih78 was wanting : a way to sort operations by their http methods within a group.
Do you have any update or snippet which is making the job for this?
If there is some requirement that paths be sorted within a group and the OrderActionsBy sorting override can't change that, I wish that fact was more discoverable in the intellisense for the method in Visual Studio. It would have saved me many hours trying to figure out why sorting was broken.
Frankly, a sorting override like this should truly override the sorting. Setting a sort override only to have it changed by internal sorting is confusing and unexpected.
Unfortunately, the sorting override doesn't work within the grouping items, which is really weird.
The way it works is withing the logical grouping (GroupName), it orders items by sub-groups (1st controller items order, then 2nd then 3rd, etc.)
The code below proves that:
``` c#
var actionsSortingOrder = new Dictionary
{
{ "GET", 0 },
{ "POST", 1 },
{ "PUT", 2 },
{ "DELETE", 3 },
};
var actionsDefaultOrder = _actionsSortingOrder.Max(m => m.Value) + 1;
var actionOrderCollector = new Dictionary
options.OrderActionsBy((oa =>
{
var method = oa.HttpMethod.ToUpperInvariant();
string res;
if (actionsSortingOrder.ContainsKey(method))
{
if (!actionOrderCollector.ContainsKey(method))
{
actionOrderCollector[method] = 0;
}
res = $"{actionsSortingOrder[method]}.{actionOrderCollector[method]++}";
}
else
{
res = actionsDefaultOrder.ToString();
}
return res;
}));
```
Most helpful comment
I do not think its working
Trying to set in the following order within the same Controller
GET - POST - PUT - DELETE
*I wish I can disable Ordering in the first place and just load the way its in the controller