From 2fe0d11b4600d413ebadf79f484b1caf3a7660ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 20 Feb 2025 17:56:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=BE=E7=BD=AE=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E5=8F=AF=E8=83=BD=E5=87=BA=E7=8E=B0=E7=9A=84NPE?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/openapi.go | 1 + generate.go | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) 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