Merge pull request '修复设置属性可能出现的NPE问题' (#17) from feature/fix_npe into master
Reviewed-on: #17
This commit is contained in:
commit
22d937a4b1
@ -126,6 +126,7 @@ type Schema struct {
|
|||||||
Xml *XML `json:"xml,omitempty"` // 这只能用于 properties schemas,在root schemas 中没有效果。
|
Xml *XML `json:"xml,omitempty"` // 这只能用于 properties schemas,在root schemas 中没有效果。
|
||||||
ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"` // 此 schema 附加的外部文档。
|
ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"` // 此 schema 附加的外部文档。
|
||||||
Example string `json:"example,omitempty"` // 一个用于示范此 schema实例的示例,可以是任意格式。为了表达无法用 JSON 或 YAML 格式呈现的示例,可以使用 string 类型的值,且在必要的地方需要使用字符转义。
|
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.
|
Deprecated bool `json:"deprecated,omitempty"` // 表示一个 schema 是废弃的,应该逐渐被放弃使用。默认值是 false.
|
||||||
Properties map[string]*Property `json:"properties,omitempty"` // 数据字段 => 数据规则
|
Properties map[string]*Property `json:"properties,omitempty"` // 数据字段 => 数据规则
|
||||||
Required []string `json:"required,omitempty"` // 必传属性列表
|
Required []string `json:"required,omitempty"` // 必传属性列表
|
||||||
|
27
generate.go
27
generate.go
@ -364,20 +364,19 @@ func (g *Generate) ParseReadConfigParam(requestCfg *define.UriBaseConfig, baseRe
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if inputType.Field(i).Type.Kind() == reflect.Interface {
|
if inputType.Field(i).Type.Kind() == reflect.Interface {
|
||||||
baseReqCfg.Parameters = append(baseReqCfg.Parameters, &define.PathConfigParameter{
|
itemParam := &define.PathConfigParameter{
|
||||||
Name: ParseStructFieldTag.GetParamName(inputType.Field(i)),
|
Name: ParseStructFieldTag.GetParamName(inputType.Field(i)),
|
||||||
In: consts.SwaggerParameterInQuery,
|
In: consts.SwaggerParameterInQuery,
|
||||||
Description: ParseStructFieldTag.GetParamDesc(inputType.Field(i)),
|
Description: ParseStructFieldTag.GetParamDesc(inputType.Field(i)),
|
||||||
Required: ValidateRule.IsRequired(inputType.Field(i)),
|
Required: ValidateRule.IsRequired(inputType.Field(i)),
|
||||||
Deprecated: ParseStructFieldTag.Deprecated(inputType.Field(i)),
|
Deprecated: ParseStructFieldTag.Deprecated(inputType.Field(i)),
|
||||||
Schema: &define.Schema{
|
Schema: &define.Schema{
|
||||||
OneOf: g.anyTypeConfig(inputType.Field(i)).OneOf,
|
OneOf: g.anyTypeConfig(inputType.Field(i)).OneOf,
|
||||||
Format: realInputTypeFormat,
|
Format: realInputTypeFormat,
|
||||||
Enum: ValidateRule.Enum(inputType.Field(i)),
|
|
||||||
XEnumDescription: ParseStructFieldTag.EnumDescription(inputType.Field(i)),
|
|
||||||
Example: ParseStructFieldTag.GetExampleValue(inputType.Field(i)),
|
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
g.setStructFieldProperty(itemParam.Schema, inputType.Field(i))
|
||||||
|
baseReqCfg.Parameters = append(baseReqCfg.Parameters, itemParam)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if inputType.Field(i).Type.Kind() == reflect.Ptr {
|
if inputType.Field(i).Type.Kind() == reflect.Ptr {
|
||||||
@ -756,6 +755,20 @@ func (g *Generate) setStructFieldProperty(schema *define.Schema, structField ref
|
|||||||
if isRequired {
|
if isRequired {
|
||||||
schema.Required = append(schema.Required, paramName)
|
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 {
|
if schema.Properties[paramName].Type == consts.SwaggerDataTypeString {
|
||||||
schema.Properties[paramName].MinLength = minVal
|
schema.Properties[paramName].MinLength = minVal
|
||||||
schema.Properties[paramName].MaxLength = maxVal
|
schema.Properties[paramName].MaxLength = maxVal
|
||||||
|
Loading…
x
Reference in New Issue
Block a user