Skip to content

Commit

Permalink
Feat: add option to sort list of endpoints (swaggo#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhlipTIP committed Oct 16, 2023
1 parent 3b4340f commit d6a2477
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)\` |
27 changes: 27 additions & 0 deletions swagger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ginSwagger

import (
"fmt"
htmlTemplate "html/template"
"net/http"
"os"
Expand All @@ -24,6 +25,7 @@ type swaggerConfig struct {
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
OperationsSorter string
}

// Config stores ginSwagger configuration variables.
Expand All @@ -37,6 +39,7 @@ type Config struct {
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
OperationsSorter string
}

func (config Config) toSwaggerConfig() swaggerConfig {
Expand All @@ -51,6 +54,7 @@ func (config Config) toSwaggerConfig() swaggerConfig {
Title: config.Title,
PersistAuthorization: config.PersistAuthorization,
Oauth2DefaultClientID: config.Oauth2DefaultClientID,
OperationsSorter: config.OperationsSorter,
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -257,6 +283,7 @@ window.onload = function() {
validatorUrl: null,
oauth2RedirectUrl: {{.Oauth2RedirectURL}},
persistAuthorization: {{.PersistAuthorization}},
operationsSorter: {{.OperationsSorter}},
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
Expand Down
17 changes: 17 additions & 0 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit d6a2477

Please sign in to comment.