Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swashbuckle custom string comparer not applied for order groups of actions

In Swashbuckle there is a setting called OrderActionGroupsBy which is supposed to change the ordering within the API, but nothing I do is working and I'm can't determine whether this is a Swashbuckle problem, or due to my IComparer any idea what I'm doing wrong?

This is setting the configurations

       config.EnableSwagger(c =>
       {
            ...
            c.OrderActionGroupsBy(new CustomStringComparer());
            c.GroupActionsBy(apiDesc => GroupBy(apiDesc));
            ...
        }

This is grouping the actions by type instead of controllerName.

        private static string GroupBy(ApiDescription apiDesc)
        {
            var controllerName = apiDesc.ActionDescriptor.ControllerDescriptor.ControllerName;
            var path = apiDesc.RelativePath;

            if (controllerName.Contains("Original"))
            {
                controllerName = controllerName.Replace("Original", "");
            }
            // Check if it is one of the entities if so group by that
            // Otherwise group by controller
            var entities = new List<string>() { "Users", "Apps", "Groups" };


            var e = entities.Where(x => attr.Contains(x.ToLower())).FirstOrDefault();
            if (e != null)
            {
                return e;
            }
            return controllerName;
        }

This is my attempt at an IComparer I want Users first and then after that alphabetical

        class CustomStringComparer : IComparer<string>
        {
            public int Compare(string x, string y)
            {
                if (x.CompareTo(y) == 0)
                    return 0;

                if (x.CompareTo("Users") == 0)
                    return -1;
                if (y.CompareTo("Users") == 0)
                    return 1;

                 return x.CompareTo(y);
            }
        }
    }

This isn't working it always defaults to alphabetical no matter what I do.

like image 914
nastassiar Avatar asked Oct 18 '22 22:10

nastassiar


1 Answers

Looks like this is a bug with Swashbuckle/Swagger-ui

Using OrderActionGroupsBy is correctly sorting the JSON file, but then swagger ui automatically resorts this to alphabetical order.

I have filed bugs with both Swashbuckle and swagger-ui since this seems to go against what is said in swagger-ui's doc regarding apisSorter.

Apply a sort to the API/tags list. It can be 'alpha' (sort by name) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged.

Swashbuckle issue

swagger-ui issue

swagger-ui specific stackoverflow question

like image 52
nastassiar Avatar answered Oct 29 '22 15:10

nastassiar