From 613e004bf225485617e0fba89244e922fe31ead7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 11 Feb 2025 22:28:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=94=9F=E6=88=90=E6=8F=8F?= =?UTF-8?q?=E8=BF=B0=EF=BC=8C=E9=BB=98=E8=AE=A4=E5=80=BC=EF=BC=8C=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generate.go | 34 ++++++++++++++++++++-------------- parser_test.go | 2 +- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/generate.go b/generate.go index 83c31c1..dd30e0e 100644 --- a/generate.go +++ b/generate.go @@ -329,25 +329,29 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string, inputType.Field(i).Type.Kind() == reflect.Slice { if convertType := g.realBaseType2SwaggerType(inputType.Field(i).Type.String()); !strings.HasPrefix(convertType, "[]") && convertType != inputType.Field(i).Type.Kind().String() { // 针对基础类型指针 - g.docData.Components.Schemas[schemaName].Properties[inputType.Field(i).Name] = &define.Property{ - Type: g.realBaseType2SwaggerType(convertType), - Format: inputType.Field(i).Type.String(), + g.docData.Components.Schemas[schemaName].Properties[g.getParamName(inputType.Field(i))] = &define.Property{ + Type: g.realBaseType2SwaggerType(convertType), + Format: inputType.Field(i).Type.String(), + Default: g.getDefaultValue(inputType.Field(i)), + Description: g.getParamDesc(inputType.Field(i)), } continue } - g.AddComponentsSchema(schemaName, schemaName+inputType.Field(i).Name, inputType.Field(i).Type) + g.AddComponentsSchema(schemaName, schemaName+g.getParamName(inputType.Field(i)), inputType.Field(i).Type) if inputType.Field(i).Type.Kind() == reflect.Struct || inputType.Field(i).Type.Kind() == reflect.Map { - g.docData.Components.Schemas[schemaName].Properties[schemaName+inputType.Field(i).Name] = &define.Property{ - Type: consts.SwaggerDataTypeObject, - Format: inputType.Field(i).Type.String(), - Properties: map[string]*define.Property{}, + g.docData.Components.Schemas[schemaName].Properties[schemaName+g.getParamName(inputType.Field(i))] = &define.Property{ + Type: consts.SwaggerDataTypeObject, + Format: inputType.Field(i).Type.String(), + Description: g.getParamDesc(inputType.Field(i)), + Properties: map[string]*define.Property{}, } } else if inputType.Field(i).Type.Kind() == reflect.Array || inputType.Field(i).Type.Kind() == reflect.Slice { - g.docData.Components.Schemas[schemaName].Properties[schemaName+inputType.Field(i).Name] = &define.Property{ - Type: consts.SwaggerDataTypeArray, - Format: inputType.Field(i).Type.String(), + g.docData.Components.Schemas[schemaName].Properties[schemaName+g.getParamName(inputType.Field(i))] = &define.Property{ + Type: consts.SwaggerDataTypeArray, + Format: inputType.Field(i).Type.String(), + Description: g.getParamDesc(inputType.Field(i)), Items: &define.PropertyXOf{ Ref: g.parseSliceItem(schemaName, inputType.Field(i).Type), }, @@ -358,9 +362,11 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string, } } else { - g.docData.Components.Schemas[schemaName].Properties[inputType.Field(i).Name] = &define.Property{ - Type: g.realBaseType2SwaggerType(inputType.Field(i).Type.String()), - Format: inputType.Field(i).Type.String(), + g.docData.Components.Schemas[schemaName].Properties[g.getParamName(inputType.Field(i))] = &define.Property{ + Type: g.realBaseType2SwaggerType(inputType.Field(i).Type.String()), + Format: inputType.Field(i).Type.String(), + Default: g.getDefaultValue(inputType.Field(i)), + Description: g.getParamDesc(inputType.Field(i)), } } } diff --git a/parser_test.go b/parser_test.go index 6c947a8..15b5c3f 100644 --- a/parser_test.go +++ b/parser_test.go @@ -24,7 +24,7 @@ import ( // Date : 17:55 2024/7/19 func Test_parser_Openapi3(t *testing.T) { type A struct { - Name string `json:"name"` + Name string `json:"name" d:"zhang" desc:"用户姓名"` } type B struct { List []A `json:"list"`