-
Notifications
You must be signed in to change notification settings - Fork 0
/
openapi.go
41 lines (39 loc) · 1.06 KB
/
openapi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package openapi
import (
"github.com/Chise1/openapi/models"
"reflect"
)
func Register2Openapi(n RouteStruct) *RouterHelper {
schemas := NewOpenapiRequest(n)
for name, childSchema := range schemas.Components {
OPENAPI.Components.Schemas[name] = childSchema
}
apiRef := reflect.TypeOf(n.GetReqBody())
endpointName := reflect.TypeOf(n).Name()
pathItem := &models.PathItem{}
method := n.GetMethod()
if method == "" {
method = "GET"
}
path := n.GetPath()
oper := &models.Operation{
Description: n.GetDescription(),
Summary: "",
OperationId: apiRef.PkgPath() + apiRef.Name() + endpointName + method,
RequestBody: schemas.Body,
Parameters: schemas.Parameters,
}
oper.Responses = map[string]*models.Response{"200": &DefaultRes}
oper.Responses = schemas.Response
if method == "GET" {
pathItem.Get = oper
} else if method == "POST" {
pathItem.Post = oper
} else if method == "PUT" {
pathItem.Put = oper
} else if method == "DELETE" {
pathItem.Delete = oper
} //todo 需要支持其他method.
OPENAPI.Paths[path] = pathItem
return schemas
}