This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
swagger.go
311 lines (240 loc) · 6.44 KB
/
swagger.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package assert
import (
"errors"
"fmt"
"io"
"io/ioutil"
"strconv"
"strings"
"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/loads"
"github.com/go-openapi/spec"
"github.com/yosida95/uritemplate/v3"
)
const (
// ErrSwaggerLoad returns an error when load swagger document.
ErrSwaggerLoad = err("unable to load the document by uri")
// ErrBodyNotFound returns an error when body does not exists.
ErrBodyNotFound = err("body does not exists")
)
// swagger stores the loaded swagger spec.
type swagger struct {
spec *spec.Swagger
}
var _ Document = &swagger{}
// LoadFromURI loads and expands swagger document by uri.
func LoadFromURI(uri string) (Document, error) {
doc, err := loads.Spec(uri)
if err != nil {
return nil, fmt.Errorf("%s: %w", ErrSwaggerLoad, err)
}
return load(doc)
}
// LoadFromReader loads and expand swagger document from io.Reader.
func LoadFromReader(r io.Reader) (Document, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("%s: %w", ErrSwaggerLoad, err)
}
doc, err := loads.Analyzed(data, "")
if err != nil {
return nil, fmt.Errorf("%s: %w", ErrSwaggerLoad, err)
}
return load(doc)
}
func load(doc *loads.Document) (Document, error) {
doc, err := doc.Expanded()
if err != nil {
return nil, fmt.Errorf("unable to expand the document: %w", err)
}
return &swagger{doc.Spec()}, nil
}
// findPath searches for an uri in document and returns the path.
func (s *swagger) findPath(uri string) (string, error) {
for path := range s.spec.Paths.Paths {
tmpl, err := uritemplate.New(s.spec.BasePath + path)
if err != nil {
return "", fmt.Errorf("resource uri does not match: %w", err)
}
if tmpl.Regexp().MatchString(uri) {
return strings.ReplaceAll(path, "/", "~1"), nil
}
}
return "", errors.New("resource uri does not match")
}
// findNode searches a node using segments in the schema.
func (s *swagger) findNode(segments ...string) (interface{}, error) {
segments = append([]string{""}, segments...)
pointer, err := jsonpointer.New(strings.Join(segments, "/"))
if err != nil {
return nil, err
}
data, _, err := pointer.Get(s.spec)
if err != nil {
return nil, fmt.Errorf("node does not exists: %w", err)
}
return data, nil
}
func (s *swagger) mediaTypes(path, method, segment string) ([]string, error) {
path, err := s.findPath(path)
if err != nil {
return []string{}, err
}
method = strings.ToLower(method)
data, err := s.findNode("paths", path, method, segment)
if err != nil {
return []string{}, err
}
var types []string
if data != nil {
types = data.([]string)
}
if len(types) > 0 {
return types, nil
}
data, err = s.findNode(segment)
if err != nil {
return []string{}, err
}
if data != nil {
types = data.([]string)
}
return types, nil
}
// RequestMediaTypes retrives a list of request media types allowed.
func (s *swagger) RequestMediaTypes(path, method string) ([]string, error) {
return s.mediaTypes(path, method, "consumes")
}
// ResponseMediaTypes retrives a list of response media types allowed.
func (s *swagger) ResponseMediaTypes(path, method string) ([]string, error) {
return s.mediaTypes(path, method, "produces")
}
func (s *swagger) requestParameters(path, method string) ([]spec.Parameter, error) {
var params []spec.Parameter
path, err := s.findPath(path)
if err != nil {
return params, err
}
data, _ := s.findNode("paths", path, "parameters")
if data != nil {
params = append(params, data.([]spec.Parameter)...)
}
data, _ = s.findNode("paths", path, strings.ToLower(method), "parameters")
if data != nil {
params = append(params, data.([]spec.Parameter)...)
}
return params, nil
}
func (s *swagger) response(path, method string, statusCode int) (spec.Response, error) {
var res spec.Response
path, err := s.findPath(path)
if err != nil {
return res, err
}
method = strings.ToLower(method)
data, err := s.findNode("paths", path, method, "responses", strconv.Itoa(statusCode))
if data != nil && err == nil {
return data.(spec.Response), nil
}
data, err = s.findNode("paths", path, method, "responses", "default")
if err != nil {
return res, err
}
return *data.(*spec.Response), err
}
// RequestHeaders retrieves a list of request headers.
func (s *swagger) RequestHeaders(path, method string) (Headers, error) {
headers := Headers{}
params, err := s.requestParameters(path, method)
if err != nil {
return headers, err
}
required := Required{}
for _, param := range params {
if param.In != "header" {
continue
}
name := strings.ToLower(param.Name)
headers[name] = &Param{
param.Type,
param.Description,
param.In,
}
if param.Required {
required = append(required, name)
}
}
if len(required) > 0 {
headers["required"] = required
}
return headers, nil
}
// ResponseHeaders retrieves a list of response headers.
func (s *swagger) ResponseHeaders(path, method string, statusCode int) (Headers, error) {
headers := Headers{}
res, err := s.response(path, method, statusCode)
if err != nil {
return headers, err
}
required := []string{}
for k, schema := range res.Headers {
name := strings.ToLower(k)
headers[name] = schema
required = append(required, name)
}
if len(required) > 0 {
headers["required"] = required
}
return headers, nil
}
// RequestQuery retrieves a list of request query.
func (s *swagger) RequestQuery(path, method string) (Query, error) {
query := Query{}
params, err := s.requestParameters(path, method)
if err != nil {
return query, err
}
required := Required{}
for _, param := range params {
if param.In != "query" {
continue
}
name := strings.ToLower(param.Name)
query[name] = &Param{
param.Type,
param.Description,
param.In,
}
if param.Required {
required = append(required, name)
}
}
if len(required) > 0 {
query["required"] = required
}
return query, nil
}
// RequestBody retrieves the request body.
func (s *swagger) RequestBody(path, method string) (Body, error) {
params, err := s.requestParameters(path, method)
if err != nil {
return nil, err
}
for _, param := range params {
if param.In == "body" {
return Body(param.Schema), nil
}
}
return nil, ErrBodyNotFound
}
// ResponseBody retrieves the response body.
func (s *swagger) ResponseBody(path, method string, statusCode int) (Body, error) {
res, err := s.response(path, method, statusCode)
if err != nil {
return nil, err
}
if res.Schema != nil {
return Body(res.Schema), nil
}
return nil, ErrBodyNotFound
}