文档生成支持即系url中的参数

This commit is contained in:
白茶清欢 2025-02-14 11:47:32 +08:00
parent d7cfa5fdd3
commit 39f9c3d67f
3 changed files with 39 additions and 12 deletions

View File

@ -100,17 +100,17 @@ type ExternalDocs struct {
//
// Date : 12:29 2024/7/19
type PathConfigParameter struct {
Name string `json:"name"` // 参数名称
In string `json:"in"` // 必选. 参数的位置,可能的值有 "query", "header", "path" 或 "cookie"。
Description string `json:"description"` // 对此参数的简要描述这里可以包含使用示例。CommonMark syntax可以被用来呈现富文本格式.
Required bool `json:"required"` // 标明此参数是否是必选参数。如果 参数位置 的值是 path那么这个参数一定是 必选 的因此这里的值必须是true。其他的则视情况而定。此字段的默认值是false。
Deprecated bool `json:"deprecated"` // 标明一个参数是被弃用的而且应该尽快移除对它的使用。
Schema *Schema `json:"schema,omitempty"` // 定义适用于此参数的类型结构。 Schema 对象 | Reference 对象
AllowEmptyValue bool `json:"allowEmptyValue"` // 设置是否允许传递空参数这只在参数值为query时有效默认值是false。如果同时指定了style属性且值为n/a无法被序列化,那么此字段 allowEmptyValue应该被忽略。
Style string `json:"style"` // 描述根据参数值类型的不同如何序列化参数。默认值为基于in字段的值query 对应 formpath 对应 simple; header 对应 simple; cookie 对应 form。
Explode bool `json:"explode"` // 当这个值为true时参数值类型为array或object的参数使用数组内的值或对象的键值对生成带分隔符的参数值。对于其他类型的参数这个字段没有任何影响。当 style 是 form时这里的默认值是 true对于其他 style 值类型默认值是false。
AllowReserved bool `json:"allowReserved"` // 决定此参数的值是否允许不使用%号编码使用定义于 RFC3986内的保留字符 :/?#[]@!$&'()*+,;=。 这个属性仅用于in的值是query时此字段的默认值是false。
Ref string `json:"$ref"` // 一个允许引用规范内部的其他部分或外部规范的对象。 Reference 对象 定义于 JSON Reference 且遵循相同的结构、行为和规则。
Name string `json:"name"` // 参数名称
In string `json:"in"` // 必选. 参数的位置,可能的值有 "query", "header", "path" 或 "cookie"。
Description string `json:"description"` // 对此参数的简要描述这里可以包含使用示例。CommonMark syntax可以被用来呈现富文本格式.
Required bool `json:"required"` // 标明此参数是否是必选参数。如果 参数位置 的值是 path那么这个参数一定是 必选 的因此这里的值必须是true。其他的则视情况而定。此字段的默认值是false。
Deprecated bool `json:"deprecated"` // 标明一个参数是被弃用的而且应该尽快移除对它的使用。
Schema *Schema `json:"schema,omitempty,omitempty"` // 定义适用于此参数的类型结构。 Schema 对象 | Reference 对象
AllowEmptyValue bool `json:"allowEmptyValue"` // 设置是否允许传递空参数这只在参数值为query时有效默认值是false。如果同时指定了style属性且值为n/a无法被序列化,那么此字段 allowEmptyValue应该被忽略。
Style string `json:"style,omitempty"` // 描述根据参数值类型的不同如何序列化参数。默认值为基于in字段的值query 对应 formpath 对应 simple; header 对应 simple; cookie 对应 form。
Explode bool `json:"explode,omitempty"` // 当这个值为true时参数值类型为array或object的参数使用数组内的值或对象的键值对生成带分隔符的参数值。对于其他类型的参数这个字段没有任何影响。当 style 是 form时这里的默认值是 true对于其他 style 值类型默认值是false。
AllowReserved bool `json:"allowReserved,omitempty"` // 决定此参数的值是否允许不使用%号编码使用定义于 RFC3986内的保留字符 :/?#[]@!$&'()*+,;=。 这个属性仅用于in的值是query时此字段的默认值是false。
Ref string `json:"$ref,omitempty"` // 一个允许引用规范内部的其他部分或外部规范的对象。 Reference 对象 定义于 JSON Reference 且遵循相同的结构、行为和规则。
}
// Schema ...
@ -133,6 +133,7 @@ type Schema struct {
Type string `json:"type,omitempty"` // 类型
Items *PropertyXOf `json:"items,omitempty"` // items 必须存在如果 type 的值是 array。
Ref string `json:"$ref,omitempty"` // 类型引用
Format string `json:"format,omitempty"` // 格式化类型
}
// Property 是从 JSON Schema 提取出来的,但是做了一些调整以适应 OpenAPI Specification。

View File

@ -208,7 +208,7 @@ func (g *Generate) AddApiFromInAndOut(paramType reflect.Type, resultType reflect
Description: baseCfg.Description,
ExternalDocs: nil,
OperationID: baseCfg.Method + "-" + defaultPkgPath,
Parameters: nil,
Parameters: make([]*define.PathConfigParameter, 0),
RequestBody: &define.RequestBody{
Required: true,
Description: "",
@ -225,6 +225,25 @@ func (g *Generate) AddApiFromInAndOut(paramType reflect.Type, resultType reflect
Security: nil,
Servers: nil,
}
// 解析绑定在url中的参数
if paramList := define.UriParamRegexp.FindAllString(baseCfg.Uri, -1); len(paramList) > 0 {
for _, param := range paramList {
param = strings.TrimPrefix(param, "{")
param = strings.TrimSuffix(param, "}")
cfg.Parameters = append(cfg.Parameters, &define.PathConfigParameter{
Name: param,
In: consts.SwaggerParameterInPath,
Description: param,
Required: true,
Deprecated: false,
Schema: &define.Schema{
Type: consts.SwaggerDataTypeString,
Format: consts.DataTypeString.String(),
},
AllowEmptyValue: false,
})
}
}
for _, itemType := range baseCfg.ContentType {
cfg.RequestBody.Content[itemType] = &define.Media{
Schema: &define.Schema{

View File

@ -35,6 +35,11 @@ func Test_parser_Openapi3(t *testing.T) {
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 List struct {
Total int64 `json:"total" binding:"required"`
UserList []User `json:"user_list"`
@ -42,6 +47,7 @@ func Test_parser_Openapi3(t *testing.T) {
var o List
var f User
var fd UserDelete
var up UserPut
g := NewOpenapiDoc(nil, []*define.ServerItem{
&define.ServerItem{
Url: "http://127.0.0.1/v1",
@ -56,6 +62,7 @@ func Test_parser_Openapi3(t *testing.T) {
})
g.AddApiFromInAndOut(reflect.TypeOf(f), reflect.TypeOf(o))
g.AddApiFromInAndOut(reflect.TypeOf(fd), reflect.TypeOf(o))
g.AddApiFromInAndOut(reflect.TypeOf(up), reflect.TypeOf(o))
byteData, _ := json.Marshal(g.docData)
fmt.Println(string(byteData))
}