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

feat(config): add DefaultModelsExpandDepth set the default expansion depth for models #107

Merged
merged 4 commits into from
Aug 23, 2023
Merged
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
56 changes: 37 additions & 19 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ var WrapHandler = Handler()
// Config stores httpSwagger configuration variables.
type Config struct {
// The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
URL string
DocExpansion string
DomID string
InstanceName string
BeforeScript template.JS
AfterScript template.JS
Plugins []template.JS
UIConfig map[template.JS]template.JS
DeepLinking bool
PersistAuthorization bool
Layout SwaggerLayout
URL string
DocExpansion string
DomID string
InstanceName string
BeforeScript template.JS
AfterScript template.JS
Plugins []template.JS
UIConfig map[template.JS]template.JS
DeepLinking bool
PersistAuthorization bool
Layout SwaggerLayout
DefaultModelsExpandDepth ModelsExpandDepthType
}

// URL presents the url pointing to API definition (normally swagger.json or swagger.yaml).
Expand Down Expand Up @@ -124,15 +125,31 @@ func Layout(layout SwaggerLayout) func(*Config) {
}
}

type ModelsExpandDepthType int

const (
ShowModel ModelsExpandDepthType = 1
HideModel ModelsExpandDepthType = -1
)

// DefaultModelsExpandDepth presents the model of response and request.
// set the default expansion depth for models
func DefaultModelsExpandDepth(defaultModelsExpandDepth ModelsExpandDepthType) func(*Config) {
return func(c *Config) {
c.DefaultModelsExpandDepth = defaultModelsExpandDepth
}
}

func newConfig(configFns ...func(*Config)) *Config {
config := Config{
URL: "doc.json",
DocExpansion: "list",
DomID: "swagger-ui",
InstanceName: "swagger",
DeepLinking: true,
PersistAuthorization: false,
Layout: StandaloneLayout,
URL: "doc.json",
DocExpansion: "list",
DomID: "swagger-ui",
InstanceName: "swagger",
DeepLinking: true,
PersistAuthorization: false,
Layout: StandaloneLayout,
DefaultModelsExpandDepth: ShowModel,
}

for _, fn := range configFns {
Expand Down Expand Up @@ -296,7 +313,8 @@ window.onload = function() {
{{- range $k, $v := .UIConfig}}
{{$k}}: {{$v}},
{{- end}}
layout: "{{$.Layout}}"
layout: "{{$.Layout}}",
defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}
})

window.ui = ui
Expand Down
34 changes: 26 additions & 8 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,13 @@ func TestUIConfigOptions(t *testing.T) {
{
desc: "default configuration",
cfg: &Config{
URL: "doc.json",
DeepLinking: true,
DocExpansion: "list",
DomID: "swagger-ui",
PersistAuthorization: false,
Layout: StandaloneLayout,
URL: "doc.json",
DeepLinking: true,
DocExpansion: "list",
DomID: "swagger-ui",
PersistAuthorization: false,
Layout: StandaloneLayout,
DefaultModelsExpandDepth: ShowModel,
},
exp: `window.onload = function() {

Expand All @@ -400,7 +401,8 @@ func TestUIConfigOptions(t *testing.T) {
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
layout: "StandaloneLayout",
defaultModelsExpandDepth: 1
})

window.ui = ui
Expand Down Expand Up @@ -432,6 +434,7 @@ func TestUIConfigOptions(t *testing.T) {
"onComplete": `() => { window.ui.setBasePath('v3'); }`,
"defaultModelRendering": `"model"`,
},
DefaultModelsExpandDepth: HideModel,
},
exp: `window.onload = function() {
const SomePlugin = (system) => ({
Expand All @@ -458,7 +461,8 @@ func TestUIConfigOptions(t *testing.T) {
defaultModelRendering: "model",
onComplete: () => { window.ui.setBasePath('v3'); },
showExtensions: true,
layout: "StandaloneLayout"
layout: "StandaloneLayout",
defaultModelsExpandDepth: -1
})

window.ui = ui
Expand Down Expand Up @@ -537,3 +541,17 @@ func TestUIConfigOptions(t *testing.T) {
})
}
}

func TestDefaultModelsExpandDepth(t *testing.T) {
cfg := newConfig()
// Default value
assert.Equal(t, ShowModel, cfg.DefaultModelsExpandDepth)

// Set hide
DefaultModelsExpandDepth(HideModel)(cfg)
assert.Equal(t, HideModel, cfg.DefaultModelsExpandDepth)

// Set show
DefaultModelsExpandDepth(ShowModel)(cfg)
assert.Equal(t, ShowModel, cfg.DefaultModelsExpandDepth)
}
Loading