处理GET类请求文档生成,待完善

This commit is contained in:
白茶清欢 2025-02-14 12:29:28 +08:00
parent 39f9c3d67f
commit 8d31a7f2ce
2 changed files with 85 additions and 16 deletions

View File

@ -171,6 +171,12 @@ func (g *Generate) AddApi(baseCfg *define.UriBaseConfig, paramList []*define.Par
//
// Date : 14:22 2025/2/9
func (g *Generate) AddApiFromInAndOut(paramType reflect.Type, resultType reflect.Type) error {
if paramType.Kind() == reflect.Ptr {
paramType = paramType.Elem()
}
if resultType.Kind() == reflect.Ptr {
resultType = resultType.Elem()
}
baseCfg := g.parseBaseUriConfig(paramType)
if nil == baseCfg {
return errors.New("baseCfg is nil")
@ -182,22 +188,6 @@ func (g *Generate) AddApiFromInAndOut(paramType reflect.Type, resultType reflect
return errors.New("baseCfg.Uri is empty")
}
baseCfg.Method = strings.ToUpper(baseCfg.Method)
paramMethod := []string{
http.MethodGet, http.MethodHead, http.MethodConnect, http.MethodOptions, http.MethodTrace,
}
if wrapper.ArrayType(paramMethod).Has(baseCfg.Method) >= 0 {
// Get类请求, TODO : get类解析
return nil
}
// post类解析
if paramType.Kind() == reflect.Ptr {
paramType = paramType.Elem()
}
if resultType.Kind() == reflect.Ptr {
resultType = resultType.Elem()
}
paramSchemaName := g.AddComponentsSchema("", paramType.PkgPath(), paramType)
resultSchemaName := g.AddComponentsSchema("", resultType.PkgPath(), resultType)
if _, exist := g.docData.Paths[baseCfg.Uri]; !exist {
g.docData.Paths[baseCfg.Uri] = &define.PathConfig{}
}
@ -244,6 +234,20 @@ func (g *Generate) AddApiFromInAndOut(paramType reflect.Type, resultType reflect
})
}
}
paramMethod := []string{
http.MethodGet, http.MethodHead, http.MethodConnect, http.MethodOptions, http.MethodTrace,
}
if wrapper.ArrayType(paramMethod).Has(baseCfg.Method) >= 0 {
// Get类请求, TODO : get类解析
// 参数解析
g.ParseReadConfigParam(baseCfg, cfg, paramType)
// 返回值解析
g.AddComponentsSchema("", resultType.PkgPath(), resultType)
return nil
}
// post类解析
paramSchemaName := g.AddComponentsSchema("", paramType.PkgPath(), paramType)
resultSchemaName := g.AddComponentsSchema("", resultType.PkgPath(), resultType)
for _, itemType := range baseCfg.ContentType {
cfg.RequestBody.Content[itemType] = &define.Media{
Schema: &define.Schema{
@ -289,6 +293,64 @@ func (g *Generate) AddApiFromInAndOut(paramType reflect.Type, resultType reflect
return nil
}
// ParseReadConfigParam 解析get类请求参数
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:55 2025/2/14
func (g *Generate) ParseReadConfigParam(requestCfg *define.UriBaseConfig, baseReqCfg *define.PathItemOperationConfig, inputType reflect.Type) {
if inputType.Kind() == reflect.Ptr {
inputType = inputType.Elem()
}
if inputType.Kind() != reflect.Struct {
panic(requestCfg.Uri + " : request param not struct")
}
for i := 0; i < inputType.NumField(); i++ {
propertyName := ParseStructField.GetParamName(inputType.Field(i))
if propertyName == "-" {
continue
}
realInputTypeFormat := inputType.Field(i).Type.Kind().String()
fieldType := inputType.Field(i).Type
if inputType.Field(i).Type.Kind() == reflect.Ptr {
fieldType = inputType.Field(i).Type.Elem()
}
if fieldType.Kind() == reflect.Ptr ||
fieldType.Kind() == reflect.Struct ||
fieldType.Kind() == reflect.Map ||
fieldType.Kind() == reflect.Array ||
fieldType.Kind() == reflect.Slice {
if convertType := g.realBaseType2SwaggerType(fieldType.String()); !strings.HasPrefix(convertType, "[]") && convertType != inputType.Field(i).Type.Kind().String() {
// 针对基础类型指针
continue
}
} else {
if nil == g.docData.Paths[requestCfg.Uri].Get {
g.docData.Paths[requestCfg.Uri].Get = &define.PathItemOperationConfig{}
}
g.docData.Paths[requestCfg.Uri].Get.Parameters = append(g.docData.Paths[requestCfg.Uri].Get.Parameters, &define.PathConfigParameter{
Name: ParseStructField.GetParamName(inputType.Field(i)),
In: consts.SwaggerParameterInQuery,
Description: ParseStructField.GetParamDesc(inputType.Field(i)),
Required: ValidateRule.IsRequired(inputType.Field(i)),
Deprecated: ParseStructField.Deprecated(inputType.Field(i)),
Schema: &define.Schema{
Type: g.realBaseType2SwaggerType(inputType.Field(i).Type.String()),
Items: nil,
Ref: "",
Format: realInputTypeFormat,
},
AllowEmptyValue: false,
Style: "",
Explode: false,
AllowReserved: false,
Ref: "",
})
}
}
}
// AddComponentsSchema 添加schema
//
// Author : go_developer@163.com<白茶清欢>

View File

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