3

I want to use Swagger for my RESTFul API Documentation from Go and Gin.

I have this code in main.go: package main

import (
    "gowebservice/config"
    "gowebservice/controllers"

    "github.com/gin-gonic/gin"
    swaggerFiles "github.com/swaggo/files"
    ginSwagger "github.com/swaggo/gin-swagger"

    _ "github.com/swaggo/gin-swagger/example/basic/docs"
)

// @title Students API
// @version 1.0
// @description This is a basic API Students using Gin and Gorm.

// @host localhost:8080
// @BasePath /

func main() {
    r := gin.Default()

    config.ConnectDatabase()

    v1 := r.Group("/api/v1")
    {
        v1.GET("students/all", controllers.GetStudents)
    }

    r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

    r.Run()
}

and this is my endpoint that have GET method:

package controllers

import (
    "gowebservice/config"
    "gowebservice/models"
    "net/http"

    "github.com/gin-gonic/gin"
)

// GetStudents godoc
// @Summary Show a list of students
// @Accept  json
// @Produce  json
// @Router /students [get]
func GetStudents(c *gin.Context) {
    var students []models.Student

    if err := config.DB.Find(&students).Error; err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
        return
    }
    c.JSON(http.StatusOK, students)
}

When I used swag init and go run main.go, Swagger UI still showing the example not my endpoint.

enter image description here

Anyone can help me?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
David Winalda
  • 103
  • 1
  • 4
  • 14

3 Answers3

8

I ran into this too

The issue is that we imported the basic example docs:

import (
    ...

    _ "github.com/swaggo/gin-swagger/example/basic/docs"
)

You need to change that to the docs package that was generated by swag init. Assuming your module name is gowebservice, then:

import (
    ...

    _ "gowebservice/docs"
)

Running go run main.go after that should make Swagger UI find your own documentation instead! :D

Wei-Liang Chew
  • 236
  • 2
  • 4
0

Whether or not these files have been generated?

docs.go      swagger.json swagger.yaml
 swag help init
NAME:
   swag init - Create docs.go

USAGE:
   swag init [command options] [arguments...]

OPTIONS:
   --generalInfo value, -g value       Go file path in which 'swagger general API Info' is written (default: "main.go")
   --dir value, -d value               Directory you want to parse (default: "./")
   --propertyStrategy value, -p value  Property Naming Strategy like snakecase,camelcase,pascalcase (default: "camelcase")
   --output value, -o value            Output directory for all the generated files(swagger.json, swagger.yaml and doc.go) (default: "./docs")
   --parseVendor                       Parse go files in 'vendor' folder, disabled by default
   --parseDependency                   Parse go files in outside dependency folder, disabled by default
   --markdownFiles value, --md value   Parse folder containing markdown files to use as description, disabled by default
   --generatedTime                     Generate timestamp at the top of docs.go, true by default
LetsGoFun
  • 39
  • 6
0

Maybe you forgot that swag init -d [...arguments] actually expects a comma separated list of directories to scan. Do not scan only main package but others too.

For example: swag init -d cmd/server,pkg/api,pkg/handlers