生成具有路径参数的路由的go-swagger规范。

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

go-swagger generate spec for route with path parameter

问题

我正在尝试使用go-swagger生成符合Swagger2.0规范的JSON API文档。我遇到了一个问题,无法为具有路径参数的路由生成JSON文档,该路由的路径如下所示:PUT /foo/{bar}

目前我的godoc如下所示:

// Update bar in foo
// swagger:route PUT /foo/{bar} updateBar
// Parameters:
// bar: barParam
// Responses:
// 500: myErrorResponse
func (h *handler) update(req *http.Request, params martini.Params) (int, string)

包装bar参数的结构体如下所示:

// swagger:parameters barParam
type BarParam struct {

  1. // aaaa
  2. // in: path
  3. bar string

}

当我运行以下命令时:

swagger generate spec -o ./swagger.json

生成的JSON当前如下所示:

"/foo/{bar}": {
"put": {
"description": "bar: barParam",
"operationId": "updateBar",
"responses": {
"500": {
"$ref": "#/responses/myErrorResponse"
}
},
"summary": "Parameters:"
}
}

但我想生成以下符合Swagger2.0规范的JSON:

"/v2/foo/{bar}": {
"put": {
"operationId": "updateBar",
"responses": {
"500": {
"$ref": "#/responses/myErrorResponse"
}
},
"parameters": [
{
"in": "path",
"name": "bar",
"description": "aaaa",
"required": true,
"type": "string"
}
]
}
}

我该如何修改go-swagger的文档注释以实现这一目标?是否有描述go-swagger确切注释格式的文档?

英文:

I am trying to generate json API documentation compliant with Swagger2.0 specification using go-swagger.

I have a problem generating JSON doc for the route with path param which looks like this:
> PUT /foo/{bar}

Currently my godoc looks like this:

  1. // Update bar in foo
  2. // swagger:route PUT /foo/{bar} updateBar
  3. // Parameters:
  4. // bar: barParam
  5. // Responses:
  6. // 500: myErrorResponse
  7. func (h *handler) update(req *http.Request, params martini.Params) (int, string)

Struct wrapping bar parameter:

  1. // swagger:parameters barParam
  2. type BarParam struct {
  3. // aaaa
  4. // in: path
  5. bar string
  6. }

When I run:

  1. swagger generate spec -o ./swagger.json

The generated JSON currently looks like this:

  1. "/foo/{bar}": {
  2. "put": {
  3. "description": "bar: barParam",
  4. "operationId": "updateBar",
  5. "responses": {
  6. "500": {
  7. "$ref": "#/responses/myErrorResponse"
  8. }
  9. },
  10. "summary": "Parameters:"
  11. }
  12. }

But I want to generate the following JSON (compliant with Swagger2.0):

  1. "/v2/foo/{bar}": {
  2. "put": {
  3. "operationId": "updateBar",
  4. "responses": {
  5. "500": {
  6. "$ref": "#/responses/myErrorResponse"
  7. }
  8. },
  9. "parameters": [
  10. {
  11. "in": "path",
  12. "name": "bar",
  13. "description": "aaaa",
  14. "required": true,
  15. "type": "string"
  16. }
  17. ]
  18. }
  19. }

How can I modify the doc comments for go-swagger to achieve that? Is there any documentation that describes the exact comments format for go-swagger?

答案1

得分: 0

尝试将您的端点操作ID('updateBar')作为参数操作ID添加进去。类似这样:

  1. // swagger:parameters updateBar
  2. type BarParam struct {
  3. // aaaa
  4. // in: path
  5. bar string
  6. }

并且从您的端点Swagger文档中删除'Parameters'。

英文:

Try to add your endpoint operatin id ('updateBar') as a parameter operation id. Something like:

  1. // swagger:parameters updateBar
  2. type BarParam struct {
  3. // aaaa
  4. // in: path
  5. bar string
  6. }

And remove 'Parameters' from your endpoint swagger doc

答案2

得分: 0

如果你想将 Bar 转换为小写,你需要进行以下设置:

  1. // swagger:parameters updateBar
  2. type BarParam struct {
  3. Bar string `json:"bar"`
  4. }
英文:

If you want to Switch Bar to lowercase, you have to set :

  1. // swagger:parameters updateBar
  2. type BarParam struct {
  3. Bar string `json:"bar"`
  4. }

huangapple
  • 本文由 发表于 2016年2月16日 20:15:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/35432369.html
匿名

发表评论

匿名网友

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

确定