Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a custom sorter to sort my springdoc swagger tags by name in the UI?

I am using springdoc-openapi with the latest version (1.3.0). Now I would like sort my tags in the UI by "name" property.

I know about the "springdoc.swagger-ui.tagsSorter" configuration and that I can use a custom sorter function. But I cannot find examples how the function should look like.

I tried the following which does not seem to work:

springdoc.swagger-ui.tagsSorter=(a, b) => a.get("name").localeCompare(b.get("name"))

like image 930
aleyandraaa Avatar asked Mar 02 '23 16:03

aleyandraaa


1 Answers

By default, you can sort tags alphabetically:

  • https://springdoc.org/faq.html#how-can-i-sort-endpoints-alphabetically

You can have control on the tags order, using OpenApiCustomiser and define your own Comparator:

@Bean
public OpenApiCustomiser sortTagsAlphabetically() {
    return openApi -> openApi.setTags(openApi.getTags()
            .stream()
            .sorted(Comparator.comparing(tag -> StringUtils.stripAccents(tag.getName())))
            .collect(Collectors.toList()));
}
like image 178
brianbro Avatar answered Apr 07 '23 04:04

brianbro