英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论