-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopenapi.go
222 lines (191 loc) · 5.43 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package ginplus
import (
"strings"
"github.com/spf13/viper"
)
const defaultOpenApiYaml = "openapi.yaml"
type (
Info struct {
Title string `yaml:"title,omitempty"`
Version string `yaml:"version,omitempty"`
}
Properties struct {
Properties map[string]SchemaInfo `yaml:"properties,omitempty"`
Type string `yaml:"type,omitempty"`
}
SchemaInfo struct {
Type string `yaml:"type,omitempty"`
Title string `yaml:"title,omitempty"`
Format string `yaml:"format,omitempty"`
Description string `yaml:"description,omitempty"`
Properties map[string]SchemaInfo `yaml:"properties,omitempty"`
Items Properties `yaml:"items,omitempty"`
}
Schema struct {
Schema SchemaInfo `yaml:"schema,omitempty"`
}
ApiContent map[string]Schema
ApiResponse struct {
Description string `yaml:"description"`
Content ApiContent `yaml:"content,omitempty"`
}
ApiRequest struct {
Content ApiContent `yaml:"content,omitempty"`
}
Parameter struct {
Name string `yaml:"name,omitempty"`
In string `yaml:"in,omitempty"`
Required bool `yaml:"required,omitempty"`
Schema SchemaInfo `yaml:"schema,omitempty"`
}
ApiHttpMethod struct {
OperationId string `yaml:"operationId,omitempty"`
Tags []string `yaml:"tags,omitempty"`
Responses map[int]ApiResponse `yaml:"responses,omitempty"`
Parameters []Parameter `yaml:"parameters,omitempty"`
RequestBody ApiRequest `yaml:"requestBody,omitempty"`
}
Path map[string]map[string]ApiHttpMethod
ApiTemplate struct {
Openapi string `yaml:"openapi,omitempty"`
Info Info `yaml:"info,omitempty"`
Paths map[string]Path `yaml:"paths,omitempty"`
}
)
func (l *GinEngine) genOpenApiYaml() {
apiYaml := defaultOpenApiYaml
if l.defaultOpenApiYaml != "" && strings.HasSuffix(l.defaultOpenApiYaml, ".yaml") {
apiYaml = l.defaultOpenApiYaml
}
// 写入之前先清空viper
viper.Reset()
viper.SetConfigFile(apiYaml)
viper.SetConfigPermissions(0644)
viper.SetConfigType("yaml")
viper.Set("openapi", "3.1.3")
viper.Set("info", Info{
Title: l.apiConfig.Title,
Version: l.apiConfig.Version,
})
viper.Set("paths", l.apiToYamlModel())
if err := viper.WriteConfig(); err != nil {
panic(err)
}
}
func isUri(uriStr string) bool {
return uriStr != "" && uriStr != "-"
}
func (l *GinEngine) apiToYamlModel() Path {
apiRoutes := l.apiRoutes
apiPath := make(Path)
for url, info := range apiRoutes {
if _, ok := apiPath[url]; !ok {
apiPath[url] = make(map[string]ApiHttpMethod)
}
methodRoute := make(map[string]ApiHttpMethod)
for _, route := range info {
methodRoute[route.HttpMethod] = ApiHttpMethod{
OperationId: route.MethodName,
Tags: nil,
Responses: map[int]ApiResponse{
200: {
Content: map[string]Schema{
"application/json": {
Schema: SchemaInfo{
Type: "object",
Title: route.RespParams.Name,
Properties: genProperties(route.RespParams.Info),
},
},
},
},
},
Parameters: func() []Parameter {
infos := route.ReqParams.Info
res := make([]Parameter, 0, len(infos))
for _, fieldInfo := range infos {
if fieldInfo.Tags.FormKey == "" && fieldInfo.Tags.UriKey == "" {
continue
}
name := fieldInfo.Tags.FormKey
in := "query"
isUriParam := isUri(fieldInfo.Tags.UriKey)
if isUriParam {
name = fieldInfo.Tags.UriKey
in = "path"
}
res = append(res, Parameter{
Name: name,
In: in,
Required: isUriParam,
Schema: SchemaInfo{
Type: getTypeMap(fieldInfo.Type),
Title: fieldInfo.Tags.Title,
Format: fieldInfo.Tags.Format,
Description: fieldInfo.Tags.Desc,
},
})
}
return res
}(),
RequestBody: ApiRequest{
Content: map[string]Schema{
"application/json": {
Schema: SchemaInfo{
Type: "object",
Title: route.ReqParams.Name,
Properties: genProperties(route.ReqParams.Info),
},
},
},
},
}
}
apiPath[url] = methodRoute
}
return apiPath
}
func genProperties(fieldList []FieldInfo) map[string]SchemaInfo {
if len(fieldList) == 0 {
return nil
}
resp := make(map[string]SchemaInfo)
for _, info := range fieldList {
jsonKey := info.Tags.JsonKey
if jsonKey == "-" || jsonKey == "" {
continue
}
fieldType := getTypeMap(info.Type)
schema := SchemaInfo{
Type: fieldType,
Title: info.Tags.Title,
Format: info.Tags.Format,
Description: info.Tags.Desc,
}
switch fieldType {
case "object":
schema.Properties = genProperties(info.Info)
case "array":
schema.Items.Properties = genProperties(info.Info)
schema.Items.Type = getTypeMap(info.ChildType)
}
resp[info.Tags.JsonKey] = schema
}
return resp
}
// "array", "boolean", "integer", "null", "number", "object", "string"
func getTypeMap(typeStr string) string {
switch typeStr {
case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64", "uintptr":
return "integer"
case "float32", "float64", "complex64", "complex128":
return "number"
case "boolean", "string", "array", "bool":
return typeStr
default:
if strings.HasPrefix(typeStr, "[]") {
return "array"
}
return "object"
}
}