95 lines
3.5 KiB
Go
95 lines
3.5 KiB
Go
// Package api_doc ...
|
|
//
|
|
// Description : api_doc ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2024-07-19 17:52
|
|
package api_doc
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"git.zhangdeman.cn/gateway/api-doc/define"
|
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
type Meta struct {
|
|
}
|
|
|
|
// Test_parser_Openapi3 测试数据结构定义正确性
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 17:55 2024/7/19
|
|
func Test_parser_Openapi3(t *testing.T) {
|
|
type User struct {
|
|
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"`
|
|
}
|
|
type UserPut struct {
|
|
Meta `json:"-" deprecated:"false" path:"/user/put/{put_user_id}" method:"PUT" 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 UserGet struct {
|
|
Meta `json:"-" deprecated:"false" path:"/user/detail/get/{put_user_id}" method:"GET" 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 UserHead struct {
|
|
Meta `json:"-" deprecated:"false" path:"/user/detail/head/{put_user_id}" method:"HEAD" 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 List struct {
|
|
Total int64 `json:"total" binding:"required"`
|
|
UserList []User `json:"user_list"`
|
|
}
|
|
var o List
|
|
var f User
|
|
var fd UserDelete
|
|
var up UserPut
|
|
var ug UserGet
|
|
var uh UserHead
|
|
g := NewOpenapiDoc(nil, []*define.ServerItem{
|
|
&define.ServerItem{
|
|
Url: "http://127.0.0.1/v1",
|
|
Description: "v1接口",
|
|
Variables: map[string]*define.ServerItemVariable{
|
|
"test": &define.ServerItemVariable{
|
|
Default: "123456",
|
|
Description: "1111",
|
|
Enum: 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))
|
|
g.AddApiFromInAndOut("", reflect.TypeOf(up), reflect.TypeOf(o))
|
|
g.AddApiFromInAndOut("", reflect.TypeOf(ug), reflect.TypeOf(o))
|
|
g.AddApiFromInAndOut("", reflect.TypeOf(uh), reflect.TypeOf(o))
|
|
byteData, _ := json.Marshal(g.docData)
|
|
fmt.Println(string(byteData))
|
|
}
|
|
|
|
func TestParseForSwagger(t *testing.T) {
|
|
docUrl := "https://git.zhangdeman.cn/swagger.v1.json"
|
|
res, _ := Parse(docUrl)
|
|
serialize.JSON.ConsoleOutput(res)
|
|
}
|