This commit is contained in:
Aleem Isiaka 2023-12-10 08:18:18 -07:00 committed by GitHub
commit 3c8ff26ec8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 3 deletions

View File

@ -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_). | | 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. | | 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. | | Oauth2DefaultClientID | string | "" | If set, it's used to prepopulate the _client_id_ field of the OAuth2 Authorization dialog. |
| SupportedSubmitMethods | []string | ["get", "put", "post", "delete", "options", "head", "patch", "trace"] | This controls the methods that has Try It Out buttons. Set the methods to support or pass an empty string slice to support none |

View File

@ -1,6 +1,7 @@
package ginSwagger package ginSwagger
import ( import (
"fmt"
htmlTemplate "html/template" htmlTemplate "html/template"
"net/http" "net/http"
"os" "os"
@ -24,6 +25,7 @@ type swaggerConfig struct {
DeepLinking bool DeepLinking bool
PersistAuthorization bool PersistAuthorization bool
Oauth2DefaultClientID string Oauth2DefaultClientID string
SupportedSubmitMethods string
} }
// Config stores ginSwagger configuration variables. // Config stores ginSwagger configuration variables.
@ -37,9 +39,24 @@ type Config struct {
DeepLinking bool DeepLinking bool
PersistAuthorization bool PersistAuthorization bool
Oauth2DefaultClientID string Oauth2DefaultClientID string
SupportedSubmitMethods []string
}
// supportedMethodsToJSArray converts a list of string to valid javascript array as a go string
func supportedMethodsToJSArray(supportedMethods []string) string {
jsStr := "["
for i, mtd := range supportedMethods {
sep := ","
if i == 0 || i+1 > len(supportedMethods) {
sep = ""
}
jsStr = fmt.Sprintf("%s%s \"%s\"", jsStr, sep, mtd)
}
return fmt.Sprintf("%s]", jsStr)
} }
func (config Config) toSwaggerConfig() swaggerConfig { func (config Config) toSwaggerConfig() swaggerConfig {
return swaggerConfig{ return swaggerConfig{
URL: config.URL, URL: config.URL,
DeepLinking: config.DeepLinking, DeepLinking: config.DeepLinking,
@ -51,6 +68,7 @@ func (config Config) toSwaggerConfig() swaggerConfig {
Title: config.Title, Title: config.Title,
PersistAuthorization: config.PersistAuthorization, PersistAuthorization: config.PersistAuthorization,
Oauth2DefaultClientID: config.Oauth2DefaultClientID, Oauth2DefaultClientID: config.Oauth2DefaultClientID,
SupportedSubmitMethods: supportedMethodsToJSArray(config.SupportedSubmitMethods),
} }
} }
@ -106,6 +124,14 @@ func Oauth2DefaultClientID(oauth2DefaultClientID string) func(*Config) {
} }
} }
// SupportedSubmitMethods set the supported methods that Try It Out could be enabled for.
// The slice can contain any or all of ["get", "put", "post", "delete", "options", "head", "patch", "trace"],
func SupportedSubmitMethods(supportedSubmitMethods []string) func(*Config) {
return func(c *Config) {
c.SupportedSubmitMethods = supportedSubmitMethods
}
}
// WrapHandler wraps `http.Handler` into `gin.HandlerFunc`. // WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.
func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerFunc { func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerFunc {
var config = Config{ var config = Config{
@ -117,6 +143,9 @@ func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerF
DeepLinking: true, DeepLinking: true,
PersistAuthorization: false, PersistAuthorization: false,
Oauth2DefaultClientID: "", Oauth2DefaultClientID: "",
SupportedSubmitMethods: []string{
"get", "put", "post", "delete", "options", "head", "patch", "trace",
},
} }
for _, c := range options { for _, c := range options {
@ -255,6 +284,7 @@ window.onload = function() {
url: "{{.URL}}", url: "{{.URL}}",
dom_id: '#swagger-ui', dom_id: '#swagger-ui',
validatorUrl: null, validatorUrl: null,
supportedSubmitMethods: {{.SupportedSubmitMethods}},
oauth2RedirectUrl: {{.Oauth2RedirectURL}}, oauth2RedirectUrl: {{.Oauth2RedirectURL}},
persistAuthorization: {{.PersistAuthorization}}, persistAuthorization: {{.PersistAuthorization}},
presets: [ presets: [

View File

@ -254,3 +254,16 @@ func TestOauth2DefaultClientID(t *testing.T) {
configFunc(&cfg) configFunc(&cfg)
assert.Equal(t, "", cfg.Oauth2DefaultClientID) assert.Equal(t, "", cfg.Oauth2DefaultClientID)
} }
func TestSupportedSubmitMethods(t *testing.T) {
var cfg Config
assert.Equal(t, 0, len(cfg.SupportedSubmitMethods))
configFunc := SupportedSubmitMethods([]string{"get"})
configFunc(&cfg)
assert.Equal(t, []string{"get"}, cfg.SupportedSubmitMethods)
configFunc = SupportedSubmitMethods([]string{})
configFunc(&cfg)
assert.Equal(t, []string{}, cfg.SupportedSubmitMethods)
}