yahehe / Nancy.Swagger

Nancy plugin for generated API documentation in Swagger format.
MIT License
133 stars 60 forks source link

Tags should be sorted before output? #154

Closed vincentparrett closed 6 years ago

vincentparrett commented 6 years ago

Expected Behavior

Output of Tags should be sorted in some manner

Actual Behavior

Seemingly random, most likely in order of discovery.

Not sure how hard this one is to tackle, but it looks rather odd when the tags are not sorted.

yahehe commented 6 years ago

Would alphabetical order be clearer?

vincentparrett commented 6 years ago

Yes, but looking at it, I suspect this is controlled by the UI. Still investigating.

vincentparrett commented 6 years ago

Ok, please ignore, the solution is adding

  tagsSorter: "alpha",

To the configuration javascript in the index.html

jnallard commented 6 years ago

Since the Tags are added dynamically, I think it still might be good to have a default alphabetical order. Yeah, you can change it in the UI, but it would pretty easy to add a sort here: https://github.com/yahehe/Nancy.Swagger/blob/master/src/Nancy.Swagger/Services/SwaggerMetadataProvider.cs#L78

vincentparrett commented 6 years ago

I actually tried that yesterday and it didn't seem to make any difference. I'll test again to be sure.

jnallard commented 6 years ago

I just tried this:

foreach (var tag in RetrieveSwaggerTags().OrderBy(x => x.Name))
{
      builder.Tag(tag);
}

And it seemed to work: alphabetical order (but with default at the end)

vincentparrett commented 6 years ago

Ok, not sure why it didn't for me. Since all my routes have tags I don't see default in the list, I guess you could always check if default is in the list and move it to the top, that would be useful as you would see the routes without tags first (I can't see why you wouldn't have tags on every route though).

jnallard commented 6 years ago

I see what you're doing. You're adding Tag names to the routes, but you're not adding Tag objects to the swagger root.

https://github.com/yahehe/Nancy.Swagger/blob/master/samples/Nancy.Swagger.Annotations.Demo/Modules/ServiceDetailsModule.cs#L26

tagCatalog.AddTag(new Tag()
{
     Name = ServiceTagName,
     Description = ServiceTagDescription
});

That's fine and it looks like Swagger UI still accepts it. But that's why your sorting didn't matter.

We could look into automatically adding tags if they are added to routes, but the logic gets tricky because of the seemingly randomized dependency injection of the modules.


Tags aren't required - so if you don't specify one (but you have others), SwaggerUI will add it to a default tag section.

I don't think default needs to be sorted, because it's a special category. Being at the bottom is fine. I created a PR for this simple change.