openapi格式的文档基础生成 #3

Merged
zhangdeman merged 32 commits from feature/upgrade_swagger into master 2025-02-14 21:13:00 +08:00
3 changed files with 28 additions and 10 deletions
Showing only changes of commit 99a84d8083 - Show all commits

View File

@ -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 开源协议

View File

@ -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,

View File

@ -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)))
}