diff --git a/README.md b/README.md index 7e07033..a702bae 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..497c71e 100644 --- a/swagger.go +++ b/swagger.go @@ -1,6 +1,7 @@ package ginSwagger import ( + "fmt" htmlTemplate "html/template" "net/http" "os" @@ -24,6 +25,7 @@ type swaggerConfig struct { DeepLinking bool PersistAuthorization bool Oauth2DefaultClientID string + OperationsSorter string } // Config stores ginSwagger configuration variables. @@ -37,6 +39,7 @@ type Config struct { DeepLinking bool PersistAuthorization bool Oauth2DefaultClientID string + OperationsSorter string } func (config Config) toSwaggerConfig() swaggerConfig { @@ -51,6 +54,7 @@ func (config Config) toSwaggerConfig() swaggerConfig { Title: config.Title, PersistAuthorization: config.PersistAuthorization, Oauth2DefaultClientID: config.Oauth2DefaultClientID, + OperationsSorter: config.OperationsSorter, } } @@ -75,6 +79,18 @@ 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) { + if operationsSorter == "alpha" || operationsSorter == "method" { + c.OperationsSorter = fmt.Sprintf(`"%s"`, operationsSorter) + return + } + 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 +133,7 @@ func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerF DeepLinking: true, PersistAuthorization: false, Oauth2DefaultClientID: "", + OperationsSorter: `"alpha"`, } for _, c := range options { @@ -138,6 +155,15 @@ func CustomWrapHandler(config *Config, handler *webdav.Handler) gin.HandlerFunc config.Title = "Swagger UI" } + if config.OperationsSorter == "" { + config.OperationsSorter = `"alpha"` + } + + //wraps the operationsSorter string options in quotes to both support "alpha", "method" and custom functions + if config.OperationsSorter == "alpha" || config.OperationsSorter == "method" { + config.OperationsSorter = fmt.Sprintf(`"%s"`, config.OperationsSorter) + } + // create a template with name index, _ := htmlTemplate.New("swagger_index.html").Parse(swaggerIndexTpl) js, _ := textTemplate.New("swagger_index.js").Parse(swaggerJSTpl) @@ -257,6 +283,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..62d16fb 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -254,3 +254,20 @@ 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(`alpha`) + configFunc(&cfg) + assert.Equal(t, `"alpha"`, cfg.OperationsSorter) + + configFunc = OperationsSorter("") + configFunc(&cfg) + assert.Equal(t, "", cfg.OperationsSorter) +}