Swashbuckle.webapi: How to sort methods by path?

Created on 24 Oct 2013  路  5Comments  路  Source: domaindrivendev/Swashbuckle.WebApi

Hi
Is it possible to add functionality to sort methods by path. For example: I have a lot of methods and it is not easy to search the method.(see attached image).

methods_path

Many thanks
Maksim

Most helpful comment

I finally did it by creating a document filter with this code:

public class CustomDocumentFilter : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, System.Web.Http.Description.IApiExplorer apiExplorer)
        {
            //make operations alphabetic
            var paths = swaggerDoc.paths.OrderBy(e => e.Key).ToList();
            swaggerDoc.paths = paths.ToDictionary(e => e.Key, e => e.Value);


        }
    }

and then in SwaggerConfig.cs, I included this:

c.DocumentFilter<CustomDocumentFilter>();

All 5 comments

Hey there.

You can customize the way in which Operations are grouped into "Api Declarations". The default grouping key is controller name and this is why you see a long list of operations under a single "OrderServices" declaration.

Swahsbuckle provides a hook for you to specify your own strategy for grouping operations. See the readme for details but the here's the gist ...

SwaggerSpecConfig.Customize(c =>
{
c.GroupDeclarationsBy(apiDesc => apiDesc.RelativePath.Replace("/", "|"));
}

One thing to note, I'm replacing the forward slash with a pipe here because of a strange feature in the swagger-ui - it removes everything before the last forward slash. This achieves the grouping you're looking for but doesn't look great. So you can play around a little to get something more appropriate. Essentially, you can create any groupings you'd like with the one caveat that a key containing forward slashes will cause issues with the current swagger-ui.

Hope this helps

Hi
I have default grouping by Controller. It is absolutely valid and correct grouping for me. My question was about ordering methods inside a group. I want to order they by path inside Group. Currently we have just two columns for ordering. They are HttpMethod and MethodPath.

So am I correct in saying you just want the list of operations under a declaration sorted alphabetically by relative path?

The current implementation doesn't apply any sorting although it would be fairly simple to implement. I've a few other things on the backlog so may not get to this until next week. Or alternatively, please feel free to submit a pull request yourself?

Thanks

I finally did it by creating a document filter with this code:

public class CustomDocumentFilter : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, System.Web.Http.Description.IApiExplorer apiExplorer)
        {
            //make operations alphabetic
            var paths = swaggerDoc.paths.OrderBy(e => e.Key).ToList();
            swaggerDoc.paths = paths.ToDictionary(e => e.Key, e => e.Value);


        }
    }

and then in SwaggerConfig.cs, I included this:

c.DocumentFilter<CustomDocumentFilter>();

pallu's code works fine (thanks by the way, I use it). Though it seems as it will be unsupported in the next major version, see: https://github.com/domaindrivendev/Swashbuckle/issues/416

Was this page helpful?
0 / 5 - 0 ratings

Related issues

niemyjski picture niemyjski  路  3Comments

kuttiram picture kuttiram  路  5Comments

raaga123 picture raaga123  路  4Comments

Misiu picture Misiu  路  3Comments

maheshmohandas picture maheshmohandas  路  4Comments