如何在Go Fiber Router函数中声明Swagger属性

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

How to declare swagger properties within go fiber Router function

问题

我正在使用fiber swagger包生成Swagger文档。我已经将我在其域内独占使用的路由进行了分组。

当我在函数上面进行声明时,它可以正常工作:

// GetDevices godoc
// @Summary Get all Devices
// @ID get-all-devices
// @Description Get all Devices
// @Accept  json
// @Produce  json
// @Tags Devices End Points
// @Success 200 {object} models.Device
// @Failure 400 {object} utils.HTTPError
// @Failure 404 {object} utils.HTTPError
// @Failure 500 {object} utils.HTTPError
// @Router /api/devices/ [get]
func DeviceRoute(route fiber.Router) {
    route.Get("", controllers.GetDevices)
    route.Post("/create", services.CheckMiddleware, controllers.CreateDevice)
}

然而,我希望为每个路由都有单独的Swagger声明,但是当我按照下面的方式做时,它不起作用:

func DeviceRoute(route fiber.Router) {
    // GetDevices godoc
    // @Summary Get all Devices
    // @ID get-all-devices
    // @Description Get all Devices
    // @ID get-item-by-int
    // @Accept  json
    // @Produce  json
    // @Tags Devices End Points
    // @Success 200 {object} models.Device
    // @Failure 400 {object} utils.HTTPError
    // @Failure 404 {object} utils.HTTPError
    // @Failure 500 {object} utils.HTTPError
    // @Router /api/devices/ [get]
    route.Get("", controllers.GetDevices)
    
    // Create Device godoc
    // @Summary Create a device
    // @ID create-device
    // @Description  Create a device
    // @Accept  json
    // @Produce  json
    // @Tags Devices
    // @param device body models.Device true  "Device details"
    // @Success 200 {object} Device
    // @Failure 400 {object} utils.HTTPError
    // @Failure 404 {object} utils.HTTPError
    // @Failure 500 {object} utils.HTTPError
    // @Router /api/devices/create [post]
    route.Post("/create", services.CheckMiddleware, controllers.CreateDevice)
}

当我按照上述方式操作时,我得到了**No operations defined in spec!**的错误。

对于每个从使用fiber.Router的函数导出和访问的路由,我应该如何处理这个问题?

注意:当我运行swag init时,当定义在DeviceRoute函数内部时,JSON文件的输出如下:

"info": {
    "description": "Teleops IOT server API",
    "title": "Teleops  API",
    "contact": {
        "name": "API Support",
        "email": "info@teleops.io"
    },
    "version": "2.0"
},
"host": "localhost:3000",
"basePath": "/",
"paths": {}
}
英文:

I generating swagger documentation using fiber swagger package. I have grouped routes that I use exclusively within their domains.

when i make the declarations above the function it works well

// GetDevices godoc
// @Summary Get all Devices
// @ID get-all-devices
// @Description Get all Devices
// @Accept  json
// @Produce  json
// @Tags Devices End Points
// @Success 200 {object} models.Device
// @Failure 400 {object} utils.HTTPError
// @Failure 404 {object} utils.HTTPError
// @Failure 500 {object} utils.HTTPError
// @Router /api/devices/ [get]
func DeviceRoute(route fiber.Router) {
	route.Get("", controllers.GetDevices)
     route.Post("/create", services.CheckMiddleware, controllers.CreateDevice)

}

however, I would want to have every swagger declaration for each route but this does not work when I do as show below

func DeviceRoute(route fiber.Router) {

	// GetDevices godoc
// @Summary Get all Devices
// @ID get-all-devices
// @Description Get all Devices
// @ID get-item-by-int
// @Accept  json
// @Produce  json
// @Tags Devices End Points
// @Success 200 {object} models.Device
// @Failure 400 {object} utils.HTTPError
// @Failure 404 {object} utils.HTTPError
// @Failure 500 {object} utils.HTTPError
// @Router /api/devices/ [get]
	route.Get("", controllers.GetDevices)
	
// Create Device godoc
// @Summary Create a device
// @ID create-device
// @Description  Create a device
// @Accept  json
// @Produce  json
// @Tags Devices
// @param device body models.Device true  "Device details"
// @Success 200 {object} Device
// @Failure 400 {object} utils.HTTPError
// @Failure 404 {object} utils.HTTPError
// @Failure 500 {object} utils.HTTPError
// @Router /api/devices/create [post]
     route.Post("/create", services.CheckMiddleware, controllers.CreateDevice)

}

when I do as above I get No operations defined in spec!

How should I do this for each route that has been expoted and accessed from a function that uses fiber.Router?

Note:when I run swag init, the output of the json file is as follows when the definitions are within the DeviceRoute function

"info": {
        "description": "Teleops IOT server API",
        "title": "Teleops  API",
        "contact": {
            "name": "API Support",
            "email": "info@teleops.io"
        },
        "version": "2.0"
    },
    "host": "localhost:3000",
    "basePath": "/",
    "paths": {}
}

答案1

得分: 2

文档生成器不会查看函数内部,只会查看函数外部。
你最好重新组织你的路由,以允许这样做,例如创建一个定义了方法的 Server 类,让你的路由调用这些方法来执行它们的功能。

英文:

The doc generator won't look inside functions, only outside them.
You're better re-structuring your routing to allow for that, e.g have a Server type that defines methods that your routes call to perform their function.

huangapple
  • 本文由 发表于 2021年10月9日 16:56:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/69505190.html
匿名

发表评论

匿名网友

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

确定