Search code examples
swaggerswagger-uispringdoc

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"))


Solution

  • By default, you can sort tags 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()));
    }