Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 339
SSK Avatar asked Jun 19 '20 14:06

SSK


People also ask

What is the difference between OpenAPI and swagger?

Although the terms once referred to the same thing, they can no longer be used interchangeably…even though some people still do. In 2021, OpenAPI refers to the industry-standard specification for RESTful API design. Swagger refers to a set of SmartBear tools.

How can I define multiple OpenAPI definitions in one spring boot project?

How can I define multiple OpenAPI definitions in one Spring Boot project? You can define your own groups of API based on the combination of: API paths and packages to scan. Each group should have a unique groupName .

How does Springdoc work?

springdoc-openapi works by examining an application at runtime to infer API semantics based on spring configurations, class structure and various annotations. Automatically generates documentation in JSON/YAML and HTML format APIs. This documentation can be completed by comments using swagger-api annotations.

Is there a way to sort the schemas by order in Swagger?

You can have full control of the schemas order using OpenApiCustomiser. 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 instead. Sorry, something went wrong. Perhaps it would be nice to make this sorting the default as well.

How do I sort an array in Swagger UI?

It can be 'alpha' (sort by paths alphanumerically) or a function (see Array.prototype.sort () to learn how to write a sort function). Two tag name strings are passed to the sorter for each pass. Default is the order determined by Swagger UI. Boolean=false.

How to add custom Swagger documentation in Spring Boot?

For custom path of the swagger documentation in HTML format, add a custom springdoc property, in your spring-boot configuration file: . 3. Springdoc-openapi Modules 3.1. General overview

Where can I find Swagger UI configuration files?

From lowest to highest precedence: 1 The swagger-config.yaml in the project root directory, if it exists, is baked into the application 2 configuration object passed as an argument to Swagger UI ( SwaggerUI ( { ... })) 3 configuration document fetched from a specified configUrl 4 configuration items passed as key/value pairs in the URL query string


Video Answer


1 Answers

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.

like image 108
brianbro Avatar answered Nov 24 '22 21:11

brianbro