feat: 修复数字取值范围与字符长度限制显示错误问题

This commit is contained in:
2026-01-06 19:17:22 +08:00
parent 708e327ddf
commit 088b813045
3 changed files with 20 additions and 7 deletions

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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:"数字类型小于等于"`