Add supported methods
This commit is contained in:
parent
3b4340f1a7
commit
14dd310733
30
swagger.go
30
swagger.go
|
|
@ -1,6 +1,7 @@
|
|||
package ginSwagger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
htmlTemplate "html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
|
|
@ -24,6 +25,7 @@ type swaggerConfig struct {
|
|||
DeepLinking bool
|
||||
PersistAuthorization bool
|
||||
Oauth2DefaultClientID string
|
||||
SupportedSubmitMethods string
|
||||
}
|
||||
|
||||
// Config stores ginSwagger configuration variables.
|
||||
|
|
@ -37,9 +39,24 @@ type Config struct {
|
|||
DeepLinking bool
|
||||
PersistAuthorization bool
|
||||
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 {
|
||||
|
||||
return swaggerConfig{
|
||||
URL: config.URL,
|
||||
DeepLinking: config.DeepLinking,
|
||||
|
|
@ -51,6 +68,7 @@ func (config Config) toSwaggerConfig() swaggerConfig {
|
|||
Title: config.Title,
|
||||
PersistAuthorization: config.PersistAuthorization,
|
||||
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`.
|
||||
func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerFunc {
|
||||
var config = Config{
|
||||
|
|
@ -117,6 +143,9 @@ func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerF
|
|||
DeepLinking: true,
|
||||
PersistAuthorization: false,
|
||||
Oauth2DefaultClientID: "",
|
||||
SupportedSubmitMethods: []string{
|
||||
"get", "put", "post", "delete", "options", "head", "patch", "trace",
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range options {
|
||||
|
|
@ -255,6 +284,7 @@ window.onload = function() {
|
|||
url: "{{.URL}}",
|
||||
dom_id: '#swagger-ui',
|
||||
validatorUrl: null,
|
||||
supportedSubmitMethods: {{.SupportedSubmitMethods}},
|
||||
oauth2RedirectUrl: {{.Oauth2RedirectURL}},
|
||||
persistAuthorization: {{.PersistAuthorization}},
|
||||
presets: [
|
||||
|
|
|
|||
|
|
@ -254,3 +254,16 @@ func TestOauth2DefaultClientID(t *testing.T) {
|
|||
configFunc(&cfg)
|
||||
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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue