Swag init 生成的只是“通用 API 信息”。

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

Swag init generates nothing but "general API information"

问题

问题描述
在使用swag init命令时,当使用多个目录和--parseDependency--parseInternal标志时,Swaggo无法递归解析Go文件中的Swagger注释。只有使用-g标志指定的主文件中的注释被解析,而指定目录中其他文件中的注释不被考虑。

包含Swaggo注释的文件是internal/transport/http/v1/auth.go、internal/transport/http/v1/trip.go和cmd/app/main.go。

仓库结构在这里

我运行的命令:
swag init -d cmd/app/ -g main.go --parseDependency --parseInternal

swag init -d cmd/app/,internal/transport/http/v1 -g main.go --parseDependency --parseInternal

上述所有命令产生相同的结果。

结果

2023/06/29 17:45:38 生成Swagger文档....
2023/06/29 17:45:38 生成通用API信息,搜索目录:cmd/app/
2023/06/29 17:45:38 生成通用API信息,搜索目录:internal/transport/http/v1
2023/06/29 17:45:40 警告:在/usr/local/go/src/runtime/mprof.go的第165行的const mProfCycleWrap评估失败,reflect: 在零值上调用reflect.Value.Len
2023/06/29 17:45:40 警告:在/Users/ikramkamat/go/GoWorkSpace/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.8/internal/characters/utf8.go的第183行的const locb评估失败,strconv.ParseUint: 解析"b10000000"时出错:无效的语法
2023/06/29 17:45:40 警告:在/Users/ikramkamat/go/GoWorkSpace/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.8/internal/characters/utf8.go的第184行的const hicb评估失败,strconv.ParseUint: 解析"b10111111"时出错:无效的语法
2023/06/29 17:45:40 警告:在/usr/local/go/src/unicode/utf8/utf8.go的第29行的const t1评估失败,strconv.ParseUint: 解析"b00000000"时出错:无效的语法
2023/06/29 17:45:40 警告:在/usr/local/go/src/unicode/utf8/utf8.go的第30行的const tx评估失败,strconv.ParseUint: 解析"b10000000"时出错:无效的语法
2023/06/29 17:45:40 警告:在/usr/local/go/src/unicode/utf8/utf8.go的第31行的const t2评估失败,strconv.ParseUint: 解析"b11000000"时出错:无效的语法
2023/06/29 17:45:40 警告:在/usr/local/go/src/unicode/utf8/utf8.go的第32行的const t3评估失败,strconv.ParseUint: 解析"b11100000"时出错:无效的语法
2023/06/29 17:45:40 警告:在/usr/local/go/src/unicode/utf8/utf8.go的第33行的const t4评估失败,strconv.ParseUint: 解析"b11110000"时出错:无效的语法
2023/06/29 17:45:40 警告:在/usr/local/go/src/unicode/utf8/utf8.go的第34行的const t5评估失败,strconv.ParseUint: 解析"b11111000"时出错:无效的语法
2023/06/29 17:45:40 警告:在/usr/local/go/src/unicode/utf8/utf8.go的第36行的const maskx评估失败,strconv.ParseUint: 解析"b00111111"时出错:无效的语法
2023/06/29 17:45:40 警告:在/usr/local/go/src/unicode/utf8/utf8.go的第37行的const mask2评估失败,strconv.ParseUint: 解析"b00011111"时出错:无效的语法
2023/06/29 17:45:40 警告:在/usr/local/go/src/unicode/utf8/utf8.go的第38行的const mask3评估失败,strconv.ParseUint: 解析"b00001111"时出错:无效的语法
2023/06/29 17:45:40 警告:在/usr/local/go/src/unicode/utf8/utf8.go的第39行的const mask4评估失败,strconv.ParseUint: 解析"b00000111"时出错:无效的语法
2023/06/29 17:45:40 警告:在/usr/local/go/src/unicode/utf8/utf8.go的第46行的const locb评估失败,strconv.ParseUint: 解析"b10000000"时出错:无效的语法
2023/06/29 17:45:40 警告:在/usr/local/go/src/unicode/utf8/utf8.go的第47行的const hicb评估失败,strconv.ParseUint: 解析"b10111111"时出错:无效的语法
2023/06/29 17:45:40 在docs/docs.go中创建docs.go
2023/06/29 17:45:40 在docs/swagger.json中创建swagger.json
2023/06/29 17:45:40 在docs/swagger.yaml中创建swagger.yaml

