Skip to content

Commit

Permalink
Add ScriptSrcs to add extra scripts URLs to load
Browse files Browse the repository at this point in the history
Summary: This allows importing plugins from local and external hosts.

```
	r.Get("/swagger/*", httpSwagger.Handler(
		httpSwagger.ScriptSrc("/static/swagger-helper.js"),
		httpSwagger.ScriptSrc("https://unpkg.com/swagger-ui-plugin-disable-try-it-out-without-servers@1.0.0/build/index.js"),
		httpSwagger.Plugins([]string{"DisableTryItOutWithoutServersPlugin"},
		httpSwagger.URL("http://localhost:1323/swagger/doc.json"), //The url pointing to API definition
	))
```

Test Plan: Updated existing tests. Added example in example/go-chi/
  • Loading branch information
Zhomart committed Jan 13, 2023
1 parent c8d62bf commit b54cdda
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
BeforeScript template.JS
AfterScript template.JS
Plugins []template.JS
ScriptSrcs []string // URLs to the extra scripts to load
UIConfig map[template.JS]template.JS
DeepLinking bool
PersistAuthorization bool
Expand Down Expand Up @@ -84,6 +85,13 @@ func Plugins(plugins []string) func(*Config) {
}
}

// Adds `<script src="src"></script>`
func ScriptSrc(src string) func(*Config) {
return func(c *Config) {
c.ScriptSrcs = append(c.ScriptSrcs, src)
}
}

// UIConfig specifies additional SwaggerUIBundle config object properties.
func UIConfig(props map[string]string) func(*Config) {
return func(c *Config) {
Expand Down Expand Up @@ -259,6 +267,9 @@ const indexTempl = `<!-- HTML for static distribution bundle build -->
<script src="./swagger-ui-bundle.js"> </script>
<script src="./swagger-ui-standalone-preset.js"> </script>
{{- range $script := .ScriptSrcs }}
<script src="{{$script}}"> </script>
{{- end}}
<script>
window.onload = function() {
{{- if .BeforeScript}}
Expand Down
11 changes: 11 additions & 0 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ func TestConfigURL(t *testing.T) {
// Some plugin
});`),
},
{
desc: "configure ScriptSrcs",
exp: &Config{
ScriptSrcs: []string{`/scripts/helper.js`},
},
cfgfn: ScriptSrc(`/scripts/helper.js`),
},
}

for _, fix := range fixtures {
Expand Down Expand Up @@ -318,6 +325,8 @@ func TestUIConfigOptions(t *testing.T) {
<script src="./swagger-ui-bundle.js"> </script>
<script src="./swagger-ui-standalone-preset.js"> </script>
<script src="./a.js"> </script>
<script src="./b.js"> </script>
<script>
`
ftr := `
Expand All @@ -336,6 +345,7 @@ func TestUIConfigOptions(t *testing.T) {
DocExpansion: "list",
DomID: "swagger-ui",
PersistAuthorization: false,
ScriptSrcs: []string{"./a.js", "./b.js"},
},
exp: `window.onload = function() {
Expand Down Expand Up @@ -379,6 +389,7 @@ func TestUIConfigOptions(t *testing.T) {
"SomePlugin",
"AnotherPlugin",
},
ScriptSrcs: []string{"./a.js", "./b.js"},
UIConfig: map[template.JS]template.JS{
"showExtensions": "true",
"onComplete": `() => { window.ui.setBasePath('v3'); }`,
Expand Down

0 comments on commit b54cdda

Please sign in to comment.