Skip to content

Commit

Permalink
integration swagger response
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyofx committed Feb 7, 2024
1 parent cf936ce commit bf1b167
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
35 changes: 31 additions & 4 deletions examples/simpleweb/contollers/usercontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/yoyofx/yoyogo/web/captcha"
"github.com/yoyofx/yoyogo/web/context"
"github.com/yoyofx/yoyogo/web/mvc"
"gorm.io/gorm/utils"
"mime/multipart"
"simpleweb/models"
"time"
)

type UserController struct {
Expand Down Expand Up @@ -164,13 +164,40 @@ func (controller UserController) TestFunc(request *struct {
return mvc.Success(request)
}

type DocumentDto struct {
Id uint64 `json:"id" doc:"文档ID"`
Name string `json:"name" doc:"文档名称"`
Time time.Time `json:"time" doc:"创建时间"`
}

// GetDocumentById TestFunc attribute routing @route("/v1/user/doc/{id}")
func (controller UserController) GetDocumentById(request *struct {
mvc.RequestGET `route:"/v1/user/doc/:id" doc:"根据ID获取文档"`
Id uint64 `path:"id" doc:"文档ID"`
}) mvc.ApiDocResult[string] {
}) mvc.ApiDocResult[DocumentDto] {

return mvc.ApiDocumentResult[string]().Success().
Data("Document Id:" + utils.ToString(request.Id)).
response := DocumentDto{Id: request.Id, Name: "test", Time: time.Now()}
return mvc.ApiDocumentResult[DocumentDto]().Success().
Data(response).
Message("GetDocumentById").Build()
}

// custom document response
type DocumentResponse struct {
Message string `json:"message" doc:"消息"`
List []DocumentDto `json:"list" doc:"文档列表"`
Success bool `json:"success" doc:"是否成功"`
}

func (controller UserController) GetDocumentList(request *struct {
mvc.RequestGET `route:"/v1/user/doc/list" doc:"获取全部文档列表"`
}) DocumentResponse {

list := []DocumentDto{
{Id: 1, Name: "test1", Time: time.Now()}, {Id: 2, Name: "test2", Time: time.Now()},
{Id: 3, Name: "test3", Time: time.Now()}, {Id: 4, Name: "test4", Time: time.Now()},
{Id: 5, Name: "test5", Time: time.Now()}, {Id: 6, Name: "test6", Time: time.Now()},
}

return DocumentResponse{Message: "GetDocumentList", List: list, Success: true}
}
26 changes: 22 additions & 4 deletions pkg/swagger/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ func GetSwaggerType(goType string) string {
if strings.Contains(goType, "file") {
return "file"
}
if strings.HasPrefix(goType, "[]") {
return "array"
}
switch goType {
case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
return "integer"
Expand All @@ -19,6 +22,8 @@ func GetSwaggerType(goType string) string {
return "string"
case "bool":
return "boolean"
case "time.Time":
return "string"
default:
return "object"
}
Expand Down Expand Up @@ -52,19 +57,32 @@ func ConvertToSwaggerResponse(data interface{}) map[string]interface{} {
//}

if swaggerType == "array" {
itemTypeName := GetSwaggerType(field.Type.Elem().String())
properties := make(map[string]interface{})
if itemTypeName == "object" {
objectVal := reflect.New(field.Type.Elem()).Elem().Interface()
properties = ConvertToSwaggerResponse(objectVal)["properties"].(map[string]interface{})
}

itemsProperty := map[string]interface{}{
"type": GetSwaggerType(itemTypeName),
"properties": properties,
}

response["properties"].(map[string]interface{})[fieldName] = map[string]interface{}{
"type": "array",
"items": map[string]interface{}{"type": GetSwaggerType(getArrayElementType(fieldType))},
}
"items": itemsProperty}
} else {
response["properties"].(map[string]interface{})[fieldName] = map[string]interface{}{
"type": swaggerType,
"type": swaggerType,
"description": field.Tag.Get("doc"),
}
}

if swaggerType == "object" {
if fieldValue != nil {
response["properties"].(map[string]interface{})[fieldName].(map[string]interface{})["properties"] = ConvertToSwaggerResponse(fieldValue)
fieldMap := ConvertToSwaggerResponse(fieldValue)
response["properties"].(map[string]interface{})[fieldName].(map[string]interface{})["properties"] = fieldMap["properties"]
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion web/endpoints/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ func FilterValidParams(controller mvc.ControllerDescriptor, openapi *swagger.Ope
reg := regexp.MustCompile(`:[a-zA-Z0-9]+`)
actPath = reg.ReplaceAllString(actPath, "{$0}")
actPath = strings.ReplaceAll(actPath, ":", "")

pathInfo.Summary = pathInfo.Summary + " ( Route Attribute ) "
} else {
pathInfo.Summary = pathInfo.Summary + " ( MVC ) "
}
// responses
pathInfo.Responses = make(map[string]swagger.ResponsesItem)
Expand Down

0 comments on commit bf1b167

Please sign in to comment.