Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order Springdoc operation parameters alphabetically

Migrating from springfox - springdoc

springfox generated an openApi definition that ordered all the parameters in alphabetical order, I've migrated to springdoc, but have been unable to find a way to keep the springfox ordering(alpha) of the parameters, e.g.

Controller:

getPerson(name, address, mobile)

springfox generated openApi definition:

getPersonService(address, mobile, name)

springdoc generated openApi definition:

getPersonService(name, address, mobile)

There are properties to order other aspects of the generated definition with:

springdoc.swagger-ui.operationsSorter=method
springdoc.swagger-ui.tagsSorter=alpha
springdoc.writer-with-order-by-keys: true

I have been unable to find a property to order the operation parameters, is there a setting to accomplish this? or can it be achieved cleanly with: OpenApiCustomiser or OperationCustomizer

like image 446
JTK Avatar asked Nov 15 '25 09:11

JTK


2 Answers

Here's a solution using the OperationCustomizer (Kotlin Syntax):

@Bean
fun parameterOrderCustomizer(): OperationCustomizer {
    return OperationCustomizer { operation, _ ->
        operation.apply {
            parameters = parameters?.sortedBy { it.name }
        }
    }
}

and in case youre using grouped apis, dont forget to also add them to the groups via .addOperationCustomizer(parameterOrderCustomizer())

like image 136
Remmli Avatar answered Nov 17 '25 08:11

Remmli


Update your application.property file with:

springdoc.writer-with-order-by-keys=true

or in application.yaml file

springdoc
 writer-with-order-by-keys: true

Paths, schemas and properties is sorted ascending alphabetically perfectly fine

like image 21
myset Avatar answered Nov 17 '25 09:11

myset