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

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

View File

@ -264,6 +264,8 @@ func (g *Generate) AddApiFromInAndOut(paramType reflect.Type, resultType reflect
g.docData.Paths[baseCfg.Uri].Options = cfg
case http.MethodTrace:
g.docData.Paths[baseCfg.Uri].Trace = cfg
default:
panic("unknown method: " + baseCfg.Method)
}
return nil
}

View File

@ -10,6 +10,7 @@ package api_doc
import (
"encoding/json"
"fmt"
"git.zhangdeman.cn/gateway/api-doc/define"
"git.zhangdeman.cn/zhangdeman/serialize"
"reflect"
"testing"
@ -25,7 +26,12 @@ type Meta struct {
// Date : 17:55 2024/7/19
func Test_parser_Openapi3(t *testing.T) {
type User struct {
Meta `json:"-" deprecated:"true" path:"/user/detail" method:"POST" desc:"测试接口" tag:"用户,搜索" content_type:"application/json" output_content_type:"application/json"`
Meta `json:"-" deprecated:"false" path:"/user/detail" method:"POST" desc:"测试接口" tag:"用户,搜索" content_type:"application/json" output_content_type:"application/json"`
Name string `json:"name" d:"zhang" desc:"用户姓名" binding:"required"`
Age string `json:"age" d:"18" desc:"年龄" binding:"required,oneof=12 13 18 90"`
}
type UserDelete struct {
Meta `json:"-" deprecated:"false" path:"/user/detail" method:"DELETE" desc:"测试接口" tag:"用户,搜索" content_type:"application/json" output_content_type:"application/json"`
Name string `json:"name" d:"zhang" desc:"用户姓名" binding:"required"`
Age string `json:"age" d:"18" desc:"年龄" binding:"required,oneof=12 13 18 90"`
}
@ -35,11 +41,23 @@ func Test_parser_Openapi3(t *testing.T) {
}
var o List
var f User
g := NewOpenapiDoc(nil, nil)
var fd UserDelete
g := NewOpenapiDoc(nil, []*define.ServerItem{
&define.ServerItem{
Url: "http://127.0.0.1/v1",
Description: "v1接口",
Variables: nil,
},
&define.ServerItem{
Url: "http://127.0.0.1/v2",
Description: "v2接口",
Variables: nil,
},
})
g.AddApiFromInAndOut(reflect.TypeOf(f), reflect.TypeOf(o))
g.AddApiFromInAndOut(reflect.TypeOf(fd), reflect.TypeOf(o))
byteData, _ := json.Marshal(g.docData)
fmt.Println(string(byteData))
fmt.Println(g.parseSliceItem("", reflect.TypeOf(o)))
}
func TestParseForSwagger(t *testing.T) {