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

Fixes #278: Adds support to control the Try It Out button #281

Open
wants to merge 5 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,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. |
| 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 |
36 changes: 33 additions & 3 deletions swagger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ginSwagger

import (
"fmt"
htmlTemplate "html/template"
"net/http"
"os"
Expand All @@ -24,6 +25,7 @@ type swaggerConfig struct {
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
SupportedSubmitMethods string
}

// Config stores ginSwagger configuration variables.
Expand All @@ -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,
Expand All @@ -48,9 +65,10 @@ func (config Config) toSwaggerConfig() swaggerConfig {
Oauth2RedirectURL: "`${window.location.protocol}//${window.location.host}$" +
"{window.location.pathname.split('/').slice(0, window.location.pathname.split('/').length - 1).join('/')}" +
"/oauth2-redirect.html`",
Title: config.Title,
PersistAuthorization: config.PersistAuthorization,
Oauth2DefaultClientID: config.Oauth2DefaultClientID,
Title: config.Title,
PersistAuthorization: config.PersistAuthorization,
Oauth2DefaultClientID: config.Oauth2DefaultClientID,
SupportedSubmitMethods: supportedMethodsToJSArray(config.SupportedSubmitMethods),
}
}

Expand Down Expand Up @@ -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{
Expand All @@ -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 {
Expand Down Expand Up @@ -255,6 +284,7 @@ window.onload = function() {
url: "{{.URL}}",
dom_id: '#swagger-ui',
validatorUrl: null,
supportedSubmitMethods: {{.SupportedSubmitMethods}},
oauth2RedirectUrl: {{.Oauth2RedirectURL}},
persistAuthorization: {{.PersistAuthorization}},
presets: [
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 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)
}