我使用go-restful
和go-restful-openapi
来自动生成我的swagger文档。然而,在用swagger编辑器测试该工具时,我得到了以下错误:
$refs必须引用文档中的有效位置
结构
type Users struct {
# uuid imported from github.com/google/uuid@v1.2.0
RelatedUsers []uuid.UUID `json:"relatedIds" validate:"required"`
}
生成的swagger片段
"Users": {
"required": [
"relatedIds"
],
"properties": {
"relatedIds": {
"type": "array",
"$ref": "#/definitions/uuid.UUID" #this line returns an error
}
}
}
}
以下是招摇配置:
swaggerConfig := restfulspec.Config{
WebServices: restfulContainer.RegisteredWebServices(),
APIPath: "/swagger.json",
PostBuildSwaggerObjectHandler: func(swo *spec.Swagger) {
swo.Info = &spec.Info{
InfoProps: spec.InfoProps{
Title: "User Service",
Description: "An example service for stackoverflow",
Version: "1.0.0",
},
}
},
}
注意:如果我在swagger编辑器中按如下方式替换上面的行,错误就会消失,但是,我不知道如何配置swagger来自动完成
"Users": {
"required": [
"relatedIds"
],
"properties": {
"relatedUsers": {
type: array
items:
type: "string"
format: "uuid"
}
}
}
}
以防万一有人遇到此问题:go-restful-openapi
无法解析导入的自定义类型。为了解决这个问题,将类型添加到定义
swo.Definitions["uuid.UUID"] = spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
Format: "uuid",
},
}