生成的swagger.yaml

basePath: /api/v1
host: localhost:8000
info:
  contact: {}
  description: InnoCoTravel App的REST API
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  title: InnoCoTravel API
  version: "1.0"
paths: {}
securityDefinitions:
  ApiKeyAuth:
    in: header
    name: Authorization
    type: apiKey
swagger: "2.0"

除cmd/app/main.go之外的文件中的注释

package v1

import (
	"encoding/json"
	"net/http"

	"github.com/gin-gonic/gin"
	_ "github.com/itoqsky/InnoCoTravel-backend/docs"
	"github.com/itoqsky/InnoCoTravel-backend/internal/core"
)

// @Summary SignUp
// @Tags auth
// @Description create account
// @ModuleID signUp
// @ID create-account
// @Accept json
// @Produce json
// @Param input body core.User true "sign up info"
// @Success 200 {integer} integer
// @Failure 400 {object} errorResponse
// @Failure 404 {object} errorResponse
// @Failure 500 {object} errorResponse
// @Failure default {object} errorResponse
// @Router /auth/sign-up [post]

func (h *Handler) signUp(c *gin.Context) {
	// 其他代码
}

swag版本
例如:1.8.12

go版本
例如:1.20.2

操作系统
OSX

英文:

Issue Description
Swaggo fails to recursively parse Swagger comments in Go files when using the swag init command with multiple directories and the --parseDependency and --parseInternal flags. Only the comments in the main file specified with the -g flag are parsed, while the comments in other files within the specified directories are not considered.

Files that contain swaggo comments are internal/transport/http/v1/auth.go, internal/transport/http/v1/trip.go, and cmd/app/main.go

Repo structure is here

commands I have run:
swag init -d cmd[enter image description here][1]/app/ -g main.go --parseDependency --parseInternal

swag init -d cmd/app/,internal/transport/http/v1 -g main.go --parseDependency --parseInternal

All the commands above produces the same result

Results

