英文:
go-restful-openapi $refs must reference a valid location in the document
问题
我正在使用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" #此行返回错误
}
}
}
}
这是Swagger配置:
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"
}
}
}
}
英文:
I'm using go-restful
in combination with go-restful-openapi
to generate my swagger doc automatically. However, on testing the tool with the swagger-editor, I get the following error:
> $refs must reference a valid location in the document
struct
type Users struct {
# uuid imported from github.com/google/uuid@v1.2.0
RelatedUsers []uuid.UUID `json:"relatedIds" validate:"required"`
}
generated swagger snippet
"Users": {
"required": [
"relatedIds"
],
"properties": {
"relatedIds": {
"type": "array",
"$ref": "#/definitions/uuid.UUID" #this line returns an error
}
}
}
}
Here's the swagger config:
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",
},
}
},
}
NOTE: If I replace the lines above in the swagger editor as follows the error disappears, however, I couldn't figure out how to configure swagger to do so automatically
"Users": {
"required": [
"relatedIds"
],
"properties": {
"relatedUsers": {
type: array
items:
type: "string"
format: "uuid"
}
}
}
}
答案1
得分: 0
只是为了防止有人遇到这个问题:go-restful-openapi
无法解析导入的自定义类型。要解决这个问题,需要将该类型添加到定义中。
swo.Definitions["uuid.UUID"] = spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
Format: "uuid",
},
}
英文:
Just in case someone is running into this issue: go-restful-openapi
cannot resolve imported custom types. To solve this issue add the type to the definitions
swo.Definitions["uuid.UUID"] = spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
Format: "uuid",
},
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论