From e021313b42c549c26230b3bc385151b817014e4e Mon Sep 17 00:00:00 2001 From: pi Date: Tue, 29 Aug 2023 17:53:42 +0200 Subject: [PATCH] Add support for Swagger UI config param defaultModelExpandDepth --- swagger.go | 19 ++++++++++++++++--- swagger_test.go | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/swagger.go b/swagger.go index 9314b81..2351afb 100644 --- a/swagger.go +++ b/swagger.go @@ -20,6 +20,7 @@ type swaggerConfig struct { Title string Oauth2RedirectURL template.JS DefaultModelsExpandDepth int + DefaultModelExpandDepth int DeepLinking bool PersistAuthorization bool Oauth2DefaultClientID string @@ -33,6 +34,7 @@ type Config struct { InstanceName string Title string DefaultModelsExpandDepth int + DefaultModelExpandDepth int DeepLinking bool PersistAuthorization bool Oauth2DefaultClientID string @@ -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`", @@ -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) { @@ -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: "", @@ -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}}"; diff --git a/swagger_test.go b/swagger_test.go index 337cc8e..3a78c76 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -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