8

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.

Sergey
  • 3,253
  • 2
  • 33
  • 55
SSK
  • 3,444
  • 6
  • 32
  • 59

1 Answers1

12

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.

brianbro
  • 4,141
  • 2
  • 24
  • 37
  • 5
    The [property](https://springdoc.org/properties.html) is `springdoc: writer-with-order-by-keys: true`. – jaco0646 Jun 29 '22 at 17:11