From d6013154ce9cadf1b6a1d68294e163e12b5afd0d Mon Sep 17 00:00:00 2001 From: yoyofx Date: Fri, 26 Jan 2024 18:47:19 +0800 Subject: [PATCH] assemble swagger api json --- web/endpoints/swagger.go | 43 ++++++++++++++++++++++++---------------- web/mvc/request_body.go | 13 ++++++++++++ 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/web/endpoints/swagger.go b/web/endpoints/swagger.go index 7fef0d5b..b7222419 100644 --- a/web/endpoints/swagger.go +++ b/web/endpoints/swagger.go @@ -1,10 +1,8 @@ package endpoints import ( - "encoding/json" "github.com/yoyofx/yoyogo/abstractions/xlog" "github.com/yoyofx/yoyogo/pkg/swagger" - "github.com/yoyofx/yoyogo/web/actionresult" "github.com/yoyofx/yoyogo/web/context" "github.com/yoyofx/yoyogo/web/mvc" "github.com/yoyofx/yoyogo/web/router" @@ -26,6 +24,13 @@ func FilterValidParams(controller mvc.ControllerDescriptor, pathMap map[string]m suf := len(controller.ControllerName) - 10 basePath := controller.ControllerName[0:suf] for _, act := range controller.GetActionDescriptors() { + // 遍历 action, 拼接api路径 swagger.Path + actPath := "/" + basePath + "/" + act.ActionName[len(act.ActionMethod):] + pathInfoMap := make(map[string]swagger.Path) + pathMap[actPath] = pathInfoMap + pathInfo := swagger.Path{} + + // action params if len(act.MethodInfo.Parameters) > 0 { for _, param := range act.MethodInfo.Parameters { // 跳过HttpContext @@ -36,28 +41,32 @@ func FilterValidParams(controller mvc.ControllerDescriptor, pathMap map[string]m if strings.HasSuffix(param.Name, "Controller") { continue } - // 拼接api路径 - actPath := "/" + basePath + "/" + act.ActionName[len(act.ActionMethod):] - pathInfoMap := make(map[string]swagger.Path) - pathMap[actPath] = pathInfoMap + paramSourceData := param.ParameterType.Elem() fieldNum := paramSourceData.NumField() //根据请求方法分类 - if act.ActionMethod == "post" { - pathInfo := swagger.Path{} - pathInfo.RequestBody = RequestBody(param) + if act.ActionMethod == "post" || act.ActionMethod == "any" { for i := 0; i < fieldNum; i++ { // 获取方法注释 filed := paramSourceData.Field(i) - if filed.Type.Name() == "RequestBody" { - pathInfo.Description = filed.Tag.Get("note") - pathInfo.Summary = filed.Tag.Get("note") - + if strings.Contains(filed.Type.Name(), "Request") { + if filed.Type.Name() == "RequestBody" || filed.Type.Name() == "RequestPOST" { + act.ActionMethod = "post" + } else { + actionMethodDef := filed.Type.Name() + actionMethodDef = strings.ReplaceAll(actionMethodDef, "Request", "") + act.ActionMethod = strings.ToLower(actionMethodDef) // get / head / delete / options / patch / put + } } + pathInfo.Description = filed.Tag.Get("note") + pathInfo.Summary = filed.Tag.Get("note") } - pathInfoMap[act.ActionMethod] = pathInfo + } + pathInfo.RequestBody = RequestBody(param) + pathInfoMap[act.ActionMethod] = pathInfo + } } @@ -97,9 +106,9 @@ func UseSwaggerUI(router router.IRouterBuilder, f func() swagger.Info) { router.GET("/swagger.json", func(ctx *context.HttpContext) { pathMap := GetAllController(router) openapi.Paths = pathMap - marshal, _ := json.Marshal(&openapi) - swaggerJson := marshal - ctx.Render(200, actionresult.Data{ContentType: "application/json; charset=utf-8", Data: []byte(swaggerJson)}) + //marshal, _ := json.Marshal(&openapi) + //swaggerJson := marshal + ctx.JSON(200, openapi) //.Render(200, actionresult.Data{ContentType: "application/json; charset=utf-8", Data: []byte(swaggerJson)}) }) router.GET("/swagger", func(ctx *context.HttpContext) { diff --git a/web/mvc/request_body.go b/web/mvc/request_body.go index 264426ce..e133c7e6 100644 --- a/web/mvc/request_body.go +++ b/web/mvc/request_body.go @@ -8,3 +8,16 @@ type RequestGET struct { type RequestPOST struct { } + +type RequestHead struct { +} + +type RequestDelete struct { +} + +type RequestOptions struct { +} + +type RequestPatch struct{} + +type RequestPut struct{}