5

I have configured Swagger in my Spring boot application, the spring boot application has 2 controllers, casesController and AttachmentController, I want all the endpoints from my attachment controller appear after the case controller endpoints, but swagger is doing the opposite.

What swagger is doing: enter image description here

my code looks as follow:

Configuration file:

@Configuration
class OpenApiConfig(
    @Value("\${springdoc.info.title}") val title: String,
    @Value("\${springdoc.info.description}") val infoDescription: String,
    @Value("\${springdoc.info.version}") val version: String,
    @Value("\${springdoc.info.license.name}") val licenseName: String,
    @Value("\${springdoc.info.license.url}") val licenseUrl: String,
    @Value("\${springdoc.info.contact.email}") val email: String,
    @Value("\${springdoc.server.url}") val url: String,
    @Value("\${springdoc.server.description}") val description: String
) {
@Bean
fun customOpenAPIConfig(): OpenAPI {
    return OpenAPI()
            .components(
                    Components()
            )
            .info(
                    Info().title(title).description(infoDescription).version(version)
                            .license(License().name(licenseName).url(licenseUrl))
                            .contact(Contact().email(email))
            )
            .servers(listOf(Server().url(url).description(description)))
            .tags(listOf(Tag()))

 }
}

AttachmentController:

@RestController
@RequestMapping("/cases/{id}/attachments")
lass AttachmentController(private val caseManagementService: CaseManagementService) {

@Operation(
    summary = "Add attachment",
    tags = ["attachment"],
    description = "Add attachment to a case"
)
@PostMapping()
@ResponseStatus(HttpStatus.CREATED)
fun addAttachment(@RequestBody attachmentRequest: AttachmentRequestDto, @PathVariable id: String, @RequestHeader(value = "X-API-KEY") xApiKey: String): Mono<AttachmentResponseDto> {

    ....
}

CaseController:

@RestController
@RequestMapping("/cases")
class CaseController(private val caseManagementService: CaseManagementService) {

@Operation(
    summary = "Create case",
    tags = ["case"],
    description = "Create case"
)
@PostMapping()
@ResponseStatus(HttpStatus.CREATED)
fun createCase(@RequestBody caseRequest: CaseRequestDto, @RequestHeader(value = "X-API-KEY") xApiKey: String): Mono<CaseResponseDto> {

.... 
}
D.B
  • 4,009
  • 14
  • 46
  • 83
  • In my apps i just naming my endpoints with numeral.) 1. Case 2. Attachments. – Deepstack Nov 23 '20 at 06:56
  • You need to add annotations/configs that would to generate the top-level `tags` section in your OpenAPI definition (see the 2nd example [here](https://swagger.io/docs/specification/grouping-operations-with-tags/)) and in that section put the `case` tag before `Attachments`. – Helen Nov 23 '20 at 10:09
  • Sorry @Helen where should I add that annotations/config, is there a way to add it in my code? or int needs to be added within the Swagger UI? – D.B Nov 23 '20 at 11:04
  • @Deepstack could you give me an example please, i don't get it, where exactly you name your endpoints? – D.B Nov 23 '20 at 11:05
  • @D.B you can add an annotation @Tag(name = "1. Case") on method in rest controller. – Deepstack Nov 23 '20 at 11:09
  • 1
    Tag order seems to be controlled by this line: `return OpenAPI() ... .tags(listOf(Tag()))`. Can it be rewritten to add tags in a specific order, i.e. `case` first, then `Attachments`? Alternatively, see if this helps - [How do I write a custom sorter to sort my springdoc tags by name in Swagger UI?](https://stackoverflow.com/q/60810048/113116) – Helen Nov 23 '20 at 11:56
  • Thanks @Helen yep, that works. You can write your answer if u want me to accept it – D.B Nov 23 '20 at 12:21
  • Is there a way to achieve something similar using the Quarkus-based rest application? I tried to achieve something similar for my quarkus-based application but could not get anything. Please find my question here: https://stackoverflow.com/q/74627820/7584240 – BATMAN_2008 Nov 30 '22 at 12:33

1 Answers1

1

Please have a look at my answer here

In brief one way is to use combination of @Tag and property springdoc.swagger-ui.tagsSorter=alpha

ADJ
  • 1,182
  • 2
  • 14
  • 26