我有一个带有 swagger 文档生成功能的 go web 应用程序。最近在我的项目中添加了新端点,该端点在 POST 和 PUT 请求中使用以下结构:
Secret struct {
// Secret unique key name.
Name string `json:"name" example:"ACCESS_TOKEN"`
// type: string
// x-go-type: "string"
Value json.RawMessage `json:"value" swagger:"type:string"`
// Tags in which this secret is used.
Tags []string `json:"tags" example:"dev,prod,omitempty"`
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用命令构建 swagger 文档时:swag init -md ./documentation -o ./swagger
我收到以下错误:
如果我正确理解应该添加type definition explicitly
,但我不明白如何正确定义类型,所有注释组合都不起作用。这个问题与类型(值字段)有关,json.RawMessage
因为如果我用 interface{} 替换该类型,一切都会正常。
我找到了我的问题的解决方案,以前我必须省略使用--parseDependency
,因为 swagger 文档生成挂起,但如果我同时传递--parseDepth
1 我的文档会成功生成,所以完整的 cmd 是:
swag init --parseDependency --parseInternal --parseDepth 1 -md ./documentation -o ./swagger
Run Code Online (Sandbox Code Playgroud)
据我了解,发生这种情况是因为json.RawMessage
需要导入,但如果我不使用--parseDependency
json.RawMessage 类型,则找不到,但在我的问题案例中,单个 --parseDependency 挂起,因此无法使用它。解决方案是使用--parseDepth
参数