-
Notifications
You must be signed in to change notification settings - Fork 72
/
parse_parameter.go
356 lines (313 loc) · 9.75 KB
/
parse_parameter.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package parser
import (
"fmt"
"net/textproto"
"strings"
"github.com/go-faster/errors"
"golang.org/x/net/http/httpguts"
"github.com/ogen-go/ogen"
"github.com/ogen-go/ogen/internal/httpcookie"
"github.com/ogen-go/ogen/jsonpointer"
"github.com/ogen-go/ogen/jsonschema"
"github.com/ogen-go/ogen/location"
"github.com/ogen-go/ogen/openapi"
)
func canonicalParamName(name string, in openapi.ParameterLocation) string {
if in.Header() {
return textproto.CanonicalMIMEHeaderKey(name)
}
return name
}
func mergeParams(opParams, itemParams []*openapi.Parameter) []*openapi.Parameter {
lookupOp := func(name string, in openapi.ParameterLocation) bool {
for _, param := range opParams {
if param.In == in && canonicalParamName(param.Name, in) == canonicalParamName(name, in) {
return true
}
}
return false
}
for _, param := range itemParams {
// Param defined in operation take precedence over param defined in pathItem.
if lookupOp(param.Name, param.In) {
continue
}
opParams = append(opParams, param)
}
return opParams
}
func (p *parser) parseParams(
params []*ogen.Parameter,
locator location.Locator,
ctx *jsonpointer.ResolveCtx,
) ([]*openapi.Parameter, error) {
// Unique parameter is defined by a combination of a name and location.
type pnameLoc struct {
name string
location openapi.ParameterLocation
}
var (
result = make([]*openapi.Parameter, 0, len(params))
unique = make(map[pnameLoc]int, len(params))
)
for idx, spec := range params {
if spec == nil {
loc := locator.Index(idx)
err := errors.Errorf("parameter %d is empty or null", idx)
return nil, p.wrapLocation(p.file(ctx), loc, err)
}
param, err := p.parseParameter(spec, ctx)
if err != nil {
return nil, errors.Wrapf(err, "parse parameter %q", spec.Name)
}
ploc := pnameLoc{
name: canonicalParamName(param.Name, param.In),
location: param.In,
}
if existingIdx, ok := unique[ploc]; ok {
file := p.file(ctx)
me := new(location.MultiError)
me.Report(file, locator.Index(existingIdx), fmt.Sprintf("duplicate parameter: %q in %q", param.Name, param.In))
me.Report(file, locator.Index(idx), "")
return nil, me
}
unique[ploc] = idx
result = append(result, param)
}
return result, nil
}
func (p *parser) validateParameter(
name string,
locatedIn openapi.ParameterLocation,
param *ogen.Parameter,
file location.File,
) error {
locator := param.Common.Locator
switch {
case param.Schema != nil && param.Content != nil:
me := new(location.MultiError)
me.Report(file, locator.Key("schema"), "parameter MUST contain either a schema property, or a content property, but not both")
me.Report(file, locator.Key("content"), "")
return me
case param.Schema == nil && param.Content == nil:
return errors.New("parameter MUST contain either a schema property, or a content property")
case param.Content != nil && len(param.Content) < 1:
// https://github.com/OAI/OpenAPI-Specification/discussions/2875
err := errors.New("content must have at least one entry")
return p.wrapField("content", file, locator, err)
}
// Path parameters are always required.
switch locatedIn {
case openapi.LocationPath:
if !param.Required {
err := errors.New("path parameters must be required")
return p.wrapField("required", file, locator, err)
}
case openapi.LocationHeader:
if !httpguts.ValidHeaderFieldName(name) {
err := errors.Errorf("invalid header name %q", name)
return p.wrapField("name", file, locator, err)
}
case openapi.LocationCookie:
if !httpcookie.IsCookieNameValid(name) {
err := errors.Errorf("invalid cookie name %q", name)
return p.wrapField("name", file, locator, err)
}
}
return nil
}
func (p *parser) parseParameter(param *ogen.Parameter, ctx *jsonpointer.ResolveCtx) (_ *openapi.Parameter, rerr error) {
if param == nil {
return nil, errors.New("parameter object is empty or null")
}
locator := param.Common.Locator
defer func() {
rerr = p.wrapLocation(p.file(ctx), locator, rerr)
}()
if ref := param.Ref; ref != "" {
parsed, err := p.resolveParameter(ref, ctx)
if err != nil {
return nil, p.wrapRef(p.file(ctx), locator, err)
}
return parsed, nil
}
types := map[string]openapi.ParameterLocation{
"query": openapi.LocationQuery,
"header": openapi.LocationHeader,
"path": openapi.LocationPath,
"cookie": openapi.LocationCookie,
}
locatedIn, exists := types[strings.ToLower(param.In)]
if !exists {
err := errors.Errorf("unknown parameter location %q", param.In)
return nil, p.wrapField("in", p.file(ctx), locator, err)
}
if err := p.validateParameter(param.Name, locatedIn, param, p.file(ctx)); err != nil {
return nil, err
}
schema, err := p.parseSchema(param.Schema, ctx)
if err != nil {
return nil, p.wrapField("schema", p.file(ctx), locator, err)
}
content, err := p.parseParameterContent(param.Content, locator.Field("content"), ctx)
if err != nil {
err := errors.Wrap(err, "content")
return nil, p.wrapField("content", p.file(ctx), locator, err)
}
op := &openapi.Parameter{
Name: param.Name,
Description: param.Description,
Deprecated: param.Deprecated,
Schema: schema,
Content: content,
In: locatedIn,
Style: inferParamStyle(locatedIn, param.Style),
Explode: inferParamExplode(locatedIn, param.Explode),
Required: param.Required,
AllowReserved: param.AllowReserved,
Pointer: locator.Pointer(p.file(ctx)),
}
if err := p.validateParamStyle(op, p.file(ctx)); err != nil {
return nil, err
}
return op, nil
}
func inferParamStyle(locatedIn openapi.ParameterLocation, style string) openapi.ParameterStyle {
if style == "" {
defaultStyles := map[openapi.ParameterLocation]openapi.ParameterStyle{
openapi.LocationPath: openapi.PathStyleSimple,
openapi.LocationQuery: openapi.QueryStyleForm,
openapi.LocationHeader: openapi.HeaderStyleSimple,
openapi.LocationCookie: openapi.CookieStyleForm,
}
return defaultStyles[locatedIn]
}
return openapi.ParameterStyle(style)
}
func inferParamExplode(locatedIn openapi.ParameterLocation, explode *bool) bool {
if explode != nil {
return *explode
}
// When style is form, the default value is true.
// For all other styles, the default value is false.
if locatedIn.Query() || locatedIn.Cookie() {
return true
}
return false
}
func (p *parser) validateParamStyle(param *openapi.Parameter, file location.File) error {
// https://swagger.io/docs/specification/serialization/
const (
primitive byte = 1 << iota
array
object
)
type stexp struct {
style openapi.ParameterStyle
explode bool
}
table := map[openapi.ParameterLocation]map[stexp]byte{
openapi.LocationPath: {
{openapi.PathStyleSimple, false}: primitive | array | object,
{openapi.PathStyleSimple, true}: primitive | array | object,
{openapi.PathStyleLabel, false}: primitive | array | object,
{openapi.PathStyleLabel, true}: primitive | array | object,
{openapi.PathStyleMatrix, false}: primitive | array | object,
{openapi.PathStyleMatrix, true}: primitive | array | object,
},
openapi.LocationQuery: {
{openapi.QueryStyleForm, true}: primitive | array | object,
{openapi.QueryStyleForm, false}: primitive | array | object,
{openapi.QueryStyleSpaceDelimited, true}: array,
{openapi.QueryStyleSpaceDelimited, false}: array,
{openapi.QueryStylePipeDelimited, true}: array,
{openapi.QueryStylePipeDelimited, false}: array,
{openapi.QueryStyleDeepObject, true}: object,
},
openapi.LocationHeader: {
{openapi.HeaderStyleSimple, false}: primitive | array | object,
{openapi.HeaderStyleSimple, true}: primitive | array | object,
},
openapi.LocationCookie: {
{openapi.CookieStyleForm, true}: primitive,
{openapi.CookieStyleForm, false}: primitive | array | object,
},
}
wrap := func(field string, err error) error {
return p.wrapField(field, file, param.Pointer.Locator, err)
}
styles, ok := table[param.In]
if !ok {
return wrap("in", errors.Errorf(`invalid "in": %q`, param.In))
}
types, ok := styles[stexp{param.Style, param.Explode}]
if !ok {
err := errors.Errorf("invalid style explode combination %q, explode:%v", param.Style, param.Explode)
return wrap("style", err)
}
var (
allowed = func(t byte) bool { return types&t != 0 }
check func(s *jsonschema.Schema) error
checkAll func(many []*jsonschema.Schema) error
)
check = func(s *jsonschema.Schema) error {
if s == nil {
return nil
}
locator := s.Pointer.Locator
switch s.Type {
case jsonschema.String, jsonschema.Integer, jsonschema.Number, jsonschema.Boolean:
if allowed(primitive) {
return nil
}
case jsonschema.Array:
if allowed(array) {
return nil
}
case jsonschema.Object:
if allowed(object) {
return nil
}
case jsonschema.Empty:
if err := checkAll(s.OneOf); err != nil {
return p.wrapField("oneOf", file, locator, err)
}
if err := checkAll(s.AnyOf); err != nil {
return p.wrapField("anyOf", file, locator, err)
}
if err := checkAll(s.AllOf); err != nil {
return p.wrapField("allOf", file, locator, err)
}
return nil
}
err := errors.Errorf("invalid schema.type:style:explode combination: (%q:%q:%v)",
s.Type, param.Style, param.Explode)
return p.wrapField("type", file, locator, err)
}
checkAll = func(many []*jsonschema.Schema) error {
if many == nil {
return nil
}
for _, s := range many {
if s == nil {
continue
}
if err := check(s); err != nil {
return p.wrapLocation(file, s.Pointer.Locator, err)
}
}
return nil
}
switch {
case param.Schema != nil:
if err := check(param.Schema); err != nil {
return wrap("schema", err)
}
case param.Content != nil:
if !allowed(primitive) {
err := errors.New("content parameter should be primitive")
return wrap("style", err)
}
}
return nil
}