openapi格式的文档基础生成 #3

Merged
zhangdeman merged 32 commits from feature/upgrade_swagger into master 2025-02-14 21:13:00 +08:00
2 changed files with 45 additions and 2 deletions
Showing only changes of commit fce39068ca - Show all commits

View File

@ -122,7 +122,7 @@ type Schema struct {
Discriminator *SchemaDiscriminator `json:"discriminator,omitempty"` // 说白了, 就是一个字段可能是不同的数据结构。。。 Discriminator *SchemaDiscriminator `json:"discriminator,omitempty"` // 说白了, 就是一个字段可能是不同的数据结构。。。
ReadOnly bool `json:"readOnly"` // 仅与 Schema "properties" 定义有关。 声明此属性是 "readonly" 的。这意味着它可以作为 response 的一部分但不应该作为 request 的一部分被发送。如果一个 property 的 readOnly 被标记为 true 且在 required 列表中required 将只作用于 response。一个 property 的 readOnly 和 writeOnly 不允许同时被标记为 true。默认值是 false。 ReadOnly bool `json:"readOnly"` // 仅与 Schema "properties" 定义有关。 声明此属性是 "readonly" 的。这意味着它可以作为 response 的一部分但不应该作为 request 的一部分被发送。如果一个 property 的 readOnly 被标记为 true 且在 required 列表中required 将只作用于 response。一个 property 的 readOnly 和 writeOnly 不允许同时被标记为 true。默认值是 false。
WriteOnly bool `json:"writeOnly"` // 仅与 Schema "properties" 定义有关。声明此 property 为 "write only"。所以它可以作为 request 的一部分而不应该作为 response 的一部分被发送。如果一个 propertywriteOnly 被标记为 true 且在 required 列表中required 将只作用于 request。一个 property 的 readOnly 和 writeOnly 不能同时被标记为 true。默认值是 false。 WriteOnly bool `json:"writeOnly"` // 仅与 Schema "properties" 定义有关。声明此 property 为 "write only"。所以它可以作为 request 的一部分而不应该作为 response 的一部分被发送。如果一个 propertywriteOnly 被标记为 true 且在 required 列表中required 将只作用于 request。一个 property 的 readOnly 和 writeOnly 不能同时被标记为 true。默认值是 false。
Xml *XML `json:"xml,omitempty"` // 这只能用于 properties schemas root schemas 中没有效果。 Xml *XML `json:"xml,omitempty"` // 这只能用于 properties schemasroot schemas 中没有效果。
ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"` // 此 schema 附加的外部文档。 ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"` // 此 schema 附加的外部文档。
Example string `json:"example,omitempty"` // 一个用于示范此 schema实例的示例可以是任意格式。为了表达无法用 JSON 或 YAML 格式呈现的示例,可以使用 string 类型的值,且在必要的地方需要使用字符转义。 Example string `json:"example,omitempty"` // 一个用于示范此 schema实例的示例可以是任意格式。为了表达无法用 JSON 或 YAML 格式呈现的示例,可以使用 string 类型的值,且在必要的地方需要使用字符转义。
Deprecated bool `json:"deprecated"` // 表示一个 schema 是废弃的,应该逐渐被放弃使用。默认值是 false. Deprecated bool `json:"deprecated"` // 表示一个 schema 是废弃的,应该逐渐被放弃使用。默认值是 false.

View File

@ -10,6 +10,7 @@ package api_doc
import ( import (
"git.zhangdeman.cn/gateway/api-doc/define" "git.zhangdeman.cn/gateway/api-doc/define"
"git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/consts"
"reflect"
"strings" "strings"
) )
@ -149,7 +150,7 @@ type PathItemOperationConfig struct {
// &define.UriBaseConfig{ // &define.UriBaseConfig{
// Uri: "/foo/bar", // Uri: "/foo/bar",
// Method: http.MethodPost, // Method: http.MethodPost,
// ContentType: "application/json", // ContentType: ["application/json"],
// TagList: []string{"测试标签"}, // TagList: []string{"测试标签"},
// Summary: "这是一份示例基础配置", // Summary: "这是一份示例基础配置",
// Description: "这是一份示例基础配置", // Description: "这是一份示例基础配置",
@ -161,3 +162,45 @@ type PathItemOperationConfig struct {
func (g *Generate) AddApi(baseCfg *define.UriBaseConfig, paramList []*define.ParamConfig, resultList []*define.ResultConfig) error { func (g *Generate) AddApi(baseCfg *define.UriBaseConfig, paramList []*define.ParamConfig, resultList []*define.ResultConfig) error {
return nil return nil
} }
// AddComponentsSchema 添加schema
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:25 2025/2/8
func (g *Generate) AddComponentsSchema(pkgPath string, inputType reflect.Type) {
if nil == g.docData.Components {
g.docData.Components = &define.Components{
Schemas: map[string]*define.Schema{},
}
}
if nil == g.docData.Components.Schemas {
g.docData.Components.Schemas = make(map[string]*define.Schema)
}
if len(pkgPath) == 0 {
pkgPath = inputType.PkgPath()
}
schemaPrefix := ""
if "" != pkgPath {
schemaPrefix = pkgPath + "."
}
if inputType.Kind() == reflect.Ptr {
inputType = inputType.Elem()
}
if inputType.Kind() == reflect.Map {
// map, 直接添加公共
if _, exist := g.docData.Components.Schemas[schemaPrefix+inputType.Name()]; !exist {
g.docData.Components.Schemas[schemaPrefix+inputType.Name()] = &define.Schema{
Type: consts.SwaggerDataTypeObject,
}
}
return
}
// 数组
if inputType.Kind() == reflect.Slice {
}
// 结构体
if inputType.Kind() == reflect.Struct {
}
}