Closed tomy128 closed 1 year ago
after my tests, if I define handler in main.go
, it will be detected properly. like this:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"net/http"
"swag/src/controller"
)
// Helloworld PingExample godoc
// @Summary ping example
// @Schemes
// @Description do ping
// @Tags example
// @Accept json
// @Produce json
// @Success 200 {string} Helloworld
// @Router /example/helloworld [get]
func Helloworld(g *gin.Context) {
g.JSON(http.StatusOK, "helloworld")
}
// @title MetaQ
// @version 1.0
// @description xxxxxxx
// @contact.name hahahah
// @contact.email xxxxxx@foxmail.com
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost
// @schemes http https
// @accept json
// @query.collection.format multi
func main() {
engine := gin.New()
engine.Use(gin.Recovery())
engine.GET("/helloworld", Helloworld)
engine.POST("/login/email", user_center.NewUserController().LoginOrRegisterWithEmail)
// swagger stuff
engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
controller.LoadRouters(engine)
if err := engine.Run(":9099"); err != nil {
fmt.Println("ListenAndServe: ", err.Error())
}
}
I had the same problem, but I solved it.
Maybe you could use swag init -d src/controller -g ../cmd/main.go
I had the same problem, but I solved it. Maybe you could use
swag init -d src/controller -g ../cmd/main.go
It's do solve it, this should be closed. thks.
I had another problem, maybe you could help help me.
In the LoginOrRegisterWithEmail godoc
// @Success 200 {object} dto.UserDTO
this will generate a model schema UserDTO
,
but what I need (function return) is something different like this: in the code above:
ctl.SuccessResponse(map[string]interface{}{})
the ctl.SuccessResponse
will convert the data input to a new format like:
gin.H{
"business_code": 0,
"message": nil,
"data": data, // this is the `SuccessResponse` input data
}
how to make swag generate model schema like this?
I had the same problem, but I solved it. Maybe you could use
swag init -d src/controller -g ../cmd/main.go
It's do solve it, this should be closed. thks.
I had another problem, maybe you could help help me.
In the
LoginOrRegisterWithEmail godoc
// @Success 200 {object} dto.UserDTO
this will generate a model schema
UserDTO
,but what I need (function return) is something different like this: in the code above:
ctl.SuccessResponse(map[string]interface{}{})
the
ctl.SuccessResponse
will convert the data input to a new format like:gin.H{ "business_code": 0, "message": nil, "data": data, // this is the `SuccessResponse` input data }
how to make swag generate model schema like this?
Okay, I know how to implement this finally. :-)
use Model composition in response
two things should be aware of:
struct
worked, which means function return struct is not availablecannot find type definition: xxx
, you can do init import is top level package where swag init -d
specified. like _ "src/pkg/to/some/struct"
engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
swaggerfiles
no longer has Handler
in 2.0.0
is there a porting guide?
I will put a complete code here for you to reproduce it. thks :-) This is the directory tree:
cmd/main.go
src/controller/router.go
src/controller/api/base_controller.go
src/controller/api/user_center/user.go
When I run
swag init -d src/cmd -g main.go
, it does generate docs directory inswag/docs
,but the
paths
field inswagger.json
orswagger.yaml
is empty.why it doesn't detect the handler
LoginOrRegisterWithEmail
, which defined insrc/controller/api/user_center/user.go
?