diff --git a/swagger.go b/swagger.go index 92c246b..89b2038 100644 --- a/swagger.go +++ b/swagger.go @@ -181,6 +181,7 @@ func Handler(configFns ...func(*Config)) http.HandlerFunc { // create a template with name index, _ := template.New("swagger_index.html").Parse(indexTempl) + indexJs, _ := template.New("swagger_index.js").Parse(indexJsTempl) re := regexp.MustCompile(`^(.*/)([^?].*)?[?|.]*$`) @@ -212,6 +213,8 @@ func Handler(configFns ...func(*Config)) http.HandlerFunc { switch path { case "index.html": _ = index.Execute(w, config) + case "index.js": + _ = indexJs.Execute(w, config) case "doc.json": doc, err := swag.ReadDoc(config.InstanceName) if err != nil { @@ -236,6 +239,44 @@ func Handler(configFns ...func(*Config)) http.HandlerFunc { } } +const indexJsTempl = ` +window.onload = function() { + {{- if .BeforeScript}} + {{.BeforeScript}} + {{- end}} + // Build a system + const ui = SwaggerUIBundle({ + url: "{{.URL}}", + deepLinking: {{.DeepLinking}}, + docExpansion: "{{.DocExpansion}}", + dom_id: "#{{.DomID}}", + persistAuthorization: {{.PersistAuthorization}}, + validatorUrl: null, + presets: [ + SwaggerUIBundle.presets.apis, + SwaggerUIStandalonePreset + ], + plugins: [ + SwaggerUIBundle.plugins.DownloadUrl + {{- range $plugin := .Plugins }}, + {{$plugin}} + {{- end}} + ], + {{- range $k, $v := .UIConfig}} + {{$k}}: {{$v}}, + {{- end}} + layout: "{{$.Layout}}", + defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}, + showExtensions: {{.ShowExtensions}} + }) + + window.ui = ui + {{- if .AfterScript}} + {{.AfterScript}} + {{- end}} +} +` + const indexTempl = ` @@ -302,45 +343,9 @@ const indexTempl = `
+ - diff --git a/swagger_test.go b/swagger_test.go index 8ef6168..74ec6b5 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -300,83 +300,6 @@ func TestUIConfigOptions(t *testing.T) { exp string } - hdr := ` - - - - - Swagger UI - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - -` - fixtures := []fixture{ { desc: "default configuration", @@ -389,14 +312,15 @@ func TestUIConfigOptions(t *testing.T) { Layout: StandaloneLayout, DefaultModelsExpandDepth: ShowModel, }, - exp: `window.onload = function() { - + exp: ` +window.onload = function() { + // Build a system const ui = SwaggerUIBundle({ url: "doc.json", - deepLinking: true , + deepLinking: true, docExpansion: "list", dom_id: "#swagger-ui", - persistAuthorization: false , + persistAuthorization: false, validatorUrl: null, presets: [ SwaggerUIBundle.presets.apis, @@ -406,11 +330,13 @@ func TestUIConfigOptions(t *testing.T) { SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout", - defaultModelsExpandDepth: 1 + defaultModelsExpandDepth: 1, + showExtensions: false }) window.ui = ui -}`, +} +`, }, { desc: "script configuration", @@ -440,18 +366,19 @@ func TestUIConfigOptions(t *testing.T) { }, DefaultModelsExpandDepth: HideModel, }, - exp: `window.onload = function() { - const SomePlugin = (system) => ({ + exp: ` +window.onload = function() { + const SomePlugin = (system) => ({ // Some plugin }); - + // Build a system const ui = SwaggerUIBundle({ url: "swagger.json", - deepLinking: false , + deepLinking: false, docExpansion: "none", dom_id: "#swagger-ui-id", - persistAuthorization: true , + persistAuthorization: true, validatorUrl: null, presets: [ SwaggerUIBundle.presets.apis, @@ -462,11 +389,12 @@ func TestUIConfigOptions(t *testing.T) { SomePlugin, AnotherPlugin ], - defaultModelRendering: "model", - onComplete: () => { window.ui.setBasePath('v3'); }, + defaultModelRendering: "model", + onComplete: () => { window.ui.setBasePath('v3'); }, showExtensions: true, layout: "StandaloneLayout", - defaultModelsExpandDepth: -1 + defaultModelsExpandDepth: -1, + showExtensions: false }) window.ui = ui @@ -474,24 +402,25 @@ func TestUIConfigOptions(t *testing.T) { // Do something }; someOtherCode(); -}`, +} +`, }, } for _, fix := range fixtures { t.Run(fix.desc, func(t *testing.T) { - tmpl := template.New("swagger_index.html") - index, err := tmpl.Parse(indexTempl) + tmpl := template.New("swagger_index.js") + indexJs, err := tmpl.Parse(indexJsTempl) if err != nil { t.Fatal(err) } buf := bytes.NewBuffer(nil) - if err := index.Execute(buf, fix.cfg); err != nil { + if err := indexJs.Execute(buf, fix.cfg); err != nil { t.Fatal(err) } - exp := hdr + fix.exp + ftr + exp := fix.exp // Compare line by line explns := strings.Split(exp, "\n")