save doc generate
This commit is contained in:
parent
e4e8702fd4
commit
88715a7d56
105
generate.go
105
generate.go
@ -9,7 +9,6 @@ package api_doc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"git.zhangdeman.cn/gateway/api-doc/define"
|
"git.zhangdeman.cn/gateway/api-doc/define"
|
||||||
"git.zhangdeman.cn/zhangdeman/consts"
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||||
@ -23,26 +22,38 @@ import (
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 15:56 2024/7/22
|
// Date : 15:56 2024/7/22
|
||||||
func NewOpenapiDoc() *Generate {
|
func NewOpenapiDoc(info *define.Info, servers []*define.ServerItem) *Generate {
|
||||||
return &Generate{
|
if nil == info {
|
||||||
docData: &define.OpenapiDoc{
|
info = &define.Info{
|
||||||
Openapi: consts.SwaggerDocVersion3,
|
|
||||||
Info: &define.Info{
|
|
||||||
Description: "openapi接口文档",
|
Description: "openapi接口文档",
|
||||||
Title: "openapi接口文档",
|
Title: "openapi接口文档",
|
||||||
TermsOfService: "",
|
TermsOfService: "",
|
||||||
Contact: &define.Contact{
|
Contact: nil,
|
||||||
Name: "",
|
License: nil,
|
||||||
|
Version: "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if nil == info.License {
|
||||||
|
info.License = &define.License{
|
||||||
|
Name: consts.LicenseApache20,
|
||||||
|
Url: consts.LicenseUrlTable[consts.LicenseApache20],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if nil == info.Contact {
|
||||||
|
info.Contact = &define.Contact{
|
||||||
|
Name: "研发人员(developer)",
|
||||||
Url: "",
|
Url: "",
|
||||||
Email: "",
|
Email: "",
|
||||||
},
|
}
|
||||||
License: &define.License{
|
}
|
||||||
Name: "",
|
if nil == servers {
|
||||||
Url: "",
|
servers = []*define.ServerItem{}
|
||||||
},
|
}
|
||||||
Version: "",
|
return &Generate{
|
||||||
},
|
docData: &define.OpenapiDoc{
|
||||||
Servers: make([]*define.ServerItem, 0),
|
Openapi: consts.SwaggerDocVersion3,
|
||||||
|
Info: info,
|
||||||
|
Servers: servers,
|
||||||
Components: &define.Components{Schemas: map[string]*define.Schema{}},
|
Components: &define.Components{Schemas: map[string]*define.Schema{}},
|
||||||
Tags: make([]*define.TagItem, 0),
|
Tags: make([]*define.TagItem, 0),
|
||||||
Paths: make(map[string]*define.PathConfig),
|
Paths: make(map[string]*define.PathConfig),
|
||||||
@ -126,23 +137,6 @@ func (g *Generate) AddServer(serverDomain string, serverDesc string, serverVaria
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
type PathItemOperationConfig struct {
|
|
||||||
Tags []string `json:"tags"` // 用于控制API文档的标签列表,标签可以用于在逻辑上分组对资源的操作或作为其它用途的先决条件。
|
|
||||||
Summary string `json:"summary"` // 对此操作行为的简短描述。
|
|
||||||
Description string `json:"description"` // 对此操作行为的详细解释。CommonMark syntax可以被用来呈现富文本格式.
|
|
||||||
ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"` // 附加的外部文档。
|
|
||||||
OperationID string `json:"operationId"` // 用于标识此操作的唯一字符串,这个id在此API内包含的所有操作中必须是唯一的。相关的工具和库可能会使用此operationId来唯一的标识一个操作,因此推荐在命名时符合一般的编程命名习惯
|
|
||||||
Parameters []*PathConfigParameter `json:"parameters,omitempty"` // 定义可用于此操作的参数列表,如果一个同名的参数已经存在于 Path Item,那么这里的定义会覆盖它但是不能移除上面的定义。这个列表不允许包含重复的参数,参数的唯一性由 name 和 location 的组合来确定。这个列表可以使用 Reference 对象 来连接定义于 OpenAPI 对象 components/parameters 的参数。
|
|
||||||
RequestBody *RequestBody `json:"requestBody,omitempty"` // 可用于此操作的请求体。requestBody 只能被用于HTTP 1.1 规范 RFC7231 中明确定义了包含请求体的请求方法,在其他没有明确定义的请求方法中,requestBody的消费者应该应该忽略requestBody。
|
|
||||||
Responses map[string]*Response `json:"responses"` // 描述一个操作可能发生的响应的响应码与响应包含的响应体的对象。default字段可以用来标记一个响应适用于其他未被规范明确定义的HTTP响应码的默认响应。一个Responses 对象必须至少包含一个响应码,而且是成功的响应。
|
|
||||||
Callbacks map[string]any `json:"callbacks,omitempty"` // 一组相对于父操作的可能出现的回调映射,映射中的每一个键都唯一的映射一个 Callback 对象
|
|
||||||
Deprecated bool `json:"deprecated"` // 声明此操作已经被废弃,使用者应该尽量避免使用此操作,默认的值是 false。
|
|
||||||
Security any `json:"security,omitempty"` // 声明那种安全机制可用于此操作。这个列表可以包含多种可用于此操作的安全需求对象,但是在认证一个请求时应该仅使用其中一种。这里的定义会覆盖任何在顶层 security 中的安全声明,因此可以声明一个空数组来变相的移除顶层的安全声明。
|
|
||||||
Servers []*ServerItem `json:"servers,omitempty"` // 一个可用于此操作的额外的 server 数组,这里的定义会覆盖 Path Item 对象 或 顶层的定义。
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// AddApi 新增Api
|
// AddApi 新增Api
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
@ -256,30 +250,37 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 15:25 2025/2/8
|
// Date : 15:25 2025/2/8
|
||||||
func (g *Generate) AddComponentsSchema(pkgPath string, inputType reflect.Type) string {
|
func (g *Generate) AddComponentsSchema(schemaName string, inputType reflect.Type) string {
|
||||||
if len(pkgPath) == 0 {
|
|
||||||
pkgPath = inputType.PkgPath()
|
|
||||||
}
|
|
||||||
schemaPrefix := ""
|
|
||||||
if "" != pkgPath {
|
|
||||||
schemaPrefix = pkgPath + "."
|
|
||||||
}
|
|
||||||
if inputType.Kind() == reflect.Ptr {
|
|
||||||
inputType = inputType.Elem()
|
|
||||||
}
|
|
||||||
schemaName := schemaPrefix + inputType.Name()
|
|
||||||
if inputType.Kind() == reflect.Map {
|
|
||||||
// map, 直接添加公共
|
|
||||||
if _, exist := g.docData.Components.Schemas[schemaName]; !exist {
|
if _, exist := g.docData.Components.Schemas[schemaName]; !exist {
|
||||||
g.docData.Components.Schemas[schemaName] = &define.Schema{
|
g.docData.Components.Schemas[schemaName] = &define.Schema{
|
||||||
Type: consts.SwaggerDataTypeObject,
|
Nullable: false,
|
||||||
|
Discriminator: nil,
|
||||||
|
ReadOnly: false,
|
||||||
|
WriteOnly: false,
|
||||||
|
Xml: nil,
|
||||||
|
ExternalDocs: nil,
|
||||||
|
Example: "",
|
||||||
|
Deprecated: false,
|
||||||
|
Properties: make(map[string]*define.Property),
|
||||||
|
Required: make([]string, 0),
|
||||||
|
Enum: make([]any, 0),
|
||||||
|
Type: "",
|
||||||
|
Ref: "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if inputType.Kind() == reflect.Map {
|
||||||
|
// map, 直接添加公共
|
||||||
|
g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeObject
|
||||||
return schemaName
|
return schemaName
|
||||||
}
|
}
|
||||||
// 数组
|
// 数组
|
||||||
if inputType.Kind() == reflect.Slice || inputType.Kind() == reflect.Array {
|
if inputType.Kind() == reflect.Slice || inputType.Kind() == reflect.Array {
|
||||||
g.parseSliceItem(inputType)
|
sliceItemType := g.parseSliceItem(inputType)
|
||||||
|
g.docData.Components.Schemas[schemaName].Properties[inputType.Name()] = &define.Property{
|
||||||
|
Type: consts.SwaggerDataTypeArray,
|
||||||
|
Format: inputType.String(),
|
||||||
|
Items: &define.PropertyXOf{Ref: g.getSchemaRes(sliceItemType)},
|
||||||
|
}
|
||||||
return schemaName
|
return schemaName
|
||||||
}
|
}
|
||||||
// 结构体
|
// 结构体
|
||||||
@ -291,8 +292,12 @@ func (g *Generate) AddComponentsSchema(pkgPath string, inputType reflect.Type) s
|
|||||||
inputType.Field(i).Type.Kind() == reflect.Array ||
|
inputType.Field(i).Type.Kind() == reflect.Array ||
|
||||||
inputType.Field(i).Type.Kind() == reflect.Slice {
|
inputType.Field(i).Type.Kind() == reflect.Slice {
|
||||||
g.AddComponentsSchema(inputType.PkgPath(), inputType.Field(i).Type)
|
g.AddComponentsSchema(inputType.PkgPath(), inputType.Field(i).Type)
|
||||||
|
} else {
|
||||||
|
g.docData.Components.Schemas[schemaName].Properties[inputType.Field(i).Type.String()] = &define.Property{
|
||||||
|
Type: "string",
|
||||||
|
Format: inputType.Field(i).Type.String(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fmt.Println(inputType.Field(i).Name)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return schemaName
|
return schemaName
|
||||||
|
@ -29,7 +29,7 @@ func Test_parser_Openapi3(t *testing.T) {
|
|||||||
List []A `json:"list"`
|
List []A `json:"list"`
|
||||||
}
|
}
|
||||||
var bArr []*B
|
var bArr []*B
|
||||||
g := NewOpenapiDoc()
|
g := NewOpenapiDoc(nil, nil)
|
||||||
g.AddApiFromInAndOut(&define.UriBaseConfig{
|
g.AddApiFromInAndOut(&define.UriBaseConfig{
|
||||||
Uri: "/a/b/c/d",
|
Uri: "/a/b/c/d",
|
||||||
Method: http.MethodPost,
|
Method: http.MethodPost,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user