diff --git a/define/openapi.go b/define/openapi.go index d361a86..f043b3e 100644 --- a/define/openapi.go +++ b/define/openapi.go @@ -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"` // 必传属性列表 diff --git a/generate.go b/generate.go index 5fc8c90..5c2f56b 100644 --- a/generate.go +++ b/generate.go @@ -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