Skip to content

Commit

Permalink
Add support for swagger-ui persist-authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
kuklyy committed Feb 6, 2022
1 parent 4799ad4 commit b045c47
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -153,6 +153,7 @@ window.onload = function() {
docExpansion: "none",
dom_id: "#swagger-ui-id",
validatorUrl: null,
persistAuthorization: false,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
Expand Down
35 changes: 23 additions & 12 deletions swagger.go
Expand Up @@ -17,14 +17,15 @@ 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
DeepLinking bool
DocExpansion string
DomID string
Plugins []template.JS
UIConfig map[template.JS]template.JS
BeforeScript template.JS
AfterScript template.JS
URL string
DeepLinking bool
DocExpansion string
DomID string
PersistAuthorization bool
Plugins []template.JS
UIConfig map[template.JS]template.JS
BeforeScript template.JS
AfterScript template.JS
}

// URL presents the url pointing to API definition (normally swagger.json or swagger.yaml).
Expand Down Expand Up @@ -55,6 +56,14 @@ func DomID(domID string) func(c *Config) {
}
}

// If set to true, it persists authorization data and it would not be lost on browser close/refresh
// Defaults to false
func PersistAuthorization(persistAuthorization bool) func(c *Config) {
return func(c *Config) {
c.PersistAuthorization = persistAuthorization
}
}

// Plugins specifies additional plugins to load into Swagger UI.
func Plugins(plugins []string) func(c *Config) {
return func(c *Config) {
Expand Down Expand Up @@ -97,10 +106,11 @@ func Handler(configFns ...func(*Config)) http.HandlerFunc {
var once sync.Once

config := &Config{
URL: "doc.json",
DeepLinking: true,
DocExpansion: "list",
DomID: "#swagger-ui",
URL: "doc.json",
DeepLinking: true,
DocExpansion: "list",
DomID: "#swagger-ui",
PersistAuthorization: false,
}
for _, configFn := range configFns {
configFn(config)
Expand Down Expand Up @@ -233,6 +243,7 @@ window.onload = function() {
deepLinking: {{.DeepLinking}},
docExpansion: "{{.DocExpansion}}",
dom_id: "{{.DomID}}",
persistAuthorization: {{.PersistAuthorization}},
validatorUrl: null,
presets: [
SwaggerUIBundle.presets.apis,
Expand Down
8 changes: 8 additions & 0 deletions swagger_test.go
Expand Up @@ -115,6 +115,14 @@ func TestDomID(t *testing.T) {
assert.Equal(t, expected, cfg.DomID)
}

func TestPersistAuthorization(t *testing.T) {
expected := true
cfg := Config{}
configFunc := PersistAuthorization(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.PersistAuthorization)
}

func TestConfigURL(t *testing.T) {

type fixture struct {
Expand Down

0 comments on commit b045c47

Please sign in to comment.