swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.5k stars 1.19k forks source link

"Failed to load API definition" / "not yet registered swag" error when rendering docs page #830

Closed dub-squared closed 3 years ago

dub-squared commented 3 years ago

Describe the bug I've followed the directions to setup the docs on a Gin based API, but when the I visit the docs page, it shows a Failed to load API definition error, with a sub error of Fetch error Internal Server Error doc.json. The server logs show an not yet registered swag error.

To Reproduce Steps to reproduce the behavior:

  1. Clone the demo repo https://github.com/dub-squared/go-swag-demo-api
  2. Run the server: make run
  3. Open a browser and visit http://localhost:3001/swagger/index.html
  4. See error

Expected behavior The Swagger docs should render successfully

Screenshots Screen Shot 2020-11-11 at 11 21 43 AM

Stacktrace

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /v1/ping                  --> go-swag-demo-api/pkg/controllers.PingController.Status-fm (3 handlers)
[GIN-debug] GET    /swagger/*any             --> github.com/swaggo/gin-swagger.CustomWrapHandler.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :3001
[GIN] 2020/11/11 - 11:09:45 | 200 |     243.212µs |             ::1 | GET      "/swagger/index.html"
[GIN] 2020/11/11 - 11:09:45 | 200 |     253.381µs |             ::1 | GET      "/swagger/swagger-ui.css"
[GIN] 2020/11/11 - 11:09:45 | 200 |      494.21µs |             ::1 | GET      "/swagger/swagger-ui-bundle.js"
[GIN] 2020/11/11 - 11:09:45 | 200 |     191.263µs |             ::1 | GET      "/swagger/swagger-ui-standalone-preset.js"
[GIN] 2020/11/11 - 11:09:45 | 200 |    3.427957ms |             ::1 | GET      "/swagger/favicon-16x16.png"

2020/11/11 11:09:45 [Recovery] 2020/11/11 - 11:09:45 panic recovered:
GET /swagger/doc.json HTTP/1.1
Host: localhost:3001
Accept: application/json,*/*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
Dnt: 1
Referer: http://localhost:3001/swagger/index.html
User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0

not yet registered swag
/Users/wade/go/pkg/mod/github.com/swaggo/gin-swagger@v1.3.0/swagger.go:94 (0x15ea0f2)
        CustomWrapHandler.func1: panic(err)
/Users/wade/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x150167a)
        (*Context).Next: c.handlers[c.index](c)
/Users/wade/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/recovery.go:83 (0x1515344)
        RecoveryWithWriter.func1: c.Next()
/Users/wade/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x150167a)
        (*Context).Next: c.handlers[c.index](c)
/Users/wade/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/logger.go:241 (0x1514444)
        LoggerWithConfig.func1: c.Next()
/Users/wade/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x150167a)
        (*Context).Next: c.handlers[c.index](c)
/Users/wade/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/gin.go:409 (0x150b699)
        (*Engine).handleHTTPRequest: c.Next()
/Users/wade/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/gin.go:367 (0x150ad8c)
        (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/Cellar/go/1.15.2/libexec/src/net/http/server.go:2843 (0x12f9d82)
        serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/Cellar/go/1.15.2/libexec/src/net/http/server.go:1925 (0x12f548c)
        (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/Cellar/go/1.15.2/libexec/src/runtime/asm_amd64.s:1374 (0x106bf40)
        goexit: BYTE    $0x90   // NOP

[GIN] 2020/11/11 - 11:09:45 | 500 |     909.198µs |             ::1 | GET      "/swagger/doc.json"

Swag version 1.6.9

Go version 1.15.2

Desktop:

dub-squared commented 3 years ago

Looks like I missed having to import the generated docs in the main.go file. Once I added _ "go-swag-demo-api/docs" to the imports it worked

toluwase1 commented 11 months ago

Looks like I missed having to import the generated docs in the main.go file. Once I added _ "go-swag-demo-api/docs" to the imports it worked

thank you, this fixed my error

ianbunn commented 3 months ago

It took me about 3 hours to figure out that this would also solve my issue. My main.go is in "project_root_dir/cmd/main.go", so I added this to main file.

_ "project_root_dir/cmd/docs"