我正在尝试使用 go-swagger 生成我的 Go 服务的规范/文档
swagger generate spec -o ./docs/swagger.json --scan-models
我能够生成基本信息+路线,但我的结构遇到了问题\
// Package classification Users' Data API
//
// Documentation for Users' Data API
//
// Schemes: http
// BasePath: /v1
// Version: 0.1.0
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// swagger:meta
package classification
import (
M "service-users-data/internals/database/models"
)
// A list of all Users
// swagger:response usersResponse
type productsResponseWrapper struct {
// All current Users
// in: body
Body []M.User
}
// Generic error message returned as a string
// swagger:response errorResponse
type errorResponseWrapper struct {
// Description of the error
// in: body
Body M.GenericError
}
招摇结果: docs/swagger.json - github
"responses": {
"errorResponse": {
"description": "Generic error message returned as a string"
},
"usersResponse": {
"description": "A list of all Users",
"schema": {
"type": "array",
"items": {}
}
}
}
// User type define user object that will be stored in the DB
// swagger:model User
type User struct {
// the firstname
FirstName string `bson:"first_name" json:"firstName" validate:"required,max=50"`
MiddleNames []string `bson:"middle_names" json:"middleNames"`
LastName string `bson:"last_name" json:"lastName" validate:"required,max=50"`
Age uint8 `bson:"age" json:"age" validate:"gte=16,lte=99"`
Email string `bson:"email" json:"email" validate:"required,email"`
Adress adress `bson:"adress" json:"adress"`
Salary salary `bson:"salary" json:"salary"`
Job string `bson:"job" json:"job" validate:"max=50"`
JoStatus string `bson:"job_status" json:"jobStatus" validate:"omitempty,oneof=intern extern"`
BeginingDate int `bson:"begining_date" json:"beginingDate"`
NextInterviewDate int `bson:"next_interview_date" json:"nextInterviewDate"`
LastInterviewDate int `bson:"last_interview_date" json:"lastInterViewDate"`
ActivityStatus string `bson:"activity_status" json:"activityStatus" validate:"omitempty,oneof=active inactive"`
CreatedOn int `bson:"created_on" json:"-"`
UpdatedOn int `bson:"updated_on" json:"-"`
DeletedOn int `bson:"deleted_on" json:"-"`
}
type adress struct {
Number uint16 `bson:"number" json:"number"`
Street string `bson:"street" json:"street"`
City string `bson:"city" json:"city"`
Province string `bson:"province" json:"province"`
}
type salary struct {
AmountYear int `bson:"amount_year" json:"amountYear"`
Bonus string `bson:"bonus" json:"bonus"`
}
Swagger 结果: docs/swagger.json - github
"definitions": {
"User": {
"description": "User type define user object that will be stored in the DB",
"x-go-package": "service-users-data/internals/database/models"
}
},
$ swagger validate docs/swagger.json
2022/12/18 09:07:52
The swagger spec at "docs/swagger.json" is valid against swagger specification 2.0
2022/12/18 09:07:52
The swagger spec at "docs/swagger.json" showed up some valid but possibly unwanted constructs.
2022/12/18 09:07:52 See warnings below:
2022/12/18 09:07:52 - WARNING: definition "#/definitions/User" is not used anywhere
感谢您花时间阅读;)
// Package api regroup all http related files
package api
import (
"github.com/go-chi/chi"
)
// UserRoutes function attach each route to the right handler
func UserRoutes() *chi.Mux {
r := chi.NewRouter()
userH := &UserH{}
// swagger:route GET /users User listUsers
// Return a list of all Users
//
// responses:
// 200: usersResponse
// 500: errorResponse
// 503: errorResponse
r.Get("/", userH.GetUsers)
r.Post("/", userH.CreateUser)
return r
}
我也遇到了同样的问题,但设法解决了。 这个问题与我安装 swagger-cli 的方式有关。 在 安装指南页面 上,我选择使用 静态二进制 部分下的指导来安装 swagger。 我遇到了同样的问题,其中模型/结构名称将成为规范的一部分,但不会选取任何结构字段。 我删除了此 cli 安装,而是按照从源安装下的指导进行操作。 下次我执行 swagger 生成规范 -o ./docs/swagger.json --scan-models 命令,规范包括所有结构字段及其注释,正如我最初预期的那样