diff --git a/README.md b/README.md index 918702f..3e8e321 100644 --- a/README.md +++ b/README.md @@ -126,4 +126,26 @@ Demo project tree, `swag init` is run at relative `.` ├── go.mod ├── go.sum └── main.go -``` \ No newline at end of file +``` + +## Configuration + +You can configure Swagger using different configuration options + +```go +func main() { + r := gin.New() + + ginSwagger.WrapHandler(swaggerFiles.Handler, + ginSwagger.URL("http://localhost:8080/swagger/doc.json"), + ginSwagger.DefaultModelsExpandDepth(-1))) + + r.Run() +} +``` + +| Option | Type | Default | Description | +|--------------------------|--------|------------|---------------------------------------------------------------------------| +| URL | string | "doc.json" | URL pointing to API definition | +| DeepLinking | bool | true | Swagger deeplinking configuration | +| DefaultModelsExpandDepth | int | 1 | Default expansion depth for models (set to -1 completely hide the models) | diff --git a/swagger.go b/swagger.go index 1698cd1..bed2193 100644 --- a/swagger.go +++ b/swagger.go @@ -17,8 +17,9 @@ import ( // Config stores ginSwagger configuration variables. type Config struct { //The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`. - URL string - DeepLinking bool + URL string + DeepLinking bool + DefaultModelsExpandDepth int } // URL presents the url pointing to API definition (normally swagger.json or swagger.yaml). @@ -35,11 +36,20 @@ func DeepLinking(deepLinking bool) func(c *Config) { } } +// DefaultModelsExpandDepth set the default expansion depth for models +// (set to -1 completely hide the models). +func DefaultModelsExpandDepth(depth int) func(c *Config) { + return func(c *Config) { + c.DefaultModelsExpandDepth = depth + } +} + // WrapHandler wraps `http.Handler` into `gin.HandlerFunc`. func WrapHandler(h *webdav.Handler, confs ...func(c *Config)) gin.HandlerFunc { defaultConfig := &Config{ - URL: "doc.json", - DeepLinking: true, + URL: "doc.json", + DeepLinking: true, + DefaultModelsExpandDepth: 1, } for _, c := range confs { @@ -61,8 +71,9 @@ func CustomWrapHandler(config *Config, h *webdav.Handler) gin.HandlerFunc { return func(c *gin.Context) { type swaggerUIBundle struct { - URL string - DeepLinking bool + URL string + DeepLinking bool + DefaultModelsExpandDepth int } var matches []string @@ -91,8 +102,9 @@ func CustomWrapHandler(config *Config, h *webdav.Handler) gin.HandlerFunc { switch path { case "index.html": index.Execute(c.Writer, &swaggerUIBundle{ - URL: config.URL, - DeepLinking: config.DeepLinking, + URL: config.URL, + DeepLinking: config.DeepLinking, + DefaultModelsExpandDepth: config.DefaultModelsExpandDepth, }) case "doc.json": doc, err := swag.ReadDoc() @@ -226,7 +238,8 @@ window.onload = function() { SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout", - deepLinking: {{.DeepLinking}} + deepLinking: {{.DeepLinking}}, + defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}} }) window.ui = ui diff --git a/swagger_test.go b/swagger_test.go index 694a84b..567fc96 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -157,3 +157,11 @@ func TestDeepLinking(t *testing.T) { configFunc(&cfg) assert.Equal(t, expected, cfg.DeepLinking) } + +func TestDefaultModelsExpandDepth(t *testing.T) { + expected := -1 + cfg := Config{} + configFunc := DefaultModelsExpandDepth(expected) + configFunc(&cfg) + assert.Equal(t, expected, cfg.DefaultModelsExpandDepth) +}