go-restful-openapi $refs 必须引用文档中的有效位置

go-restful-openapi $refs must reference a valid location in the document

我将 go-restfulgo-restful-openapi 结合使用来自动生成我的 swagger 文档。但是,在使用 swagger-editor 测试该工具时,出现以下错误:

$refs must reference a valid location in the document

结构

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 
     }
    }
   }
  }

这是 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 Whosebug",
                    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",
    },
}