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

possibility to disable try-it-out button and models depth #107

Open
wants to merge 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 59 additions & 18 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import (
// Config stores echoSwagger configuration variables.
type Config struct {
// The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `mockedSwag.json`.
URLs []string
DocExpansion string
DomID string
InstanceName string
DeepLinking bool
PersistAuthorization bool
SyntaxHighlight bool
URLs []string
DocExpansion string
DomID string
InstanceName string
DefaultModelsExpandDepth int
DeepLinking bool
PersistAuthorization bool
SyntaxHighlight bool
TryItOutEnabled bool

// The information for OAuth2 integration, if any.
OAuth *OAuthConfig
Expand Down Expand Up @@ -90,6 +92,24 @@ func PersistAuthorization(persistAuthorization bool) func(*Config) {
}
}

// DefaultModelsExpandDepth expansion depth for models (set -1 to
// completely hide the models).
// Defaults to 1
func DefaultModelsExpandDepth(depth int) func(*Config) {
return func(c *Config) {
c.DefaultModelsExpandDepth = depth
}
}

// TryItOutEnabled controls whether the "Try it out" section should be
// enabled by default.
// Defaults to true
func TryItOutEnabled(enable bool) func(*Config) {
return func(c *Config) {
c.TryItOutEnabled = enable
}
}

func OAuth(config *OAuthConfig) func(*Config) {
return func(c *Config) {
c.OAuth = config
Expand All @@ -98,13 +118,15 @@ func OAuth(config *OAuthConfig) func(*Config) {

func newConfig(configFns ...func(*Config)) *Config {
config := Config{
URLs: []string{"doc.json", "doc.yaml"},
DocExpansion: "list",
DomID: "swagger-ui",
InstanceName: "swagger",
DeepLinking: true,
PersistAuthorization: false,
SyntaxHighlight: true,
URLs: []string{"doc.json", "doc.yaml"},
DocExpansion: "list",
DomID: "swagger-ui",
InstanceName: "swagger",
DefaultModelsExpandDepth: 1,
DeepLinking: true,
PersistAuthorization: false,
SyntaxHighlight: true,
TryItOutEnabled: true,
}

for _, fn := range configFns {
Expand Down Expand Up @@ -268,6 +290,26 @@ const indexTemplate = `<!-- HTML for static distribution bundle build -->
<script src="./swagger-ui-standalone-preset.js"> </script>
<script>
window.onload = function() {
const DisableTryItOutPlugin = function() {
return {
statePlugins: {
spec: {
wrapSelectors: {
allowTryItOutFor: () => () => false
}
}
}
}
}

const plugins = [
SwaggerUIBundle.plugins.DownloadUrl
]

{{if not .TryItOutEnabled}}
plugins.push(DisableTryItOutPlugin)
{{end}}

// Build a system
const ui = SwaggerUIBundle({
urls: [
Expand All @@ -288,10 +330,9 @@ window.onload = function() {
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
plugins: plugins,
layout: "StandaloneLayout",
defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}},
})

{{if .OAuth}}
Expand Down
42 changes: 42 additions & 0 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,34 @@ func TestConfigWithOAuth(t *testing.T) {
})`)
}

func TestConfigWithDefaultModelsExpandDepth(t *testing.T) {
router := echo.New()

swaggerHandler := EchoWrapHandler(
DefaultModelsExpandDepth(-1),
)
router.GET("/*", swaggerHandler)

w := performRequest("GET", "/index.html", router)
assert.Equal(t, 200, w.Code)
body := w.Body.String()
assert.Contains(t, body, `defaultModelsExpandDepth: -1`)
}

func TestConfigWithTryItOutEnabled(t *testing.T) {
router := echo.New()

swaggerHandler := EchoWrapHandler(
TryItOutEnabled(false),
)
router.GET("/*", swaggerHandler)

w := performRequest("GET", "/index.html", router)
assert.Equal(t, 200, w.Code)
body := w.Body.String()
assert.Contains(t, body, `plugins.push(DisableTryItOutPlugin)`)
}

func TestHandlerReuse(t *testing.T) {
router := echo.New()

Expand Down Expand Up @@ -432,3 +460,17 @@ func TestOAuthNil(t *testing.T) {
OAuth(expected)(&cfg)
assert.Equal(t, expected, cfg.OAuth)
}

func TestDefaultModelsExpandDepth(t *testing.T) {
var cfg Config
expected := 1
DefaultModelsExpandDepth(expected)(&cfg)
assert.Equal(t, expected, cfg.DefaultModelsExpandDepth)
}

func TestTryItOutEnabled(t *testing.T) {
var cfg Config
expected := false
TryItOutEnabled(expected)(&cfg)
assert.Equal(t, expected, cfg.TryItOutEnabled)
}