Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I manually order the endpoints displayed on Swagger UI?

I'm using Docket to configure my Swagger 2 instance. But the only Options I currently see are to sort by Type (POST, GET, etc.) or by endpoint name (a-z).

There is a logical order to my endpoints and I'd like to display them in that order

What I want:

POST /start
POST /uplaod
POST /finalize
POST /checkStatus

Instead I get something like this:

POST /checkStatus
POST /finalize
POST /start
POST /upload

Code:

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .host(swaggerHost)
                .pathProvider(new RelativePathProvider(servletContext) {

                    @Override
                    public String getApplicationBasePath() {
                        return swaggerBasePath;
                    }
                })
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .build()
                .apiInfo(apiInfo())
                .securitySchemes(Collections.singletonList(securitySchema()))
                .securityContexts(Collections.singletonList(securityContext()));
    }
like image 548
user11058144 Avatar asked Nov 26 '22 23:11

user11058144


1 Answers

Sorting operations in Swagger UI - Sort by specific http verb order, then by path

operationsSorter: function (a, b) {
          const order = {
              'get': '0', 'post': '1', 'patch': '2', 'put': '3', 'delete': '4',
              'head': '5', 'options': '6', 'connect': '7', 'trace': '8'
          };
          return (
              order[a.get("method")].localeCompare(order[b.get("method")])
              || a.get("path").localeCompare(b.get("path"))
          );
}
like image 151
Peter Avatar answered Nov 28 '22 18:11

Peter