【问题标题】:How to setup go-swagger to generate spec from annotation如何设置 go-swagger 从注释生成规范
【发布时间】:2016-10-17 05:10:19
【问题描述】:

我正在按照https://goswagger.io/generate/spec.html 此处生成 swagger 规范的说明进行操作。

我有一个需要 API 用户界面的现有项目。我想用 go swagger 但我完全糊涂了https://github.com/go-swagger/go-swagger/tree/master/examples/todo-list 我想设置它,所以我在代码中添加注释,然后运行命令 swagger generate spec 它会生成规范 但是,每当我运行它时,它都会打印出{"swagger":"2.0","paths":{},"definitions":{}}

这是我运行它的命令

...com/projectFolder]$ swagger generate spec                                                
{"swagger":"2.0","paths":{},"definitions":{}}

我的项目结构如下

project/ 
  main.go
  api/
    router.go

在 main.go 我有这个注释

//go:generate swagger generate spec
package main

在我的一个处理程序上方的路由器中,我有这个注释

// swagger:route GET /profile
//
// Gets profile of user
//
//     Produces:
//     - application/json
//     - application/x-protobuf
//
//     Schemes: http, https, ws, wss
//
//     Security:
//       api_key:
//       oauth: read, write
//
//     Responses:
//       default: genericError
//       200: someResponse
//       422: validationError
r.GET("/profile", profileHandler

一段时间以来,我一直在尝试设置 api 生成器。任何帮助深表感谢。如果您有设置它的经验,请告诉我您是如何做到的

【问题讨论】:

    标签: go swagger go-swagger


    【解决方案1】:

    这可能是一个迟到的答案,但无论如何。我遇到了同样的问题,并且也在努力使用 go-swagger 文档;这就是我最终想到的:

    swagger generate 可以接受输入文件参数(-b main.go,应用程序的入口点,它会自动从中找到所有其他注释)和输出文件 (-o swagger.json)

    所以总的来说你的命令应该是这样的:

    swagger generate spec -b ./main.go -o swagger.json

    在我的主要 go 文件中,在它的顶部,我的注释与您的注释有些不同。它定义了基本的项目属性,如标题:

    // Project title (the dot in the end matters).
    //
    // Project description.
    //
    //     Schemes: http
    //     Version: 0.1
    //
    //     Consumes:
    //     - application/json
    //
    //     Produces:
    //     - application/json
    //
    //
    // swagger:meta
    

    【讨论】:

    • -b 不是有效标志
    猜你喜欢
    • 2016-09-30
    • 1970-01-01
    • 2020-12-19
    • 2021-05-26
    • 2018-11-20
    • 2013-08-01
    • 2016-06-29
    • 2017-07-08
    • 2021-05-16
    相关资源
    最近更新 更多