初始化gin时,路径为空。

huangapple go评论87阅读模式
英文:

swag init gin. Paths is empty

问题

我正在尝试使用swag init生成swagger.json。这是我的代码:

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表示有关唱片专辑的数据。

// @title          Gin Album Service
// @version        1.0
// @description    使用Gin框架在Go中编写的专辑管理服务API。
// @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() {
	// 一些代码
}

// GetAlbums godoc
// @Summary  根据给定的ID检索用户
// @Produce  json
// @Router   /albums/ [get]
func GetAlbums(c *gin.Context) {
	// 一些代码
}

然后在执行swag init之后,我得到了swagger.json文件:

{
    "swagger": "2.0",
    "info": {
        "description": "使用Gin框架在Go中编写的专辑管理服务API。",
        "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": {}
}

为什么"paths"为空?我的GetAlbums的注释有什么问题吗?

看起来我按照源代码的说明做了一切正确:https://github.com/swaggo/swag

英文:

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 <pre> swag init </pre> I have swagger.json file:

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

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

答案1

得分: 1

我找到了解决问题的方法。当我使用<pre> /usr/lib/go-1.18/src/album/swag init </pre>时,会创建错误的JSON,但是当我使用<pre> /usr/lib/go/src/album/swag init </pre>时,一切正常,JSON是正确的。我不知道为什么会发生这种情况。"go"只是指向"go-1.18"的链接。

英文:

I found how to fix problem. The wrong json is created when I use <pre> /usr/lib/go-1.18/src/album/swag init </pre>
but when it's
<pre> /usr/lib/go/src/album/swag init </pre>
everything is OK and json is correct. I don't know why it's happens. "go" is just a link to "go-1.18"

答案2

得分: -1

为了创建正确的 Swagger 文档的 JSON 文件,请参考下面的文档,这将解决问题。

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

如果在 Windows 上使用 swag 遇到任何问题,请使用以下命令安装 swag:

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

英文:

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

huangapple
  • 本文由 发表于 2022年9月29日 10:27:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/73889660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定