From 47ebe851e0d82871b5d2fcedb495f5793c61088b Mon Sep 17 00:00:00 2001 From: Minh Ngoc Nguyen Date: Sat, 7 Sep 2024 09:04:30 +0700 Subject: [PATCH] feat: add filter option --- README.md | 1 + swagger.go | 11 +++++++++++ swagger_test.go | 13 +++++++++++++ 3 files changed, 25 insertions(+) diff --git a/README.md b/README.md index 9c20ddd..f6ec1d0 100644 --- a/README.md +++ b/README.md @@ -204,3 +204,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. | +| Filter | bool | false | If set, it allows filtering the API by tag. | \ No newline at end of file diff --git a/swagger.go b/swagger.go index 9206c78..dae4b98 100644 --- a/swagger.go +++ b/swagger.go @@ -24,6 +24,7 @@ type swaggerConfig struct { DeepLinking bool PersistAuthorization bool Oauth2DefaultClientID string + Filter bool } // Config stores ginSwagger configuration variables. @@ -37,6 +38,7 @@ type Config struct { DeepLinking bool PersistAuthorization bool Oauth2DefaultClientID string + Filter bool } func (config Config) toSwaggerConfig() swaggerConfig { @@ -51,6 +53,7 @@ func (config Config) toSwaggerConfig() swaggerConfig { Title: config.Title, PersistAuthorization: config.PersistAuthorization, Oauth2DefaultClientID: config.Oauth2DefaultClientID, + Filter: config.Filter, } } @@ -106,6 +109,12 @@ func Oauth2DefaultClientID(oauth2DefaultClientID string) func(*Config) { } } +func Filter(filter bool) func(*Config) { + return func(c *Config) { + c.Filter = filter + } +} + // WrapHandler wraps `http.Handler` into `gin.HandlerFunc`. func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerFunc { var config = Config{ @@ -117,6 +126,7 @@ func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerF DeepLinking: true, PersistAuthorization: false, Oauth2DefaultClientID: "", + Filter: false, } for _, c := range options { @@ -264,6 +274,7 @@ window.onload = function() { plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], + filter: {{.Filter}}, layout: "StandaloneLayout", docExpansion: "{{.DocExpansion}}", deepLinking: {{.DeepLinking}}, diff --git a/swagger_test.go b/swagger_test.go index a7a825b..c8344c1 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 TestFilter(t *testing.T) { + var cfg Config + assert.Equal(t, false, cfg.Filter) + + configFunc := Filter(true) + configFunc(&cfg) + assert.Equal(t, true, cfg.Filter) + + configFunc = Filter(false) + configFunc(&cfg) + assert.Equal(t, false, cfg.Filter) +}