feat: 修复数字取值范围与字符长度限制显示错误问题
This commit is contained in:
@@ -137,7 +137,7 @@ func (g *Generate) AddApiDoc(apiMeta define.UriConfig, request any, response any
|
|||||||
|
|
||||||
schemaData := GenerateOpenAPISchema(requestType)
|
schemaData := GenerateOpenAPISchema(requestType)
|
||||||
apiOperate, isRead := g.initApiConfig(apiMeta)
|
apiOperate, isRead := g.initApiConfig(apiMeta)
|
||||||
requestTypeStr := strings.ReplaceAll(requestType.String(), ".", "_")
|
requestTypeStr := requestType.String()
|
||||||
if isRead {
|
if isRead {
|
||||||
for paramName, paramConfig := range schemaData.Value.Properties {
|
for paramName, paramConfig := range schemaData.Value.Properties {
|
||||||
apiOperate.Parameters = append(apiOperate.Parameters, &openapi3.ParameterRef{
|
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,
|
Description: paramConfig.Value.Description,
|
||||||
Style: "",
|
Style: "",
|
||||||
Explode: nil,
|
Explode: nil,
|
||||||
AllowEmptyValue: false,
|
AllowEmptyValue: paramConfig.Value.AllowEmptyValue,
|
||||||
AllowReserved: false,
|
AllowReserved: false,
|
||||||
Deprecated: false,
|
Deprecated: false,
|
||||||
Required: op_array.ArrayType(paramConfig.Value.Required).Has(paramName) >= 0,
|
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 {
|
if _, exist := g.doc.Components.Schemas[requestTypeStr]; !exist {
|
||||||
g.doc.Components.Schemas[requestTypeStr] = schemaData
|
g.doc.Components.Schemas[requestTypeStr] = schemaData
|
||||||
}
|
}
|
||||||
responseTypeStr := strings.ReplaceAll(requestType.String(), ".", "_")
|
responseTypeStr := requestType.String()
|
||||||
if _, exist := g.doc.Components.Schemas[responseTypeStr]; !exist {
|
if _, exist := g.doc.Components.Schemas[responseTypeStr]; !exist {
|
||||||
g.doc.Components.Schemas[responseTypeStr] = GenerateOpenAPISchema(responseType)
|
g.doc.Components.Schemas[responseTypeStr] = GenerateOpenAPISchema(responseType)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
|
|
||||||
// StructFieldInfo 结构体字段信息
|
// StructFieldInfo 结构体字段信息
|
||||||
type StructFieldInfo struct {
|
type StructFieldInfo struct {
|
||||||
|
IsString bool `json:"is_string" dc:"是否字符串"`
|
||||||
Name string `json:"name" dc:"结构体字段名"`
|
Name string `json:"name" dc:"结构体字段名"`
|
||||||
JSONName string `json:"json_name" dc:"json tag"`
|
JSONName string `json:"json_name" dc:"json tag"`
|
||||||
Type reflect.Type `json:"type" dc:"字段类型"`
|
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))
|
validateRule := util.ParseValidateRule(field.Type, util.ParseStructFieldTag.GetValidateRule(field))
|
||||||
info := &StructFieldInfo{
|
info := &StructFieldInfo{
|
||||||
|
IsString: validateRule.IsString,
|
||||||
Name: field.Name,
|
Name: field.Name,
|
||||||
JSONName: "",
|
JSONName: "",
|
||||||
Type: field.Type,
|
Type: field.Type,
|
||||||
@@ -51,16 +53,23 @@ func ParseStructField(field reflect.StructField) *StructFieldInfo {
|
|||||||
Example: util.ParseStructFieldTag.GetExampleValue(field), // 解析示例值
|
Example: util.ParseStructFieldTag.GetExampleValue(field), // 解析示例值
|
||||||
Default: util.ParseStructFieldTag.GetDefaultValue(field), // 解析默认值
|
Default: util.ParseStructFieldTag.GetDefaultValue(field), // 解析默认值
|
||||||
Required: validateRule.Required,
|
Required: validateRule.Required,
|
||||||
Min: validateRule.Min,
|
Min: nil,
|
||||||
Max: validateRule.Max,
|
Max: nil,
|
||||||
MinLength: validateRule.Min,
|
MinLength: nil,
|
||||||
MaxLength: validateRule.Max,
|
MaxLength: nil,
|
||||||
Pattern: validateRule.Regexp,
|
Pattern: validateRule.Regexp,
|
||||||
Format: field.Type.String(),
|
Format: field.Type.String(),
|
||||||
Enum: validateRule.Oneof, // 解析枚举值
|
Enum: validateRule.Oneof, // 解析枚举值
|
||||||
EnumDesc: util.ParseStructFieldTag.EnumDescription(field), // 解析枚举值描述
|
EnumDesc: util.ParseStructFieldTag.EnumDescription(field), // 解析枚举值描述
|
||||||
OmitEmpty: false,
|
OmitEmpty: false,
|
||||||
}
|
}
|
||||||
|
if info.IsString {
|
||||||
|
info.MinLength = validateRule.Min
|
||||||
|
info.MaxLength = validateRule.Max
|
||||||
|
} else {
|
||||||
|
info.Min = validateRule.Min
|
||||||
|
info.Max = validateRule.Max
|
||||||
|
}
|
||||||
|
|
||||||
// 解析 JSON tag
|
// 解析 JSON tag
|
||||||
info.JSONName, info.OmitEmpty = util.ParseStructFieldTag.GetParamName(field)
|
info.JSONName, info.OmitEmpty = util.ParseStructFieldTag.GetParamName(field)
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ func ParseValidateRule(dataType reflect.Type, ruleStr string) ValidateRule {
|
|||||||
}
|
}
|
||||||
dataKind := dataType.Kind()
|
dataKind := dataType.Kind()
|
||||||
rule := ValidateRule{
|
rule := ValidateRule{
|
||||||
|
IsString: dataKind == reflect.String,
|
||||||
Omitempty: false,
|
Omitempty: false,
|
||||||
Required: false,
|
Required: false,
|
||||||
Lte: nil,
|
Lte: nil,
|
||||||
@@ -75,8 +76,10 @@ func ParseValidateRule(dataType reflect.Type, ruleStr string) ValidateRule {
|
|||||||
rule.Lt = &val
|
rule.Lt = &val
|
||||||
case consts.ValidatorRuleCommonLen.String():
|
case consts.ValidatorRuleCommonLen.String():
|
||||||
rule.Len = &val
|
rule.Len = &val
|
||||||
|
|
||||||
case consts.ValidatorRuleCommonMin.String():
|
case consts.ValidatorRuleCommonMin.String():
|
||||||
rule.Min = &val
|
rule.Min = &val
|
||||||
|
|
||||||
case consts.ValidatorRuleCommonMax.String():
|
case consts.ValidatorRuleCommonMax.String():
|
||||||
rule.Max = &val
|
rule.Max = &val
|
||||||
}
|
}
|
||||||
@@ -110,6 +113,7 @@ func ParseValidateRule(dataType reflect.Type, ruleStr string) ValidateRule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ValidateRule struct {
|
type ValidateRule struct {
|
||||||
|
IsString bool `json:"is_string" dc:"是否字符串"`
|
||||||
Omitempty bool `json:"omitempty" dc:"为空则不校验"`
|
Omitempty bool `json:"omitempty" dc:"为空则不校验"`
|
||||||
Required bool `json:"required" dc:"必传校验"`
|
Required bool `json:"required" dc:"必传校验"`
|
||||||
Lte *float64 `json:"lte" dc:"数字类型小于等于"`
|
Lte *float64 `json:"lte" dc:"数字类型小于等于"`
|
||||||
|
|||||||
Reference in New Issue
Block a user