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

huangapple go评论82阅读模式
英文:

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

问题

我正在使用go-restfulgo-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",
	},
}

huangapple
  • 本文由 发表于 2021年6月21日 15:53:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/68064268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定