From 4672a89b2f36c371ccc8cba7067361b57610fcb4 Mon Sep 17 00:00:00 2001 From: Muhammad Abdurrohman Al Fatih Date: Mon, 8 Apr 2024 12:20:49 +0700 Subject: [PATCH] feat: add ShowCommonExtensions option --- README.md | 1 + swagger.go | 14 +++++++++++++- swagger_test.go | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9c20ddd..10ee4b9 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. | +| ShowCommonExtensions | bool | false | Controls the display of extensions (pattern, maxLength, minLength, maximum, minimum) fields and values for Parameters. | \ No newline at end of file diff --git a/swagger.go b/swagger.go index 9206c78..b4360e6 100644 --- a/swagger.go +++ b/swagger.go @@ -24,6 +24,7 @@ type swaggerConfig struct { DeepLinking bool PersistAuthorization bool Oauth2DefaultClientID string + ShowCommonExtensions bool } // Config stores ginSwagger configuration variables. @@ -37,6 +38,7 @@ type Config struct { DeepLinking bool PersistAuthorization bool Oauth2DefaultClientID string + ShowCommonExtensions bool } func (config Config) toSwaggerConfig() swaggerConfig { @@ -51,6 +53,7 @@ func (config Config) toSwaggerConfig() swaggerConfig { Title: config.Title, PersistAuthorization: config.PersistAuthorization, Oauth2DefaultClientID: config.Oauth2DefaultClientID, + ShowCommonExtensions: config.ShowCommonExtensions, } } @@ -106,6 +109,13 @@ func Oauth2DefaultClientID(oauth2DefaultClientID string) func(*Config) { } } +// ShowCommonExtensions set the swagger show common extensions configuration. +func ShowCommonExtensions(showCommonExtensions bool) func(*Config) { + return func(c *Config) { + c.ShowCommonExtensions = showCommonExtensions + } +} + // WrapHandler wraps `http.Handler` into `gin.HandlerFunc`. func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerFunc { var config = Config{ @@ -117,6 +127,7 @@ func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerF DeepLinking: true, PersistAuthorization: false, Oauth2DefaultClientID: "", + ShowCommonExtensions: false, } for _, c := range options { @@ -267,7 +278,8 @@ window.onload = function() { layout: "StandaloneLayout", docExpansion: "{{.DocExpansion}}", deepLinking: {{.DeepLinking}}, - defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}} + defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}, + showCommonExtensions: {{.ShowCommonExtensions}} }) const defaultClientId = "{{.Oauth2DefaultClientID}}"; diff --git a/swagger_test.go b/swagger_test.go index a7a825b..7049e90 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -254,3 +254,17 @@ func TestOauth2DefaultClientID(t *testing.T) { configFunc(&cfg) assert.Equal(t, "", cfg.Oauth2DefaultClientID) } + +func TestShowCommonExtensions(t *testing.T) { + var cfg Config + assert.Equal(t, false, cfg.ShowCommonExtensions) + + configFunc := ShowCommonExtensions(true) + configFunc(&cfg) + assert.Equal(t, true, cfg.ShowCommonExtensions) + + configFunc = ShowCommonExtensions(false) + configFunc(&cfg) + assert.Equal(t, false, cfg.ShowCommonExtensions) + +}