英文:
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:
{
"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
答案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 文件,请参考下面的文档,这将解决问题。
如果在 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.
If you face any issues to get swag use below command to get swag on windows
go install github.com/swaggo/swag/cmd/swag
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论