Issue
Quiet pretty straighforward question: how can I sort the endpoints in swagger-ui in v2.2.6? I'm using springfox for the java part.
In my main page of swagger, I'm having a collection of resources grouped by tags. These tags are in a random order. I would like to have it in a custom order (and if it's not possible, alphabetically). My SwaggerConfig.java file:
package cat.meteo.apiinterna.commons.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.tags(
new Tag("XEMA Me", "1"),
new Tag("XEMA Ul", "2"),
new Tag("XEMA Ag", "3"),
new Tag("Prono", "4"),
new Tag("Sound", "5")
)
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API REST")
.description("Self-documented API")
.version("v0.1.0")
.build();
}
}
And this is the tag resulting json file that swagger-ui uses in the same order. As you can see the order seems to be random. Not the java tags order, not alphabetically. (http://localhost:8080/XXX/v2/api-docs)
"tags": [
{
"name": "XEMA Ul",
"description": "2"
},
{
"name": "XEMA Me",
"description": "1"
},
{
"name": "XEMA Ag",
"description": "3"
},
{
"name": "Sound",
"description": "5"
},
{
"name": "Prono",
"description": "4"
}
]
Solution
After digging into the problem, I see that new versions of swagger-ui do not support custom ordering as an option. Springfox also give no chance of reorder the generated json, so the only way is to implement a "workaround" in swagger-ui.
In render function, line 21766 of swagger-ui.js file (v2.2.6):
// Workaround: alphabetically ordered tags
this.api.apisArray.sort(function (a, b) {
if (a.tag < b.tag)
return -1;
if (a.tag > b.tag)
return 1;
return 0;
})
With this code the UI shows the tags ordered.
Answered By - Emilio