gofiber / swagger

🧬 fiber middleware to automatically generate RESTful API documentation with Swagger

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

swagger.json is almost empty. No operations defined in spec!

shinebayar-g opened this issue · comments

Hi, I'm new to swagger in general. I followed the README. Here is what I did so far.

  1. Added few comments to main.go
// @title Fiber Example API
// @version 1.0
// @description This is a sample swagger for Fiber
// @contact.name API Support
// @contact.email youremail@provider.com
// @host localhost:3000
// @BasePath /
func main() {
...
}
  1. Added default handler to Fiber instance
app.Get("/swagger/*", swagger.HandlerDefault) // default
  1. Imported docs package in the same file as handler.
_ "main/docs"
  1. Ran swag init in the root dir of the workspace.

This produces following docs/swagger.json

{
    "swagger": "2.0",
    "info": {
        "description": "This is a sample swagger for Fiber",
        "title": "Fiber Example API",
        "contact": {
            "name": "API Support",
            "email": "youremail@provider.com"
        },
        "version": "1.0"
    },
    "host": "localhost:3000",
    "basePath": "/",
    "paths": {}
}

http://127.0.0.1:3000/swagger/index.html returns No operations defined in spec!

Am I missing anything?

Thanks.

commented

End up fixing this? Trying to sort this out, having two gets in the main each having their own swagger blocks it all gets overwritten to the last one and on top of that get the No operations defined in spec! guessing the docs assumes prior knowledge...

nah i gave up.

guessing the docs assumes prior knowledge...

Yeah I agree.

I'm facing the same issue. Someone fixed it ?

Have a good day

Ok I found the solution, you must specify your routes like this :

// User godoc
// @Summary      Get users
// @Tags         user
// @Accept       json
// @Produce      json
// @Success      200  {object}  answers.UsersAnswer
// @Failure      400  {object}  errors.ApiError
// @Failure      500  {object}  errors.ApiError
// @Router /v1/users [GET]
func (controller *UserController) GetUsers(c *fiber.Ctx) error {
	return utils.HandleMessageLessRequest[answers.UsersAnswer](c, []string{"admin"}, controller.getUsersUseCase)
}

// User godoc
// @Summary      Get user
// @Tags         user
// @Accept       json
// @Produce      json
// @Success      200  {object}  answers.UserDetailAnswer
// @Failure      400  {object}  errors.ApiError
// @Failure      500  {object}  errors.ApiError
// @Router /v1/users/:id [GET]
func (controller *UserController) GetUser(c *fiber.Ctx) error {
	return utils.HandleRequest[messages.GetUserMessage, answers.UserDetailAnswer](c, []string{"admin"}, controller.getUserUseCase)
}

// User godoc
// @Summary      Get user
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        message body messages.UpdateUserMessage true "UpdateUserMessage message"
// @Success      200  {object}  answers.UserDetailAnswer
// @Failure      400  {object}  errors.ApiError
// @Failure      500  {object}  errors.ApiError
// @Router /v1/users/:id [PATCH]
func (controller *UserController) UpdateUser(c *fiber.Ctx) error {
	return utils.HandleRequest[messages.UpdateUserMessage, answers.UserDetailAnswer](c, []string{"admin"}, controller.updateUserUseCase)
}

// User godoc
// @Summary      Get user
// @Tags         user
// @Accept       json
// @Produce      json
// @Param        message body messages.CreateUserMessage true "CreateUserMessage message"
// @Success      200  {object}  answers.UserDetailAnswer
// @Failure      400  {object}  errors.ApiError
// @Failure      500  {object}  errors.ApiError
// @Router /v1/users [POST]
func (controller *UserController) CreateUser(c *fiber.Ctx) error {
	return utils.HandleRequest[messages.CreateUserMessage, answers.UserDetailAnswer](c, []string{"admin"}, controller.createUserUseCase)
}

And it works, I think it can be good to specify this in the README @ReneWerner87

@FournyP I thought it was swagger's job to autogenerate those things? :O

Nop, the command swag init is based on those comments, we must specify them