Skip to content

Commit

Permalink
openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
Artanis-c committed Jan 23, 2024
1 parent b86106b commit feefadd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 59 deletions.
2 changes: 1 addition & 1 deletion examples/simpleweb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func registerEndpointRouterConfig(rb router.IRouterBuilder) {
endpoints.UseJwt(rb)
endpoints.UseRouteInfo(rb)
endpoints.UseSwaggerUI(rb, func() swagger.Info {
return swagger.Info{}
return swagger.Info{Title: "simple web swagger", Version: "10.0.0", Description: "尝试对接swagger文档"}
})

rb.GET("/error", func(ctx *context.HttpContext) {
Expand Down
83 changes: 25 additions & 58 deletions web/endpoints/swagger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package endpoints

import (
"encoding/json"
"github.com/yoyofx/yoyogo/abstractions/xlog"
"github.com/yoyofx/yoyogo/pkg/swagger"
"github.com/yoyofx/yoyogo/web/actionresult"
Expand All @@ -11,16 +12,17 @@ import (
"strings"
)

func GetAllController(router router.IRouterBuilder) {
func GetAllController(router router.IRouterBuilder) map[string]map[string]swagger.Path {
builder := router.GetMvcBuilder()
controllerList := builder.GetControllerDescriptorList()
pathMap := make(map[string]map[string]swagger.Path)
for _, controller := range controllerList {
FilterValidParams(controller)
FilterValidParams(controller, pathMap)
}
return pathMap
}

func FilterValidParams(controller mvc.ControllerDescriptor) map[string]swagger.Path {
pathMap := make(map[string]swagger.Path)
func FilterValidParams(controller mvc.ControllerDescriptor, pathMap map[string]map[string]swagger.Path) map[string]map[string]swagger.Path {
suf := len(controller.ControllerName) - 10
basePath := controller.ControllerName[0:suf]
for _, act := range controller.GetActionDescriptors() {
Expand All @@ -35,23 +37,27 @@ func FilterValidParams(controller mvc.ControllerDescriptor) map[string]swagger.P
continue
}
// 拼接api路径
actPath := basePath + "/" + act.ActionName[len(act.ActionMethod):]
pathInfo := swagger.Path{}
pathMap[actPath] = pathInfo
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.MethodInfo.Name == "post" {
//根据请求方法分类
if act.ActionMethod == "post" {
pathInfo := swagger.Path{}
pathInfo.RequestBody = RequestBody(param)
}
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")
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")

}
}
pathInfoMap[act.ActionMethod] = pathInfo
}

}
}

Expand Down Expand Up @@ -89,49 +95,10 @@ func UseSwaggerUI(router router.IRouterBuilder, f func() swagger.Info) {
openapi.Openapi = "3.0.3"
openapi.Info = f()
router.GET("/swagger.json", func(ctx *context.HttpContext) {
GetAllController(router)
swaggerJson := `{
"swagger": "2.0",
"info": {
"title": "My API",
"version": "1.0.0"
},
"host": "localhost:8080",
"basePath": "/",
"schemes": ["http"],
"paths": {
"/users": {
"get": {
"summary": "List users",
"description": "Returns a list of users.",
"responses": {
"200": {
"description": "A list of users",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/User"
}
}
}
}
}
}
},
"definitions": {
"User": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
}
}
}
}
}`
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)})
})

Expand Down

0 comments on commit feefadd

Please sign in to comment.