修复设置属性可能出现的NPE问题

This commit is contained in:
白茶清欢 2025-02-20 17:56:12 +08:00
parent 6987b4829b
commit 2fe0d11b46
2 changed files with 21 additions and 7 deletions

View File

@ -126,6 +126,7 @@ type Schema struct {
Xml *XML `json:"xml,omitempty"` // 这只能用于 properties schemas在root schemas 中没有效果。
ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"` // 此 schema 附加的外部文档。
Example string `json:"example,omitempty"` // 一个用于示范此 schema实例的示例可以是任意格式。为了表达无法用 JSON 或 YAML 格式呈现的示例,可以使用 string 类型的值,且在必要的地方需要使用字符转义。
Description string `json:"description,omitempty"` // 一个用于示范此 schema实例的示例可以是任意格式。为了表达无法用 JSON 或 YAML 格式呈现的示例,可以使用 string 类型的值,且在必要的地方需要使用字符转义。
Deprecated bool `json:"deprecated,omitempty"` // 表示一个 schema 是废弃的,应该逐渐被放弃使用。默认值是 false.
Properties map[string]*Property `json:"properties,omitempty"` // 数据字段 => 数据规则
Required []string `json:"required,omitempty"` // 必传属性列表

View File

@ -364,20 +364,19 @@ func (g *Generate) ParseReadConfigParam(requestCfg *define.UriBaseConfig, baseRe
continue
}
if inputType.Field(i).Type.Kind() == reflect.Interface {
baseReqCfg.Parameters = append(baseReqCfg.Parameters, &define.PathConfigParameter{
itemParam := &define.PathConfigParameter{
Name: ParseStructFieldTag.GetParamName(inputType.Field(i)),
In: consts.SwaggerParameterInQuery,
Description: ParseStructFieldTag.GetParamDesc(inputType.Field(i)),
Required: ValidateRule.IsRequired(inputType.Field(i)),
Deprecated: ParseStructFieldTag.Deprecated(inputType.Field(i)),
Schema: &define.Schema{
OneOf: g.anyTypeConfig(inputType.Field(i)).OneOf,
Format: realInputTypeFormat,
Enum: ValidateRule.Enum(inputType.Field(i)),
XEnumDescription: ParseStructFieldTag.EnumDescription(inputType.Field(i)),
Example: ParseStructFieldTag.GetExampleValue(inputType.Field(i)),
OneOf: g.anyTypeConfig(inputType.Field(i)).OneOf,
Format: realInputTypeFormat,
},
})
}
g.setStructFieldProperty(itemParam.Schema, inputType.Field(i))
baseReqCfg.Parameters = append(baseReqCfg.Parameters, itemParam)
continue
}
if inputType.Field(i).Type.Kind() == reflect.Ptr {
@ -756,6 +755,20 @@ func (g *Generate) setStructFieldProperty(schema *define.Schema, structField ref
if isRequired {
schema.Required = append(schema.Required, paramName)
}
if nil == schema.Properties[paramName] {
if schema.Type == consts.SwaggerDataTypeString {
schema.MinLength = minVal
schema.MaxLength = maxVal
} else {
schema.Minimum = minVal
schema.Maximum = maxVal
}
schema.Enum = enum
schema.XEnumDescription = xEnumDescription
schema.Example = example
schema.Description = description
return
}
if schema.Properties[paramName].Type == consts.SwaggerDataTypeString {
schema.Properties[paramName].MinLength = minVal
schema.Properties[paramName].MaxLength = maxVal