Skip to content

Commit

Permalink
assemble swagger api json
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyofx committed Jan 26, 2024
1 parent f555af9 commit d601315
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
43 changes: 26 additions & 17 deletions web/endpoints/swagger.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -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

}
}

Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions web/mvc/request_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

0 comments on commit d601315

Please sign in to comment.