fix: oauth2-redirect outside of the base swagger path (#177)

This commit is contained in:
alvaro garcia fernandez
2021-11-04 21:25:29 +01:00
committed by GitHub
parent 7da8e84293
commit 498deb2d90
5 changed files with 58 additions and 19 deletions
+16 -2
View File
@@ -8,12 +8,13 @@ gin middleware to automatically generate RESTFUL API documentation with Swagger
[![GoDoc](https://godoc.org/github.com/swaggo/gin-swagger?status.svg)](https://godoc.org/github.com/swaggo/gin-swagger) [![GoDoc](https://godoc.org/github.com/swaggo/gin-swagger?status.svg)](https://godoc.org/github.com/swaggo/gin-swagger)
[![Release](https://img.shields.io/github/release/swaggo/gin-swagger.svg?style=flat-square)](https://github.com/swaggo/gin-swagger/releases) [![Release](https://img.shields.io/github/release/swaggo/gin-swagger.svg?style=flat-square)](https://github.com/swaggo/gin-swagger/releases)
## Usage ## Usage
### Start using it ### Start using it
1. Add comments to your API source code, [See Declarative Comments Format](https://swaggo.github.io/swaggo.io/declarative_comments_format/). 1. Add comments to your API source code, [See Declarative Comments Format](https://swaggo.github.io/swaggo.io/declarative_comments_format/).
2. Download [Swag](https://github.com/swaggo/swag) for Go by using: 2. Download [Swag](https://github.com/swaggo/swag) for Go by using:
```sh ```sh
go get -u github.com/swaggo/swag/cmd/swag go get -u github.com/swaggo/swag/cmd/swag
``` ```
@@ -21,14 +22,18 @@ go get -u github.com/swaggo/swag/cmd/swag
3. Run the [Swag](https://github.com/swaggo/swag) at your Go project root path(for instance `~/root/go-peoject-name`), 3. Run the [Swag](https://github.com/swaggo/swag) at your Go project root path(for instance `~/root/go-peoject-name`),
[Swag](https://github.com/swaggo/swag) will parse comments and generate required files(`docs` folder and `docs/doc.go`) [Swag](https://github.com/swaggo/swag) will parse comments and generate required files(`docs` folder and `docs/doc.go`)
at `~/root/go-peoject-name/docs`. at `~/root/go-peoject-name/docs`.
```sh ```sh
swag init swag init
``` ```
4. Download [gin-swagger](https://github.com/swaggo/gin-swagger) by using: 4. Download [gin-swagger](https://github.com/swaggo/gin-swagger) by using:
```sh ```sh
go get -u github.com/swaggo/gin-swagger go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files go get -u github.com/swaggo/files
``` ```
Import following in your code: Import following in your code:
```go ```go
@@ -38,7 +43,9 @@ import "github.com/swaggo/files" // swagger embed files
``` ```
### Canonical example: ### Canonical example:
Now assume you have implemented a simple api as following: Now assume you have implemented a simple api as following:
```go ```go
// A get function which returns a hello world string by json // A get function which returns a hello world string by json
func Helloworld(g *gin.Context) { func Helloworld(g *gin.Context) {
@@ -46,9 +53,11 @@ func Helloworld(g *gin.Context) {
} }
``` ```
So how to use gin-swagger on api above? Just follow the following guide. So how to use gin-swagger on api above? Just follow the following guide.
1. Add Comments for apis and main function with gin-swagger rules like following: 1. Add Comments for apis and main function with gin-swagger rules like following:
```go ```go
// @BasePath /api/v1 // @BasePath /api/v1
@@ -69,14 +78,17 @@ func Helloworld(g *gin.Context) {
2. Use `swag init` command to generate a docs, docs generated will be stored at 2. Use `swag init` command to generate a docs, docs generated will be stored at
3. import the docs like this: 3. import the docs like this:
I assume your project named `github.com/go-project-name/docs`. I assume your project named `github.com/go-project-name/docs`.
```go ```go
import ( import (
docs "github.com/go-project-name/docs" docs "github.com/go-project-name/docs"
) )
``` ```
4. build your application and after that, go to http://localhost:8080/swagger/index.html ,you to see your Swagger UI. 4. build your application and after that, go to http://localhost:8080/swagger/index.html ,you to see your Swagger UI.
5. The full code and folder relatives here: 5. The full code and folder relatives here:
```go ```go
package main package main
@@ -117,7 +129,9 @@ func main() {
} }
``` ```
Demo project tree, `swag init` is run at relative `.` Demo project tree, `swag init` is run at relative `.`
``` ```
. .
├── docs ├── docs
@@ -146,7 +160,7 @@ func main() {
``` ```
| Option | Type | Default | Description | | Option | Type | Default | Description |
|--------------------------|--------|------------|---------------------------------------------------------------------------| | ------------------------ | ------ | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| URL | string | "doc.json" | URL pointing to API definition | | URL | string | "doc.json" | URL pointing to API definition |
| 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). | | 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. | | DeepLinking | bool | true | If set to true, enables deep linking for tags and operations. See the Deep Linking documentation for more information. |
+1 -1
View File
@@ -2,7 +2,7 @@ package main
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/swaggo/files" swaggerFiles "github.com/swaggo/files"
"github.com/swaggo/gin-swagger" "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/example/basic/api" "github.com/swaggo/gin-swagger/example/basic/api"
+1 -1
View File
@@ -3,7 +3,7 @@ package main
import ( import (
"github.com/gin-contrib/gzip" "github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/swaggo/files" swaggerFiles "github.com/swaggo/files"
"github.com/swaggo/gin-swagger" "github.com/swaggo/gin-swagger"
_ "github.com/swaggo/gin-swagger/example/basic/docs" _ "github.com/swaggo/gin-swagger/example/basic/docs"
+28 -4
View File
@@ -14,6 +14,14 @@ import (
"github.com/swaggo/swag" "github.com/swaggo/swag"
) )
type swaggerConfig struct {
URL string
DeepLinking bool
DocExpansion string
DefaultModelsExpandDepth int
Oauth2RedirectURL template.JS
}
// Config stores ginSwagger configuration variables. // Config stores ginSwagger configuration variables.
type Config struct { type Config struct {
//The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`. //The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
@@ -23,6 +31,21 @@ type Config struct {
DefaultModelsExpandDepth int DefaultModelsExpandDepth int
} }
// Convert the config to a swagger one in order to fill unexposed template values.
func (c Config) ToSwaggerConfig() swaggerConfig {
return swaggerConfig{
URL: c.URL,
DeepLinking: c.DeepLinking,
DocExpansion: c.DocExpansion,
DefaultModelsExpandDepth: c.DefaultModelsExpandDepth,
Oauth2RedirectURL: template.JS(
"`${window.location.protocol}//${window.location.host}$" +
"{window.location.pathname.split('/').slice(0, window.location.pathname.split('/').length - 1).join('/')}" +
"/oauth2-redirect.html`",
),
}
}
// URL presents the url pointing to API definition (normally swagger.json or swagger.yaml). // URL presents the url pointing to API definition (normally swagger.json or swagger.yaml).
func URL(url string) func(c *Config) { func URL(url string) func(c *Config) {
return func(c *Config) { return func(c *Config) {
@@ -82,7 +105,7 @@ func CustomWrapHandler(config *Config, handler *webdav.Handler) gin.HandlerFunc
matches := rexp.FindStringSubmatch(c.Request.RequestURI) matches := rexp.FindStringSubmatch(c.Request.RequestURI)
if len(matches) != 3 { if len(matches) != 3 {
c.Status(404) c.Status(http.StatusNotFound)
_, _ = c.Writer.Write([]byte("404 page not found")) _, _ = c.Writer.Write([]byte("404 page not found"))
return return
} }
@@ -107,7 +130,7 @@ func CustomWrapHandler(config *Config, handler *webdav.Handler) gin.HandlerFunc
switch path { switch path {
case "index.html": case "index.html":
_ = index.Execute(c.Writer, config) _ = index.Execute(c.Writer, config.ToSwaggerConfig())
case "doc.json": case "doc.json":
doc, err := swag.ReadDoc() doc, err := swag.ReadDoc()
if err != nil { if err != nil {
@@ -130,7 +153,7 @@ func DisablingWrapHandler(h *webdav.Handler, envName string) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
// Simulate behavior when route unspecified and // Simulate behavior when route unspecified and
// return 404 HTTP code // return 404 HTTP code
c.String(404, "") c.String(http.StatusNotFound, "")
} }
} }
@@ -145,7 +168,7 @@ func DisablingCustomWrapHandler(config *Config, h *webdav.Handler, envName strin
return func(c *gin.Context) { return func(c *gin.Context) {
// Simulate behavior when route unspecified and // Simulate behavior when route unspecified and
// return 404 HTTP code // return 404 HTTP code
c.String(404, "") c.String(http.StatusNotFound, "")
} }
} }
@@ -230,6 +253,7 @@ window.onload = function() {
url: "{{.URL}}", url: "{{.URL}}",
dom_id: '#swagger-ui', dom_id: '#swagger-ui',
validatorUrl: null, validatorUrl: null,
oauth2RedirectUrl: {{.Oauth2RedirectURL}},
presets: [ presets: [
SwaggerUIBundle.presets.apis, SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset SwaggerUIStandalonePreset
+5 -4
View File
@@ -62,6 +62,7 @@ func TestWrapCustomHandler(t *testing.T) {
w6 := performRequest("GET", "/notfound", router) w6 := performRequest("GET", "/notfound", router)
assert.Equal(t, 404, w6.Code) assert.Equal(t, 404, w6.Code)
} }
func TestDisablingWrapHandler(t *testing.T) { func TestDisablingWrapHandler(t *testing.T) {
@@ -94,11 +95,11 @@ func TestDisablingWrapHandler(t *testing.T) {
w22 := performRequest("GET", "/disabling/doc.json", router) w22 := performRequest("GET", "/disabling/doc.json", router)
assert.Equal(t, 404, w22.Code) assert.Equal(t, 404, w22.Code)
w33 := performRequest("GET", "/disabling/favicon-16x16.png", router) w44 := performRequest("GET", "/disabling/oauth2-redirect.html", router)
assert.Equal(t, 404, w33.Code)
w44 := performRequest("GET", "/disabling/notfound", router)
assert.Equal(t, 404, w44.Code) assert.Equal(t, 404, w44.Code)
w55 := performRequest("GET", "/disabling/notfound", router)
assert.Equal(t, 404, w55.Code)
} }
func TestDisablingCustomWrapHandler(t *testing.T) { func TestDisablingCustomWrapHandler(t *testing.T) {