Home > Software design >  How to declare swagger properties within go fiber Router function
How to declare swagger properties within go fiber Router function

Time:10-09

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": "[email protected]"
        },
        "version": "2.0"
    },
    "host": "localhost:3000",
    "basePath": "/",
    "paths": {}
}

CodePudding user response:

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.

  • Related