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

Add support for Swagger UI config param defaultModelExpandDepth #279

Open
wants to merge 1 commit 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
19 changes: 16 additions & 3 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type swaggerConfig struct {
Title string
Oauth2RedirectURL template.JS
DefaultModelsExpandDepth int
DefaultModelExpandDepth int
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
Expand All @@ -33,6 +34,7 @@ type Config struct {
InstanceName string
Title string
DefaultModelsExpandDepth int
DefaultModelExpandDepth int
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
Expand All @@ -44,6 +46,7 @@ func (config Config) toSwaggerConfig() swaggerConfig {
DeepLinking: config.DeepLinking,
DocExpansion: config.DocExpansion,
DefaultModelsExpandDepth: config.DefaultModelsExpandDepth,
DefaultModelExpandDepth: config.DefaultModelExpandDepth,
Oauth2RedirectURL: "`${window.location.protocol}//${window.location.host}$" +
"{window.location.pathname.split('/').slice(0, window.location.pathname.split('/').length - 1).join('/')}" +
"/oauth2-redirect.html`",
Expand Down Expand Up @@ -82,6 +85,14 @@ func DefaultModelsExpandDepth(depth int) func(*Config) {
}
}

// DefaultModelsExpandDepth set the default expansion depth for the
// model on the model-example section.
func DefaultModelExpandDepth(depth int) func(*Config) {
return func(c *Config) {
c.DefaultModelExpandDepth = depth
}
}

// InstanceName set the instance name that was used to generate the swagger documents
// Defaults to swag.Name ("swagger").
func InstanceName(name string) func(*Config) {
Expand Down Expand Up @@ -113,6 +124,7 @@ func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerF
InstanceName: swag.Name,
Title: "Swagger UI",
DefaultModelsExpandDepth: 1,
DefaultModelExpandDepth: 1,
DeepLinking: true,
PersistAuthorization: false,
Oauth2DefaultClientID: "",
Expand Down Expand Up @@ -307,10 +319,11 @@ window.onload = function() {
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
layout: "StandaloneLayout",
docExpansion: "{{.DocExpansion}}",
deepLinking: {{.DeepLinking}},
defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}
deepLinking: {{.DeepLinking}},
defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}},
defaultModelExpandDepth: {{.DefaultModelExpandDepth}}
})

const defaultClientId = "{{.Oauth2DefaultClientID}}";
Expand Down
16 changes: 16 additions & 0 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ func TestDefaultModelsExpandDepth(t *testing.T) {
assert.Equal(t, expected, cfg.DefaultModelsExpandDepth)
}

func TestDefaultModelExpandDepth(t *testing.T) {
var cfg Config

assert.Equal(t, 0, cfg.DefaultModelExpandDepth)

expected := -1
configFunc := DefaultModelExpandDepth(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.DefaultModelExpandDepth)

expected = 1
configFunc = DefaultModelExpandDepth(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.DefaultModelExpandDepth)
}

func TestInstanceName(t *testing.T) {
var cfg Config

Expand Down