dousi0144 2016-02-16 12:15
浏览 460
已采纳

go-swagger使用path参数生成路线规格

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:

// 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)

Struct wrapping bar parameter:

// swagger:parameters barParam
type BarParam struct {

    // aaaa
    // in: path
    bar string
}

When I run:

swagger generate spec -o ./swagger.json

The generated JSON currently looks like this:

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

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

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

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?

  • 写回答

2条回答 默认 最新

  • doumou1864 2016-02-16 15:26
    关注

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

    // swagger:parameters updateBar
    type BarParam struct {
    
        // aaaa
        // in: path
        bar string
    }
    

    And remove 'Parameters' from your endpoint swagger doc

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!