2

I'm trying to generate swagger.json by using swag init. Here my code:

package main

import (
    _ "album/docs"
    "context"
    "fmt"
    "net/http"
    "os"

    "github.com/gin-gonic/gin"
    "github.com/jackc/pgx/v5"

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

// album represents data about a record album.

// @title          Gin Album Service
// @version        1.0
// @description    A albums management service API in Go using Gin framework.
// @termsOfService https://example.dev

// @contact.name  Santosh Kumar
// @contact.url   https://twitter.com/example
// @contact.email example@gmail.com

// @license.name Apache 2.0
// @license.url  http://www.apache.org/licenses/LICENSE-2.0.html

// @host     localhost:8080
// @BasePath /
func main() {
<some code>
}

// GetAlbums godoc
// @Summary  Retrieves user based on given ID
// @Produce  json
// @Router   /albums/ [get]
func GetAlbums(c *gin.Context) {
    <some code>
}

then after

 swag init 
I have swagger.json file:
{
    "swagger": "2.0",
    "info": {
        "description": "A albums management service API in Go using Gin framework.",
        "title": "Gin Album Service",
        "termsOfService": "https://example.dev",
        "contact": {
            "name": "Santosh Kumar",
            "url": "https://twitter.com/example",
            "email": "example@gmail.com"
        },
        "license": {
            "name": "Apache 2.0",
            "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
        },
        "version": "1.0"
    },
    "host": "localhost:8080",
    "basePath": "/",
    "paths": {}
}

why "paths" is empty? What's wrong with my annotations of GetAlbums?

it seems that I did everything according to the source: https://github.com/swaggo/swag

anton
  • 31
  • 3
  • Comment above `GetAlbums` works for me. `/albums/` showed up in my `swagger.json` - `paths`. I need more detail about your code. – spike014 Sep 29 '22 at 06:37
  • swag.exe version v1.8.6 may be problem in version of swag? GetAlbum just function that return json of all albums `func GetAlbums(c *gin.Context) { rows, err := conn.Query(context.Background(), "select * from \"Album\"") if err != nil { fmt.Println("err - ", err) return } var res = []album{} var al album for rows.Next() { err := rows.Scan(&al.ID, &al.Title, &al.Artist, &al.Price) if err != nil { return } res = append(res, al) } c.IndentedJSON(http.StatusOK, res) }` – anton Sep 29 '22 at 07:47
  • No, I mean the `main` function or the function you set ruote. I use `swag version v1.8.4`. – spike014 Sep 29 '22 at 09:39
  • I tried different options. It seems to me that the content of the function does not matter to swag init. Or is it not? `func main() { var err error conn, err = pgx.Connect(context.Background(), "postgres://postgres:1234@localhost:5432/albums") if err != nil { fmt.Println("Unable to connect to database", err) os.Exit(1) } else { fmt.Println("db ok") } defer conn.Close(context.Background()) router := gin.Default() router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) router.GET("/albums", GetAlbums) router.Run("localhost:8080") }` – anton Sep 29 '22 at 16:23

2 Answers2

1

I found how to fix problem. The wrong json is created when I use

 /usr/lib/go-1.18/src/album/swag init 
but when it's
 /usr/lib/go/src/album/swag init 

everything is OK and json is correct. I don't know why it's happens. "go" is just a link to "go-1.18"

anton
  • 31
  • 3
-1

To crate a correct swag docs json files please go through the below document, this will solve the issue.

https://levelup.gitconnected.com/tutorial-generate-swagger-specification-and-swaggerui-for-gin-go-web-framework-9f0c038483b5

If you face any issues to get swag use below command to get swag on windows

go install github.com/swaggo/swag/cmd/swag

Vijay
  • 311
  • 3
  • 10