如何使用 swaggo 在另一个文件中使用类型定义?

ioo*_*ooi 6 include package go swagger godoc

我正在使用swaggo根据godoc语法生成 API 文档。

源文件夹和文件

|-post
|--controller.go
|--response.go
Run Code Online (Sandbox Code Playgroud)

对于这个定义:

controller.go

package post

...

// Index godoc
// @Summary Index Post
// @Success 200 {array} []*Response
// @Router /v1/posts [get]
func Index(ctx *gin.Context) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

Response是在同一包的另一个文件中定义的。

response.go

package post

// Response  is post response body
type Response struct {
    ID   int64  `json:"id"`
    Name string `json:"name"`

    CreatedAt string `json:"created_at"`
    UpdatedAt string `json:"updated_at"`
}
Run Code Online (Sandbox Code Playgroud)

但是当运行swag init生成 swagger 文档时,它说:

2021/01/29 09:39:56 Generate swagger docs....
2021/01/29 09:39:56 Generate general API Info, search dir:./
2021/01/29 09:39:56 ParseComment error in file application/post/controller.go :cannot find type definition:
Run Code Online (Sandbox Code Playgroud)

当我将Response结构移动到时controller.go,它就起作用了。

如何使用 godoc 或 swaggo 导入它?

Lai*_*Lee 13

我也遇到了这个问题,但是我用下面的命令解决了:

swag init --parseDependency --parseInternal
Run Code Online (Sandbox Code Playgroud)


小智 7

您需要附加--parseDependencyswag init命令中。