feat: add support for swagger-ui persist-authorization (#195)

This commit is contained in:
Jakub Mikłasz
2022-02-06 12:05:35 +01:00
committed by GitHub
parent 0bb2c39427
commit cb6be4cbcf
3 changed files with 28 additions and 2 deletions
+2 -1
View File
@@ -165,4 +165,5 @@ func main() {
| DocExpantion | string | "list" | Controls the default expansion setting for the operations and tags. It can be 'list' (expands only the tags), 'full' (expands the tags and operations) or 'none' (expands nothing). |
| DeepLinking | bool | true | If set to true, enables deep linking for tags and operations. See the Deep Linking documentation for more information. |
| DefaultModelsExpandDepth | int | 1 | Default expansion depth for models (set to -1 completely hide the models). |
| InstanceName | string | "swagger" | The instance name of the swagger document. If multiple different swagger instances should be deployed on one gin router, ensure that each instance has a unique name (use the _--instanceName_ parameter to generate swagger documents with _swag init_). |
| InstanceName | string | "swagger" | The instance name of the swagger document. If multiple different swagger instances should be deployed on one gin router, ensure that each instance has a unique name (use the _--instanceName_ parameter to generate swagger documents with _swag init_).
| PersistAuthotization | bool | false | If set to true, it persists authorization data and it would not be lost on browser close/refresh. |
+13 -1
View File
@@ -21,6 +21,7 @@ type swaggerConfig struct {
DefaultModelsExpandDepth int
Oauth2RedirectURL template.JS
Title string
PersistAuthorization bool
}
// Config stores ginSwagger configuration variables.
@@ -32,6 +33,7 @@ type Config struct {
DefaultModelsExpandDepth int
InstanceName string
Title string
PersistAuthorization bool
}
// Convert the config to a swagger one in order to fill unexposed template values.
@@ -46,7 +48,8 @@ func (c Config) ToSwaggerConfig() swaggerConfig {
"{window.location.pathname.split('/').slice(0, window.location.pathname.split('/').length - 1).join('/')}" +
"/oauth2-redirect.html`",
),
Title: c.Title,
Title: c.Title,
PersistAuthorization: c.PersistAuthorization,
}
}
@@ -87,6 +90,14 @@ func InstanceName(name 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
}
}
// WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.
func WrapHandler(h *webdav.Handler, confs ...func(c *Config)) gin.HandlerFunc {
defaultConfig := &Config{
@@ -275,6 +286,7 @@ window.onload = function() {
dom_id: '#swagger-ui',
validatorUrl: null,
oauth2RedirectUrl: {{.Oauth2RedirectURL}},
persistAuthorization: {{.PersistAuthorization}},
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
+13
View File
@@ -226,3 +226,16 @@ func TestInstanceName(t *testing.T) {
configFunc(&cfg)
assert.Equal(t, expected, cfg.InstanceName)
}
func TestPersistAuthorization(t *testing.T) {
var cfg Config
assert.Equal(t, false, cfg.PersistAuthorization)
configFunc := PersistAuthorization(true)
configFunc(&cfg)
assert.Equal(t, true, cfg.PersistAuthorization)
configFunc = PersistAuthorization(false)
configFunc(&cfg)
assert.Equal(t, false, cfg.PersistAuthorization)
}