feat: 枚举值增加 x-enum-descriptions 用于标记枚举值含义

This commit is contained in:
2026-01-06 22:42:29 +08:00
parent a0090ad524
commit 5d86daadeb
3 changed files with 25 additions and 27 deletions

View File

@@ -11,30 +11,29 @@ import (
"reflect"
"time"
"git.zhangdeman.cn/zhangdeman/api-doc/define"
"git.zhangdeman.cn/zhangdeman/api-doc/util"
"github.com/getkin/kin-openapi/openapi3"
)
// StructFieldInfo 结构体字段信息
type StructFieldInfo struct {
IsString bool `json:"is_string" dc:"是否字符串"`
Name string `json:"name" dc:"结构体字段名"`
JSONName string `json:"json_name" dc:"json tag"`
Type reflect.Type `json:"type" dc:"字段类型"`
Description string `json:"description" dc:"参数描述"`
Example any `json:"example" dc:"示例值"`
Default any `json:"default" dc:"默认值"`
Required bool `json:"required" dc:"是否必传"`
Min *float64 `json:"min" dc:"最小值"`
Max *float64 `json:"max" dc:"最大值"`
MinLength *float64 `json:"min_length" dc:"最小长度"`
MaxLength *float64 `json:"max_length" dc:"最大长度"`
Pattern string `json:"pattern" dc:"模式"`
Format string `json:"format" dc:"格式"`
Enum []any `json:"enum" dc:"枚举值列表"`
EnumDesc []define.EnumValue `json:"enum_desc" dc:"枚举值详细描述"`
OmitEmpty bool `json:"omit_empty" dc:"是否可控"`
IsString bool `json:"is_string" dc:"是否字符串"`
Name string `json:"name" dc:"结构体字段名"`
JSONName string `json:"json_name" dc:"json tag"`
Type reflect.Type `json:"type" dc:"字段类型"`
Description string `json:"description" dc:"参数描述"`
Example any `json:"example" dc:"示例值"`
Default any `json:"default" dc:"默认值"`
Required bool `json:"required" dc:"是否必传"`
Min *float64 `json:"min" dc:"最小值"`
Max *float64 `json:"max" dc:"最大值"`
MinLength *float64 `json:"min_length" dc:"最小长度"`
MaxLength *float64 `json:"max_length" dc:"最大长度"`
Pattern string `json:"pattern" dc:"模式"`
Format string `json:"format" dc:"格式"`
Enum []any `json:"enum" dc:"枚举值列表"`
EnumDesc map[string]string `json:"enum_desc" dc:"枚举值详细描述"`
OmitEmpty bool `json:"omit_empty" dc:"是否可控"`
}
// ParseStructField 解析结构体字段信息
@@ -212,9 +211,10 @@ func applyFieldInfoToSchema(schema *openapi3.Schema, info *StructFieldInfo) {
}
if len(info.Enum) > 0 {
schema.Enum = info.Enum
/*for _, item := range info.Enum {
schema.Enum = append(schema.Enum, item.Value)
}*/
if nil == schema.Extensions {
schema.Extensions = map[string]any{}
}
schema.Extensions["x-enum-descriptions"] = info.EnumDesc
}
}