feat: 完善结构体字段类型解析

This commit is contained in:
2026-01-06 10:55:27 +08:00
parent 9f2059837c
commit b03fc5acf9
2 changed files with 65 additions and 7 deletions

View File

@@ -9,7 +9,6 @@ package openapi
import (
"reflect"
"strconv"
"time"
"git.zhangdeman.cn/zhangdeman/api-doc/define"
@@ -69,7 +68,7 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo {
}
// GenerateOpenAPISchema 生成完整的 OpenAPI Schema
func GenerateOpenAPISchema(s interface{}) *openapi3.SchemaRef {
func GenerateOpenAPISchema(s any) *openapi3.SchemaRef {
t := reflect.TypeOf(s)
if t.Kind() == reflect.Ptr {
t = t.Elem()
@@ -78,6 +77,7 @@ func GenerateOpenAPISchema(s interface{}) *openapi3.SchemaRef {
return generateSchemaRecursive(t, make(map[string]bool))
}
// 生成 schema
func generateSchemaRecursive(t reflect.Type, seen map[string]bool) *openapi3.SchemaRef {
// 检查循环引用
typeName := t.PkgPath() + "." + t.Name()
@@ -182,10 +182,10 @@ func applyFieldInfoToSchema(schema *openapi3.Schema, info *StructFieldInfo) {
schema.Max = info.Max
}
if info.MinLength != nil {
schema.MinLength = *info.MinLength
schema.MinLength = uint64(*info.MinLength)
}
if info.MaxLength != nil {
schema.MaxLength = openapi3.Uint64Ptr(*info.MaxLength)
schema.MaxLength = openapi3.Ptr(uint64(*info.MaxLength))
}
if info.Pattern != "" {
schema.Pattern = info.Pattern
@@ -194,10 +194,10 @@ func applyFieldInfoToSchema(schema *openapi3.Schema, info *StructFieldInfo) {
schema.Format = info.Format
}
if len(info.Enum) > 0 {
schema.Enum = make([]any, 0)
for _, item := range info.Enum {
schema.Enum = info.Enum
/*for _, item := range info.Enum {
schema.Enum = append(schema.Enum, item.Value)
}
}*/
}
}