2023/06/29 17:45:38 Generate swagger docs....
2023/06/29 17:45:38 Generate general API Info, search dir:cmd/app/
2023/06/29 17:45:38 Generate general API Info, search dir:internal/transport/http/v1
2023/06/29 17:45:40 warning: failed to evaluate const mProfCycleWrap at /usr/local/go/src/runtime/mprof.go:165:7, reflect: call of reflect.Value.Len on zero Value
2023/06/29 17:45:40 warning: failed to evaluate const locb at /Users/ikramkamat/go/GoWorkSpace/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.8/internal/characters/utf8.go:183:2, strconv.ParseUint: parsing "b10000000": invalid syntax
2023/06/29 17:45:40 warning: failed to evaluate const hicb at /Users/ikramkamat/go/GoWorkSpace/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.8/internal/characters/utf8.go:184:2, strconv.ParseUint: parsing "b10111111": invalid syntax
2023/06/29 17:45:40 warning: failed to evaluate const t1 at /usr/local/go/src/unicode/utf8/utf8.go:29:2, strconv.ParseUint: parsing "b00000000": invalid syntax
2023/06/29 17:45:40 warning: failed to evaluate const tx at /usr/local/go/src/unicode/utf8/utf8.go:30:2, strconv.ParseUint: parsing "b10000000": invalid syntax
2023/06/29 17:45:40 warning: failed to evaluate const t2 at /usr/local/go/src/unicode/utf8/utf8.go:31:2, strconv.ParseUint: parsing "b11000000": invalid syntax
2023/06/29 17:45:40 warning: failed to evaluate const t3 at /usr/local/go/src/unicode/utf8/utf8.go:32:2, strconv.ParseUint: parsing "b11100000": invalid syntax
2023/06/29 17:45:40 warning: failed to evaluate const t4 at /usr/local/go/src/unicode/utf8/utf8.go:33:2, strconv.ParseUint: parsing "b11110000": invalid syntax
2023/06/29 17:45:40 warning: failed to evaluate const t5 at /usr/local/go/src/unicode/utf8/utf8.go:34:2, strconv.ParseUint: parsing "b11111000": invalid syntax
2023/06/29 17:45:40 warning: failed to evaluate const maskx at /usr/local/go/src/unicode/utf8/utf8.go:36:2, strconv.ParseUint: parsing "b00111111": invalid syntax
2023/06/29 17:45:40 warning: failed to evaluate const mask2 at /usr/local/go/src/unicode/utf8/utf8.go:37:2, strconv.ParseUint: parsing "b00011111": invalid syntax
2023/06/29 17:45:40 warning: failed to evaluate const mask3 at /usr/local/go/src/unicode/utf8/utf8.go:38:2, strconv.ParseUint: parsing "b00001111": invalid syntax
2023/06/29 17:45:40 warning: failed to evaluate const mask4 at /usr/local/go/src/unicode/utf8/utf8.go:39:2, strconv.ParseUint: parsing "b00000111": invalid syntax
2023/06/29 17:45:40 warning: failed to evaluate const locb at /usr/local/go/src/unicode/utf8/utf8.go:46:2, strconv.ParseUint: parsing "b10000000": invalid syntax
2023/06/29 17:45:40 warning: failed to evaluate const hicb at /usr/local/go/src/unicode/utf8/utf8.go:47:2, strconv.ParseUint: parsing "b10111111": invalid syntax
2023/06/29 17:45:40 create docs.go at docs/docs.go
2023/06/29 17:45:40 create swagger.json at docs/swagger.json
2023/06/29 17:45:40 create swagger.yaml at docs/swagger.yaml

generated swagger.yaml

basePath: /api/v1
host: localhost:8000
info:
  contact: {}
  description: REST API for InnoCoTravel App
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  title: InnoCoTravel API
  version: "1.0"
paths: {}
securityDefinitions:
  ApiKeyAuth:
    in: header
    name: Authorization
    type: apiKey
swagger: "2.0"

Comments in one of the files other than cmd/app/main.g

package v1

import (
	"encoding/json"
	"net/http"

	"github.com/gin-gonic/gin"
	_ "github.com/itoqsky/InnoCoTravel-backend/docs"
	"github.com/itoqsky/InnoCoTravel-backend/internal/core"
)

//	@Summary		SignUp
//	@Tags			auth
//	@Description	create account
//	@ModuleID		signUp
//	@ID				create-account
//	@Accept			json
//	@Produce		json
//	@Param			input	body		core.User	true	"sign up info"
//	@Success		200		{integer}	integer
//	@Failure		400		{object}	errorResponse
//	@Failure		404		{object}	errorResponse
//	@Failure		500		{object}	errorResponse
//	@Failure		default	{object}	errorResponse
//	@Router			/auth/sign-up [post]

func (h *Handler) signUp(c *gin.Context) {
	<other code>

swag version
e.g. 1.8.12

go version
e.g. 1.20.2

OS
OSX

答案1

得分: 1

在使用v1.8.16时,我也遇到了同样的问题。降级到v1.8.4后,一切正常。你可以尝试降级,这样在swaggo中就能正常工作了。

英文:

Have the same issue too when using v1.8.16. Everything works fine after downgrading to v1.8.4. You should try downgrade and it work in swaggo

huangapple
  • 本文由 发表于 2023年6月29日 23:01:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582283.html
匿名

发表评论

匿名网友

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

确定