diff --git a/swagger.go b/swagger.go index dfbc77f..1d24f72 100644 --- a/swagger.go +++ b/swagger.go @@ -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). @@ -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 { @@ -296,7 +313,8 @@ window.onload = function() { {{- range $k, $v := .UIConfig}} {{$k}}: {{$v}}, {{- end}} - layout: "{{$.Layout}}" + layout: "{{$.Layout}}", + defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}} }) window.ui = ui diff --git a/swagger_test.go b/swagger_test.go index e9fd9a8..f7941b1 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -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() { @@ -400,7 +401,8 @@ func TestUIConfigOptions(t *testing.T) { plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], - layout: "StandaloneLayout" + layout: "StandaloneLayout", + defaultModelsExpandDepth: 1 }) window.ui = ui @@ -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) => ({ @@ -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 @@ -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) +}