From 088b813045d0adb95a3dafb935205c26710bb471 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 6 Jan 2026 19:17:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E5=A4=8D=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E5=8F=96=E5=80=BC=E8=8C=83=E5=9B=B4=E4=B8=8E=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E9=99=90=E5=88=B6=E6=98=BE=E7=A4=BA=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- openapi/generate.go | 6 +++--- openapi/schema.go | 17 +++++++++++++---- util/validate_v10_parse.go | 4 ++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/openapi/generate.go b/openapi/generate.go index 920349e..bd5503a 100644 --- a/openapi/generate.go +++ b/openapi/generate.go @@ -137,7 +137,7 @@ func (g *Generate) AddApiDoc(apiMeta define.UriConfig, request any, response any schemaData := GenerateOpenAPISchema(requestType) apiOperate, isRead := g.initApiConfig(apiMeta) - requestTypeStr := strings.ReplaceAll(requestType.String(), ".", "_") + requestTypeStr := requestType.String() if isRead { for paramName, paramConfig := range schemaData.Value.Properties { apiOperate.Parameters = append(apiOperate.Parameters, &openapi3.ParameterRef{ @@ -152,7 +152,7 @@ func (g *Generate) AddApiDoc(apiMeta define.UriConfig, request any, response any Description: paramConfig.Value.Description, Style: "", Explode: nil, - AllowEmptyValue: false, + AllowEmptyValue: paramConfig.Value.AllowEmptyValue, AllowReserved: false, Deprecated: false, Required: op_array.ArrayType(paramConfig.Value.Required).Has(paramName) >= 0, @@ -176,7 +176,7 @@ func (g *Generate) AddApiDoc(apiMeta define.UriConfig, request any, response any if _, exist := g.doc.Components.Schemas[requestTypeStr]; !exist { g.doc.Components.Schemas[requestTypeStr] = schemaData } - responseTypeStr := strings.ReplaceAll(requestType.String(), ".", "_") + responseTypeStr := requestType.String() if _, exist := g.doc.Components.Schemas[responseTypeStr]; !exist { g.doc.Components.Schemas[responseTypeStr] = GenerateOpenAPISchema(responseType) } diff --git a/openapi/schema.go b/openapi/schema.go index 6d71d25..19f15c7 100644 --- a/openapi/schema.go +++ b/openapi/schema.go @@ -18,6 +18,7 @@ import ( // 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:"字段类型"` @@ -44,6 +45,7 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo { // 解析验证规则 validateRule := util.ParseValidateRule(field.Type, util.ParseStructFieldTag.GetValidateRule(field)) info := &StructFieldInfo{ + IsString: validateRule.IsString, Name: field.Name, JSONName: "", Type: field.Type, @@ -51,16 +53,23 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo { Example: util.ParseStructFieldTag.GetExampleValue(field), // 解析示例值 Default: util.ParseStructFieldTag.GetDefaultValue(field), // 解析默认值 Required: validateRule.Required, - Min: validateRule.Min, - Max: validateRule.Max, - MinLength: validateRule.Min, - MaxLength: validateRule.Max, + Min: nil, + Max: nil, + MinLength: nil, + MaxLength: nil, Pattern: validateRule.Regexp, Format: field.Type.String(), Enum: validateRule.Oneof, // 解析枚举值 EnumDesc: util.ParseStructFieldTag.EnumDescription(field), // 解析枚举值描述 OmitEmpty: false, } + if info.IsString { + info.MinLength = validateRule.Min + info.MaxLength = validateRule.Max + } else { + info.Min = validateRule.Min + info.Max = validateRule.Max + } // 解析 JSON tag info.JSONName, info.OmitEmpty = util.ParseStructFieldTag.GetParamName(field) diff --git a/util/validate_v10_parse.go b/util/validate_v10_parse.go index 56d179c..24312ac 100644 --- a/util/validate_v10_parse.go +++ b/util/validate_v10_parse.go @@ -23,6 +23,7 @@ func ParseValidateRule(dataType reflect.Type, ruleStr string) ValidateRule { } dataKind := dataType.Kind() rule := ValidateRule{ + IsString: dataKind == reflect.String, Omitempty: false, Required: false, Lte: nil, @@ -75,8 +76,10 @@ func ParseValidateRule(dataType reflect.Type, ruleStr string) ValidateRule { rule.Lt = &val case consts.ValidatorRuleCommonLen.String(): rule.Len = &val + case consts.ValidatorRuleCommonMin.String(): rule.Min = &val + case consts.ValidatorRuleCommonMax.String(): rule.Max = &val } @@ -110,6 +113,7 @@ func ParseValidateRule(dataType reflect.Type, ruleStr string) ValidateRule { } type ValidateRule struct { + IsString bool `json:"is_string" dc:"是否字符串"` Omitempty bool `json:"omitempty" dc:"为空则不校验"` Required bool `json:"required" dc:"必传校验"` Lte *float64 `json:"lte" dc:"数字类型小于等于"`