/
helper.go
212 lines (169 loc) · 5.33 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
package openapi3
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/swaggest/openapi-go"
)
// ToParameterOrRef exposes Parameter in general form.
func (p Parameter) ToParameterOrRef() ParameterOrRef {
return ParameterOrRef{
Parameter: &p,
}
}
// WithOperation sets Operation to PathItem.
//
// Deprecated: use Spec.AddOperation.
func (p *PathItem) WithOperation(method string, operation Operation) *PathItem {
return p.WithMapOfOperationValuesItem(strings.ToLower(method), operation)
}
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
}
// 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.Paths.MapOfPathItemValues[path]
operation := pathItem.MapOfOperationValues[method]
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
}
pathItem.WithMapOfOperationValuesItem(method, operation)
s.Paths.WithMapOfPathItemValuesItem(path, pathItem)
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.Paths.MapOfPathItemValues[path]
if _, found := pathItem.MapOfOperationValues[method]; found {
return fmt.Errorf("operation already exists: %s %s", method, path)
}
// Add "No Content" response if there are no responses configured.
if len(operation.Responses.MapOfResponseOrRefValues) == 0 && operation.Responses.Default == nil {
operation.Responses.WithMapOfResponseOrRefValuesItem(strconv.Itoa(http.StatusNoContent), ResponseOrRef{
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().SecuritySchemesEns().WithMapOfSecuritySchemeOrRefValuesItem(
securityName,
SecuritySchemeOrRef{
SecurityScheme: &SecurityScheme{
HTTPSecurityScheme: (&HTTPSecurityScheme{}).WithScheme("basic").WithDescription(description),
},
},
)
}
// SetAPIKeySecurity sets security definition.
func (s *Spec) SetAPIKeySecurity(securityName string, fieldName string, fieldIn openapi.In, description string) {
s.ComponentsEns().SecuritySchemesEns().WithMapOfSecuritySchemeOrRefValuesItem(
securityName,
SecuritySchemeOrRef{
SecurityScheme: &SecurityScheme{
APIKeySecurityScheme: (&APIKeySecurityScheme{}).
WithName(fieldName).
WithIn(APIKeySecuritySchemeIn(fieldIn)).
WithDescription(description),
},
},
)
}
// SetHTTPBearerTokenSecurity sets security definition.
func (s *Spec) SetHTTPBearerTokenSecurity(securityName string, format string, description string) {
ss := &SecurityScheme{
HTTPSecurityScheme: (&HTTPSecurityScheme{}).
WithScheme("bearer").
WithDescription(description),
}
if format != "" {
ss.HTTPSecurityScheme.WithBearerFormat(format)
}
s.ComponentsEns().SecuritySchemesEns().WithMapOfSecuritySchemeOrRefValuesItem(
securityName,
SecuritySchemeOrRef{
SecurityScheme: ss,
},
)
}