-
Notifications
You must be signed in to change notification settings - Fork 18
/
helper.go
271 lines (221 loc) · 6.28 KB
/
helper.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
package openapi31
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/swaggest/openapi-go"
)
// ToParameterOrRef exposes Parameter in general form.
func (p Parameter) ToParameterOrRef() ParameterOrReference {
return ParameterOrReference{
Parameter: &p,
}
}
// Operation retrieves method Operation from PathItem.
func (p PathItem) Operation(method string) (*Operation, error) {
switch strings.ToUpper(method) {
case http.MethodGet:
return p.Get, nil
case http.MethodPut:
return p.Put, nil
case http.MethodPost:
return p.Post, nil
case http.MethodDelete:
return p.Delete, nil
case http.MethodOptions:
return p.Options, nil
case http.MethodHead:
return p.Head, nil
case http.MethodPatch:
return p.Patch, nil
case http.MethodTrace:
return p.Trace, nil
default:
return nil, fmt.Errorf("unexpected http method: %s", method)
}
}
// SetOperation sets a method Operation to PathItem.
func (p *PathItem) SetOperation(method string, op *Operation) error {
method = strings.ToUpper(method)
switch method {
case http.MethodGet:
p.Get = op
case http.MethodPut:
p.Put = op
case http.MethodPost:
p.Post = op
case http.MethodDelete:
p.Delete = op
case http.MethodOptions:
p.Options = op
case http.MethodHead:
p.Head = op
case http.MethodPatch:
p.Patch = op
case http.MethodTrace:
p.Trace = op
default:
return fmt.Errorf("unexpected http method: %s", method)
}
return nil
}
// SetupOperation creates operation if it is not present and applies setup functions.
func (s *Spec) SetupOperation(method, path string, setup ...func(*Operation) error) error {
method, path, pathParams, err := openapi.SanitizeMethodPath(method, path)
if err != nil {
return err
}
pathItem := s.PathsEns().MapOfPathItemValues[path]
operation, err := pathItem.Operation(method)
if err != nil {
return err
}
if operation == nil {
operation = &Operation{}
}
for _, f := range setup {
if err := f(operation); err != nil {
return err
}
}
pathParamsMap := make(map[string]bool, len(pathParams))
for _, p := range pathParams {
pathParamsMap[p] = true
}
if err := operation.validatePathParams(pathParamsMap); err != nil {
return err
}
if err := pathItem.SetOperation(method, operation); err != nil {
return err
}
s.PathsEns().WithMapOfPathItemValuesItem(path, pathItem)
return nil
}
func (o *Operation) validatePathParams(pathParams map[string]bool) error {
paramIndex := make(map[string]bool, len(o.Parameters))
var errs []string
for _, p := range o.Parameters {
if p.Parameter == nil {
continue
}
if found := paramIndex[p.Parameter.Name+string(p.Parameter.In)]; found {
errs = append(errs, "duplicate parameter in "+string(p.Parameter.In)+": "+p.Parameter.Name)
continue
}
if found := pathParams[p.Parameter.Name]; !found && p.Parameter.In == ParameterInPath {
errs = append(errs, "missing path parameter placeholder in url: "+p.Parameter.Name)
continue
}
paramIndex[p.Parameter.Name+string(p.Parameter.In)] = true
}
for pathParam := range pathParams {
if !paramIndex[pathParam+string(ParameterInPath)] {
errs = append(errs, "undefined path parameter: "+pathParam)
}
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, ", "))
}
return nil
}
// AddOperation validates and sets operation by path and method.
//
// It will fail if operation with method and path already exists.
func (s *Spec) AddOperation(method, path string, operation Operation) error {
method = strings.ToLower(method)
pathItem := s.PathsEns().MapOfPathItemValues[path]
op, err := pathItem.Operation(method)
if err != nil {
return err
}
if op != nil {
return fmt.Errorf("operation already exists: %s %s", method, path)
}
// Add "No Content" response if there are no responses configured.
if len(operation.ResponsesEns().MapOfResponseOrReferenceValues) == 0 && operation.Responses.Default == nil {
operation.Responses.WithMapOfResponseOrReferenceValuesItem(strconv.Itoa(http.StatusNoContent), ResponseOrReference{
Response: &Response{
Description: http.StatusText(http.StatusNoContent),
},
})
}
return s.SetupOperation(method, path, func(op *Operation) error {
*op = operation
return nil
})
}
// UnknownParamIsForbidden indicates forbidden unknown parameters.
func (o Operation) UnknownParamIsForbidden(in ParameterIn) bool {
f, ok := o.MapOfAnything[xForbidUnknown+string(in)].(bool)
return f && ok
}
var _ openapi.SpecSchema = &Spec{}
// Title returns service title.
func (s *Spec) Title() string {
return s.Info.Title
}
// SetTitle describes the service.
func (s *Spec) SetTitle(t string) {
s.Info.Title = t
}
// Description returns service description.
func (s *Spec) Description() string {
if s.Info.Description != nil {
return *s.Info.Description
}
return ""
}
// SetDescription describes the service.
func (s *Spec) SetDescription(d string) {
s.Info.WithDescription(d)
}
// Version returns service version.
func (s *Spec) Version() string {
return s.Info.Version
}
// SetVersion describes the service.
func (s *Spec) SetVersion(v string) {
s.Info.Version = v
}
// SetHTTPBasicSecurity sets security definition.
func (s *Spec) SetHTTPBasicSecurity(securityName string, description string) {
s.ComponentsEns().WithSecuritySchemesItem(
securityName,
SecuritySchemeOrReference{
SecurityScheme: (&SecurityScheme{
HTTP: (&SecuritySchemeHTTP{}).WithScheme("basic"),
}).WithDescription(description),
},
)
}
// SetAPIKeySecurity sets security definition.
func (s *Spec) SetAPIKeySecurity(securityName string, fieldName string, fieldIn openapi.In, description string) {
s.ComponentsEns().WithSecuritySchemesItem(
securityName,
SecuritySchemeOrReference{
SecurityScheme: (&SecurityScheme{
APIKey: (&SecuritySchemeAPIKey{}).
WithName(fieldName).
WithIn(SecuritySchemeAPIKeyIn(fieldIn)),
}).WithDescription(description),
},
)
}
// SetHTTPBearerTokenSecurity sets security definition.
func (s *Spec) SetHTTPBearerTokenSecurity(securityName string, format string, description string) {
ss := (&SecurityScheme{
HTTPBearer: (&SecuritySchemeHTTPBearer{}).
WithScheme("bearer"),
}).WithDescription(description)
if format != "" {
ss.HTTPBearer.WithBearerFormat(format)
}
s.ComponentsEns().WithSecuritySchemesItem(
securityName,
SecuritySchemeOrReference{
SecurityScheme: ss,
},
)
}