Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add filter option #310

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
11 changes: 11 additions & 0 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type swaggerConfig struct {
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
Filter bool
}

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

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

Expand Down Expand Up @@ -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{
Expand All @@ -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 {
Expand Down Expand Up @@ -264,6 +274,7 @@ window.onload = function() {
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
filter: {{.Filter}},
layout: "StandaloneLayout",
docExpansion: "{{.DocExpansion}}",
deepLinking: {{.DeepLinking}},
Expand Down
13 changes: 13 additions & 0 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}