swagger-uispringdoc-openapi-uispringdoc-ui

How to sort the Schemas on Swagger-ui SpringDoc open ui


I want to sort my Schemas generated for my Entity classes, DTO classes in Springdoc UI.
I am able to sort the tags and operations using the below configuration in yml file but my schemas are not in the sorted order.

springdoc:
  swagger-ui:
    disable-swagger-default-url: true
    tags-sorter: alpha
    operations-sorter: alpha
    doc-expansion: none

How could I sort my schemas.
Thanks.


Solution

  • You can have full control of the schemas order using OpenApiCustomiser. This is a sample code that you can customize using Comparators, depending on the sorting logic you want:

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

    If you are interested on the sorting on the swagger-ui, not on the server side, then you can log a feature request on the swagger-ui project.