From 99a84d80834db15f5f22f587411cfc58571ef8bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 10 Feb 2025 11:15:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E7=A1=80schema=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/openapi.go | 6 +++--- generate.go | 27 +++++++++++++++++++++------ parser_test.go | 5 ++++- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/define/openapi.go b/define/openapi.go index 5926b16..3aa863b 100644 --- a/define/openapi.go +++ b/define/openapi.go @@ -279,9 +279,9 @@ type Info struct { // // Date : 16:08 2024/4/19 type Contact struct { - Name string `json:"name"` // 人或组织的名称。 - Url string `json:"url"` // 指向联系人信息的 URL 地址,必须是 URL 地址格式。 - Email string `json:"email"` // 人或组织的 email 地址,必须是 email 地址格式 + Name string `json:"name,omitempty"` // 人或组织的名称。 + Url string `json:"url,omitempty"` // 指向联系人信息的 URL 地址,必须是 URL 地址格式。 + Email string `json:"email,omitempty"` // 人或组织的 email 地址,必须是 email 地址格式 } // License 开源协议 diff --git a/generate.go b/generate.go index 4570ea2..efc0a6c 100644 --- a/generate.go +++ b/generate.go @@ -9,6 +9,7 @@ package api_doc import ( "errors" + "fmt" "git.zhangdeman.cn/gateway/api-doc/define" "git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/wrapper" @@ -186,8 +187,8 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r } // post类解析 defaultPkgPath := wrapper.String(strings.ReplaceAll(strings.TrimLeft(baseCfg.Uri, "/"), "/", "_")).SnakeCaseToCamel() - paramPkgPath := defaultPkgPath + "Param" - resultPkgPath := defaultPkgPath + "Result" + paramPkgPath := defaultPkgPath + baseCfg.Method + "Param" + resultPkgPath := defaultPkgPath + baseCfg.Method + "Result" g.AddComponentsSchema(paramPkgPath, paramType) g.AddComponentsSchema(resultPkgPath, resultType) if _, exist := g.docData.Paths[baseCfg.Uri]; !exist { @@ -206,16 +207,30 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r Content: map[string]*define.Media{}, Ref: "", }, - Responses: nil, + Responses: map[string]*define.Response{ + fmt.Sprintf("%v", http.StatusOK): &define.Response{ + Content: map[string]*define.Media{}, + }, + }, Callbacks: nil, Deprecated: false, Security: nil, Servers: nil, } - for _, itemOutputType := range baseCfg.OutputContentType { - cfg.RequestBody.Content[itemOutputType] = &define.Media{ + for _, itemType := range baseCfg.ContentType { + cfg.RequestBody.Content[itemType] = &define.Media{ Schema: &define.Schema{ - Ref: g.getSchemaRes(paramPkgPath + "." + paramType.Name()), + Ref: g.getSchemaRes(paramPkgPath), + }, + Example: "", + Examples: nil, + Encoding: nil, + } + } + for _, itemOutputType := range baseCfg.OutputContentType { + cfg.Responses[fmt.Sprintf("%v", http.StatusOK)].Content[itemOutputType] = &define.Media{ + Schema: &define.Schema{ + Ref: g.getSchemaRes(resultPkgPath), }, Example: "", Examples: nil, diff --git a/parser_test.go b/parser_test.go index 40c83cf..7e3f360 100644 --- a/parser_test.go +++ b/parser_test.go @@ -8,6 +8,7 @@ package api_doc import ( + "encoding/json" "fmt" "git.zhangdeman.cn/gateway/api-doc/define" "git.zhangdeman.cn/zhangdeman/serialize" @@ -28,7 +29,7 @@ func Test_parser_Openapi3(t *testing.T) { type B struct { List []A `json:"list"` } - var bArr []*B + var bArr *B g := NewOpenapiDoc(nil, nil) g.AddApiFromInAndOut(&define.UriBaseConfig{ Uri: "/a/b/c/d", @@ -41,6 +42,8 @@ func Test_parser_Openapi3(t *testing.T) { ParamList: nil, ResultList: nil, }, reflect.TypeOf(bArr), reflect.TypeOf(bArr)) + byteData, _ := json.Marshal(g.docData) + fmt.Println(string(byteData)) fmt.Println(g.parseSliceItem(reflect.TypeOf(bArr))) }