diff --git a/README.md b/README.md index 7e07033..6cc6019 100644 --- a/README.md +++ b/README.md @@ -178,3 +178,4 @@ func main() { | InstanceName | string | "swagger" | The instance name of the swagger document. If multiple different swagger instances should be deployed on one gin router, ensure that each instance has a unique name (use the _--instanceName_ parameter to generate swagger documents with _swag init_). | | PersistAuthorization | bool | false | If set to true, it persists authorization data and it would not be lost on browser close/refresh. | | Oauth2DefaultClientID | string | "" | If set, it's used to prepopulate the _client_id_ field of the OAuth2 Authorization dialog. | +| OperationsSorter | string | "alpha" | Controls the order which APIs are sorted. Default sorts endpoints alphabetically, it also supports "method" to sort by method or a custom sort function: Function=(a => a) | \ No newline at end of file diff --git a/swagger.go b/swagger.go index 9206c78..5028bb2 100644 --- a/swagger.go +++ b/swagger.go @@ -24,6 +24,7 @@ type swaggerConfig struct { DeepLinking bool PersistAuthorization bool Oauth2DefaultClientID string + OperationsSorter string } // Config stores ginSwagger configuration variables. @@ -37,6 +38,7 @@ type Config struct { DeepLinking bool PersistAuthorization bool Oauth2DefaultClientID string + OperationsSorter string } func (config Config) toSwaggerConfig() swaggerConfig { @@ -51,6 +53,7 @@ func (config Config) toSwaggerConfig() swaggerConfig { Title: config.Title, PersistAuthorization: config.PersistAuthorization, Oauth2DefaultClientID: config.Oauth2DefaultClientID, + OperationsSorter: config.OperationsSorter, } } @@ -75,6 +78,14 @@ func DeepLinking(deepLinking bool) func(*Config) { } } +// OperationsSorter sets the swagger operationsSorter configuration. Either "alpha", "method" or Function=(a => a) +// default is "alpha" (alphabetically sorted). +func OperationsSorter(operationsSorter string) func(*Config) { + return func(c *Config) { + c.OperationsSorter = operationsSorter + } +} + // DefaultModelsExpandDepth set the default expansion depth for models // (set to -1 completely hide the models). func DefaultModelsExpandDepth(depth int) func(*Config) { @@ -117,6 +128,7 @@ func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerF DeepLinking: true, PersistAuthorization: false, Oauth2DefaultClientID: "", + OperationsSorter: "alpha", } for _, c := range options { @@ -138,6 +150,10 @@ func CustomWrapHandler(config *Config, handler *webdav.Handler) gin.HandlerFunc config.Title = "Swagger UI" } + if config.OperationsSorter == "" { + config.OperationsSorter = "alpha" + } + // create a template with name index, _ := htmlTemplate.New("swagger_index.html").Parse(swaggerIndexTpl) js, _ := textTemplate.New("swagger_index.js").Parse(swaggerJSTpl) @@ -257,6 +273,7 @@ window.onload = function() { validatorUrl: null, oauth2RedirectUrl: {{.Oauth2RedirectURL}}, persistAuthorization: {{.PersistAuthorization}}, + operationsSorter: {{.OperationsSorter}}, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset diff --git a/swagger_test.go b/swagger_test.go index a7a825b..37499da 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -254,3 +254,16 @@ func TestOauth2DefaultClientID(t *testing.T) { configFunc(&cfg) assert.Equal(t, "", cfg.Oauth2DefaultClientID) } + +func TestOperationSorter(t *testing.T) { + var cfg Config + assert.Equal(t, "", cfg.OperationsSorter) + + configFunc := OperationsSorter("method") + configFunc(&cfg) + assert.Equal(t, "method", cfg.OperationsSorter) + + configFunc = OperationsSorter("") + configFunc(&cfg) + assert.Equal(t, "", cfg.OperationsSorter) +}