api-doc/swagger/run_test.go

153 lines
4.2 KiB
Go
Raw Normal View History

2024-04-22 18:21:46 +08:00
// Package swagger ...
//
// Description : swagger ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-04-22 16:02
package swagger
import (
"encoding/json"
"fmt"
"git.zhangdeman.cn/gateway/api-doc/define"
"git.zhangdeman.cn/zhangdeman/consts"
"net/http"
"testing"
)
func TestGenerate(t *testing.T) {
res, _ := Generate(&define.SwaggerInput{
Schemes: []string{},
2024-04-25 16:34:22 +08:00
Swagger: consts.SwaggerDocVersion2,
2024-04-22 18:21:46 +08:00
Host: "http://www.baidu.com",
BasePath: "/",
Info: define.Info{
Description: "测试",
Title: "测试",
2024-12-23 16:07:10 +08:00
Contact: &define.Contact{
2024-04-22 18:21:46 +08:00
Name: "白茶",
Url: "http://www.baidu.com",
Email: "go@email.com",
},
2024-12-23 16:07:10 +08:00
License: &define.License{
2024-04-22 18:21:46 +08:00
Name: consts.LicenseApache20,
Url: consts.LicenseUrlTable[consts.LicenseApache20],
},
Version: "",
},
PathConfigList: []*define.SwaggerPathInput{
&define.SwaggerPathInput{
Uri: "/test",
Method: http.MethodPost,
ContentType: consts.MimeTypeJson,
Summary: "测试接口",
Description: "测试接口",
2024-04-23 13:49:12 +08:00
TagList: []string{"测试接口生成"},
2024-04-22 18:21:46 +08:00
ParameterList: []*define.SwaggerParameterInput{
&define.SwaggerParameterInput{
2025-02-07 21:27:33 +08:00
Type: consts.DataTypeString.String(),
2024-04-22 18:21:46 +08:00
Description: "姓名",
Name: "name",
In: "body",
Required: true,
EnumList: []interface{}{"zhang", "de", "man"},
},
2024-04-23 14:43:23 +08:00
&define.SwaggerParameterInput{
2025-02-07 21:27:33 +08:00
Type: consts.DataTypeString.String(),
2024-04-23 14:43:23 +08:00
Description: "性别",
Name: "person.sex",
In: "body",
Required: true,
EnumList: []interface{}{"man", "woman", "other"},
},
2024-04-23 15:54:06 +08:00
&define.SwaggerParameterInput{
2025-02-07 21:27:33 +08:00
Type: consts.DataTypeString.String(),
2024-04-23 15:54:06 +08:00
Description: "性别",
Name: "person.job.list.[]",
In: "body",
Required: true,
EnumList: nil,
},
&define.SwaggerParameterInput{
2025-02-07 21:27:33 +08:00
Type: consts.DataTypeInt.String(),
2024-04-23 15:54:06 +08:00
Description: "年份",
Name: "person.job.year.[]",
In: "body",
Required: true,
EnumList: nil,
},
&define.SwaggerParameterInput{
2025-02-07 21:27:33 +08:00
Type: consts.DataTypeInt.String(),
2024-04-23 15:54:06 +08:00
Description: "测试工作",
Name: "person.job.test",
In: "body",
Required: true,
EnumList: nil,
},
2024-04-23 13:49:12 +08:00
&define.SwaggerParameterInput{
2025-02-07 21:27:33 +08:00
Type: consts.DataTypeInt.String(),
2024-04-23 13:49:12 +08:00
Description: "年龄",
Name: "age",
In: "body",
Required: true,
EnumList: []interface{}{18, 19, 20},
},
2024-04-23 16:53:40 +08:00
&define.SwaggerParameterInput{
2025-02-07 21:27:33 +08:00
Type: consts.DataTypeInt.String(),
2024-04-23 16:53:40 +08:00
Description: "年龄",
Name: "test_list.[].age",
In: "body",
Required: true,
EnumList: []interface{}{18, 19, 20},
},
&define.SwaggerParameterInput{
2025-02-07 21:27:33 +08:00
Type: consts.DataTypeString.String(),
2024-04-23 16:53:40 +08:00
Description: "年龄",
Name: "test_list.[].name",
In: "body",
Required: true,
EnumList: nil,
},
2024-04-25 17:21:42 +08:00
&define.SwaggerParameterInput{
2025-02-07 21:27:33 +08:00
Type: consts.DataTypeMapAnyAny.String(),
2024-04-25 17:21:42 +08:00
Description: "测试global_map",
Name: "obj",
In: "body",
Required: true,
EnumList: nil,
},
2024-04-22 18:21:46 +08:00
},
2024-04-22 18:55:03 +08:00
ResponseList: []*define.SwaggerResponseInput{
&define.SwaggerResponseInput{
Code: "200",
Description: "成功",
List: []*define.SwaggerResponseItemInput{
&define.SwaggerResponseItemInput{
2025-02-07 21:27:33 +08:00
Type: consts.DataTypeString.String(),
2024-04-22 18:55:03 +08:00
Description: "姓名",
Field: "nick_name",
IsRequired: false,
},
2024-04-23 20:47:10 +08:00
&define.SwaggerResponseItemInput{
2025-02-07 21:27:33 +08:00
Type: consts.DataTypeString.String(),
2024-04-23 20:47:10 +08:00
Description: "昵称",
Field: "person.nick_name",
IsRequired: false,
},
2024-04-25 17:54:15 +08:00
&define.SwaggerResponseItemInput{
2025-02-07 21:27:33 +08:00
Type: consts.DataTypeMapAnyAny.String(),
2024-04-25 17:54:15 +08:00
Description: "测试返回生成map",
Field: "obj",
IsRequired: false,
},
2024-04-22 18:55:03 +08:00
},
},
},
2024-04-22 18:21:46 +08:00
},
},
})
byteData, _ := json.MarshalIndent(res, "", "\t")
fmt.Println(string(byteData))
}