6

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

ADJ
  • 1,182
  • 2
  • 14
  • 26
aleyandraaa
  • 63
  • 1
  • 1
  • 4

3 Answers3

11

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()));
}
brianbro
  • 4,141
  • 2
  • 24
  • 37
  • 1
    Since Springdoc sort tags, field value alphabetically, how can we disable this ? – PAA Jun 04 '20 at 13:27
11

With reference from @brianbro's answer, as suggested at https://springdoc.org/faq.html#how-can-i-sort-endpoints-alphabetically

I added

@Tag(name="1. Admin endpoints")

@Tag(name = "2. Everyone's enpoints!")

and below prop to application.yml :

springdoc.swagger-ui.tagsSorter=alpha

And can see them sorted according to numbering on my swagger UI.

ADJ
  • 1,182
  • 2
  • 14
  • 26
2

For sorting schemas , paths and tags in OpenApi.

    @Bean
    public OpenApiCustomiser openApiCustomiser() {
        return openApi -> {
            Map<String, Schema> schemas = openApi.getComponents().getSchemas();
            openApi.getComponents().setSchemas(new TreeMap<>(schemas));
        };
    }

    @Bean
    public OpenApiCustomiser sortPathsAndTagsAlphabetically() {
        return openApi -> {
            Map<String, PathItem> paths = openApi.getPaths();
            Paths sortedPaths = new Paths();
            TreeMap<String, PathItem> sortedTree = new TreeMap<String, PathItem>(paths);

            Set<Map.Entry<String, PathItem>> pathItems = sortedTree.entrySet();
            Map<String, Map.Entry<String, PathItem>> distinctTagMap = new TreeMap<String, Map.Entry<String, PathItem>>();
            for ( Map.Entry<String, PathItem> entry:pathItems) {
                PathItem pathItem = entry.getValue();
                Operation getOp = pathItem.getGet();
                if(getOp != null) {
                    String tag = getOp.getTags().get(0);
                    if (!distinctTagMap.containsKey(tag)) {
                        distinctTagMap.put(tag, entry);
                    }
                }
                Operation postOp = pathItem.getPost();
                if(postOp != null){
                    String tag1 = postOp.getTags().get(0);
                    if(!distinctTagMap.containsKey(tag1)){
                        distinctTagMap.put(tag1,entry);
                    }
                }

                Operation putOp = pathItem.getPut();
                if(putOp != null) {
                    String tag2 = putOp.getTags().get(0);
                    if (!distinctTagMap.containsKey(tag2)) {
                        distinctTagMap.put(tag2, entry);
                    }
                }
            }

            LinkedHashMap<String, PathItem> customOrderMap = new LinkedHashMap<String, PathItem>();
            for (Map.Entry<String, PathItem> entry: distinctTagMap.values()) {
                customOrderMap.put(entry.getKey(), entry.getValue());
            }
            for(Map.Entry<String, PathItem> entry : sortedTree.entrySet()) {
                customOrderMap.putIfAbsent(entry.getKey(), entry.getValue());
            }
            sortedPaths.putAll(customOrderMap);
            openApi.setPaths(sortedPaths);

        };
    }