swaggo / gin-swagger

gin middleware to automatically generate RESTful API documentation with Swagger 2.0.
MIT License
3.76k stars 270 forks source link

Support for `persistAuthorization` swagger ui #192

Closed kuklyy closed 2 years ago

kuklyy commented 2 years ago

For now apiKey (or any other authorization method) is lost after browser reload, so I have to provide it again.

Swagger UI provides an out of the box solution to this problem (documentation reference). I will provide code samples for gin-swagger. Similar changes should be introduced for other frameworks. There is a quick solution for the project. ginSwagger.Config and ginSwagger.ToSwaggerConfig should accept another parameter


// Config stores ginSwagger 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
    DefaultModelsExpandDepth int
    InstanceName             string
    Title                    string
    PersistAuthorization     bool
}

So lib can pass this value to SwaggerUIBundle


window.onload = function() {
  // Build a system
  const ui = SwaggerUIBundle({
    url: "{{.URL}}",
    persistAuthorization: {{.PersistAuthorization}},
    dom_id: '#swagger-ui',
    validatorUrl: null,
    oauth2RedirectUrl: {{.Oauth2RedirectURL}},
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout",
    docExpansion: "{{.DocExpansion}}",
    deepLinking: {{.DeepLinking}},
    defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}
  })

  window.ui = ui
}

I can provide pull request if you found this feature should be available in your library.

ubogdan commented 2 years ago

@kuklyy This is an open-source project. Feel free to contribute, and I will assist you with the code review.

ubogdan commented 2 years ago

@kuklyy Thanks for your contribution!