forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openapi_v2_builder.go
840 lines (782 loc) · 20.9 KB
/
openapi_v2_builder.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
package openapi
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"goa.design/goa/codegen"
"goa.design/goa/expr"
)
// NewV2 returns the OpenAPI v2 specification for the given API.
func NewV2(root *expr.RootExpr, h *expr.HostExpr) (*V2, error) {
if root == nil {
return nil, nil
}
tags := tagsFromExpr(root.Meta)
u, err := url.Parse(string(h.URIs[0]))
if err != nil {
// This should never happen because server expression must have been
// validated. If it does, then we must fix server validation.
return nil, fmt.Errorf("failed to parse server URL: %s", err)
}
host := u.Host
basePath := root.API.HTTP.Path
if hasAbsoluteRoutes(root) {
basePath = ""
}
params := paramsFromExpr(root.API.HTTP.Params, basePath)
var paramMap map[string]*Parameter
if len(params) > 0 {
paramMap = make(map[string]*Parameter, len(params))
for _, p := range params {
paramMap[p.Name] = p
}
}
s := &V2{
Swagger: "2.0",
Info: &Info{
Title: root.API.Title,
Description: root.API.Description,
TermsOfService: root.API.TermsOfService,
Contact: root.API.Contact,
License: root.API.License,
Version: root.API.Version,
Extensions: ExtensionsFromExpr(root.Meta),
},
Host: host,
BasePath: basePath,
Paths: make(map[string]interface{}),
Consumes: root.API.HTTP.Consumes,
Produces: root.API.HTTP.Produces,
Parameters: paramMap,
Tags: tags,
SecurityDefinitions: securitySpecFromExpr(root),
ExternalDocs: docsFromExpr(root.API.Docs),
}
for _, he := range root.API.HTTP.Errors {
res := responseSpecFromExpr(s, root, he.Response, "")
if s.Responses == nil {
s.Responses = make(map[string]*Response)
}
s.Responses[he.Name] = res
}
for _, res := range root.API.HTTP.Services {
if !mustGenerate(res.Meta) || !mustGenerate(res.ServiceExpr.Meta) {
continue
}
for k, v := range ExtensionsFromExpr(res.Meta) {
s.Paths[k] = v
}
for _, fs := range res.FileServers {
if !mustGenerate(fs.Meta) || !mustGenerate(fs.Service.Meta) {
continue
}
buildPathFromFileServer(s, root, fs)
}
for _, a := range res.HTTPEndpoints {
if !mustGenerate(a.Meta) || !mustGenerate(a.MethodExpr.Meta) {
continue
}
for _, route := range a.Routes {
buildPathFromExpr(s, root, h, route, basePath)
}
}
}
if len(Definitions) > 0 {
s.Definitions = make(map[string]*Schema)
for n, d := range Definitions {
// sad but swagger doesn't support these
d.Media = nil
d.Links = nil
s.Definitions[n] = d
}
}
return s, nil
}
// ExtensionsFromExpr generates swagger extensions from the given meta
// expression.
func ExtensionsFromExpr(mdata expr.MetaExpr) map[string]interface{} {
extensions := make(map[string]interface{})
for key, value := range mdata {
chunks := strings.Split(key, ":")
if len(chunks) != 3 {
continue
}
if chunks[0] != "swagger" || chunks[1] != "extension" {
continue
}
if !strings.HasPrefix(chunks[2], "x-") {
continue
}
val := value[0]
ival := interface{}(val)
if err := json.Unmarshal([]byte(val), &ival); err != nil {
extensions[chunks[2]] = val
continue
}
extensions[chunks[2]] = ival
}
if len(extensions) == 0 {
return nil
}
return extensions
}
// mustGenerate returns true if the meta indicates that a OpenAPI specification should be
// generated, false otherwise.
func mustGenerate(meta expr.MetaExpr) bool {
if m, ok := meta["swagger:generate"]; ok {
if len(m) > 0 && m[0] == "false" {
return false
}
}
return true
}
// securitySpecFromExpr generates the OpenAPI security definitions from the
// security design.
func securitySpecFromExpr(root *expr.RootExpr) map[string]*SecurityDefinition {
sds := make(map[string]*SecurityDefinition)
for _, svc := range root.API.HTTP.Services {
for _, e := range svc.HTTPEndpoints {
for _, req := range e.Requirements {
for _, s := range req.Schemes {
sd := SecurityDefinition{
Description: s.Description,
Extensions: ExtensionsFromExpr(s.Meta),
}
switch s.Kind {
case expr.BasicAuthKind:
sd.Type = "basic"
case expr.APIKeyKind:
sd.Type = "apiKey"
sd.In = s.In
sd.Name = s.Name
case expr.JWTKind:
sd.Type = "apiKey"
// OpenAPI V2 spec does not support JWT scheme. Hence we add the scheme
// information to the description.
lines := []string{}
for _, scope := range s.Scopes {
lines = append(lines, fmt.Sprintf(" * `%s`: %s", scope.Name, scope.Description))
}
sd.In = s.In
sd.Name = s.Name
sd.Description += fmt.Sprintf("\n**Security Scopes**:\n%s", strings.Join(lines, "\n"))
case expr.OAuth2Kind:
sd.Type = "oauth2"
if scopesLen := len(s.Scopes); scopesLen > 0 {
scopes := make(map[string]string, scopesLen)
for _, scope := range s.Scopes {
scopes[scope.Name] = scope.Description
}
sd.Scopes = scopes
}
}
if len(s.Flows) > 0 {
switch s.Flows[0].Kind {
case expr.AuthorizationCodeFlowKind:
sd.Flow = "accessCode"
case expr.ImplicitFlowKind:
sd.Flow = "implicit"
case expr.PasswordFlowKind:
sd.Flow = "password"
case expr.ClientCredentialsFlowKind:
sd.Flow = "application"
}
sd.AuthorizationURL = s.Flows[0].AuthorizationURL
sd.TokenURL = s.Flows[0].TokenURL
}
sds[s.Hash()] = &sd
}
}
}
}
return sds
}
// hasAbsoluteRoutes returns true if any endpoint exposed by the API uses an
// absolute route of if the API has file servers. This is needed as OpenAPI does
// not support exceptions to the base path so if the API has any absolute route
// the base path must be "/" and all routes must be absolutes.
func hasAbsoluteRoutes(root *expr.RootExpr) bool {
hasAbsoluteRoutes := false
for _, res := range root.API.HTTP.Services {
if !mustGenerate(res.Meta) || !mustGenerate(res.ServiceExpr.Meta) {
continue
}
for _, fs := range res.FileServers {
if !mustGenerate(fs.Meta) || !mustGenerate(fs.Service.Meta) {
continue
}
hasAbsoluteRoutes = true
break
}
for _, a := range res.HTTPEndpoints {
if !mustGenerate(a.Meta) || !mustGenerate(a.MethodExpr.Meta) {
continue
}
for _, ro := range a.Routes {
if ro.IsAbsolute() {
hasAbsoluteRoutes = true
break
}
}
if hasAbsoluteRoutes {
break
}
}
if hasAbsoluteRoutes {
break
}
}
return hasAbsoluteRoutes
}
func tagsFromExpr(mdata expr.MetaExpr) (tags []*Tag) {
var keys []string
for k := range mdata {
keys = append(keys, k)
}
sort.Strings(keys)
for _, key := range keys {
chunks := strings.Split(key, ":")
if len(chunks) != 3 {
continue
}
if chunks[0] != "swagger" || chunks[1] != "tag" {
continue
}
tag := &Tag{Name: chunks[2]}
mdata[key] = mdata[fmt.Sprintf("%s:desc", key)]
if len(mdata[key]) != 0 {
tag.Description = mdata[key][0]
}
hasDocs := false
docs := &ExternalDocs{}
mdata[key] = mdata[fmt.Sprintf("%s:url", key)]
if len(mdata[key]) != 0 {
docs.URL = mdata[key][0]
hasDocs = true
}
mdata[key] = mdata[fmt.Sprintf("%s:url:desc", key)]
if len(mdata[key]) != 0 {
docs.Description = mdata[key][0]
hasDocs = true
}
if hasDocs {
tag.ExternalDocs = docs
}
tag.Extensions = ExtensionsFromExpr(mdata)
tags = append(tags, tag)
}
return
}
func tagNamesFromExpr(mdatas ...expr.MetaExpr) (tagNames []string) {
for _, mdata := range mdatas {
tags := tagsFromExpr(mdata)
for _, tag := range tags {
tagNames = append(tagNames, tag.Name)
}
}
return
}
func summaryFromExpr(name string, e *expr.HTTPEndpointExpr) string {
for n, mdata := range e.Meta {
if n == "swagger:summary" && len(mdata) > 0 {
return mdata[0]
}
}
for n, mdata := range e.MethodExpr.Meta {
if n == "swagger:summary" && len(mdata) > 0 {
return mdata[0]
}
}
return name
}
func summaryFromMeta(name string, meta expr.MetaExpr) string {
for n, mdata := range meta {
if n == "swagger:summary" && len(mdata) > 0 {
return mdata[0]
}
}
return name
}
func paramsFromExpr(params *expr.MappedAttributeExpr, path string) []*Parameter {
if params == nil {
return nil
}
var (
res []*Parameter
wildcards = expr.ExtractHTTPWildcards(path)
i = 0
)
codegen.WalkMappedAttr(params, func(n, pn string, required bool, at *expr.AttributeExpr) error {
in := "query"
for _, w := range wildcards {
if n == w {
in = "path"
required = true
break
}
}
param := paramFor(at, pn, in, required)
res = append(res, param)
i++
return nil
})
return res
}
func paramsFromHeaders(endpoint *expr.HTTPEndpointExpr) []*Parameter {
params := []*Parameter{}
var (
rma = endpoint.Service.Params
ma = endpoint.Headers
merged *expr.MappedAttributeExpr
)
{
if rma == nil {
merged = ma
} else if ma == nil {
merged = rma
} else {
merged = expr.DupMappedAtt(rma)
merged.Merge(ma)
}
}
for _, n := range *expr.AsObject(merged.Type) {
header := n.Attribute
required := merged.IsRequiredNoDefault(n.Name)
p := paramFor(header, merged.ElemName(n.Name), "header", required)
params = append(params, p)
}
// Add basic auth to headers
if att := expr.TaggedAttribute(endpoint.MethodExpr.Payload, "security:username"); att != "" {
// Basic Auth is always encoded in the Authorization header
// https://golang.org/pkg/net/http/#Request.SetBasicAuth
params = append(params, &Parameter{
In: "header",
Name: "Authorization",
Required: endpoint.MethodExpr.Payload.IsRequired(att),
Description: "Basic Auth security using Basic scheme (https://tools.ietf.org/html/rfc7617)",
Type: "string",
})
}
return params
}
func paramFor(at *expr.AttributeExpr, name, in string, required bool) *Parameter {
p := &Parameter{
In: in,
Name: name,
Default: toStringMap(at.DefaultValue),
Description: at.Description,
Required: required,
Type: at.Type.Name(),
}
if expr.IsArray(at.Type) {
p.Items = itemsFromExpr(expr.AsArray(at.Type).ElemType)
p.CollectionFormat = "multi"
}
switch at.Type {
case expr.Int, expr.UInt, expr.UInt32, expr.UInt64:
p.Type = "integer"
case expr.Int32, expr.Int64:
p.Type = "integer"
p.Format = at.Type.Name()
case expr.Float32:
p.Type = "number"
p.Format = "float"
case expr.Float64:
p.Type = "number"
p.Format = "double"
case expr.Bytes:
p.Type = "string"
p.Format = "byte"
}
p.Extensions = ExtensionsFromExpr(at.Meta)
initValidations(at, p)
return p
}
func itemsFromExpr(at *expr.AttributeExpr) *Items {
items := &Items{Type: at.Type.Name()}
initValidations(at, items)
if expr.IsArray(at.Type) {
items.Items = itemsFromExpr(expr.AsArray(at.Type).ElemType)
}
return items
}
func responseSpecFromExpr(s *V2, root *expr.RootExpr, r *expr.HTTPResponseExpr, typeNamePrefix string) *Response {
var schema *Schema
if mt, ok := r.Body.Type.(*expr.ResultTypeExpr); ok {
view := expr.DefaultView
if v, ok := r.Body.Meta["view"]; ok {
view = v[0]
}
schema = NewSchema()
schema.Ref = ResultTypeRefWithPrefix(root.API, mt, view, typeNamePrefix)
} else if r.Body.Type != expr.Empty {
schema = AttributeTypeSchemaWithPrefix(root.API, r.Body, typeNamePrefix)
}
headers := headersFromExpr(r.Headers)
desc := r.Description
if desc == "" {
desc = fmt.Sprintf("%s response.", http.StatusText(r.StatusCode))
}
return &Response{
Description: desc,
Schema: schema,
Headers: headers,
Extensions: ExtensionsFromExpr(r.Meta),
}
}
func headersFromExpr(headers *expr.MappedAttributeExpr) map[string]*Header {
if headers == nil {
return nil
}
res := make(map[string]*Header)
codegen.WalkMappedAttr(headers, func(_, n string, required bool, at *expr.AttributeExpr) error {
header := &Header{
Default: at.DefaultValue,
Description: at.Description,
Type: at.Type.Name(),
}
initValidations(at, header)
res[n] = header
return nil
})
if len(res) == 0 {
return nil
}
return res
}
func buildPathFromFileServer(s *V2, root *expr.RootExpr, fs *expr.HTTPFileServerExpr) {
for _, path := range fs.RequestPaths {
wcs := expr.ExtractHTTPWildcards(path)
var param []*Parameter
if len(wcs) > 0 {
param = []*Parameter{{
In: "path",
Name: wcs[0],
Description: "Relative file path",
Required: true,
Type: "string",
}}
}
responses := map[string]*Response{
"200": {
Description: "File downloaded",
Schema: &Schema{Type: File},
},
}
if len(wcs) > 0 {
schema := TypeSchema(root.API, expr.ErrorResult)
responses["404"] = &Response{Description: "File not found", Schema: schema}
}
operationID := fmt.Sprintf("%s#%s", fs.Service.Name(), path)
schemes := root.API.Schemes()
// remove grpc and grpcs from schemes since it is not a valid scheme in
// openapi.
for i := len(schemes) - 1; i >= 0; i-- {
if schemes[i] == "grpc" || schemes[i] == "grpcs" {
schemes = append(schemes[:i], schemes[i+1:]...)
}
}
operation := &Operation{
Description: fs.Description,
Summary: summaryFromMeta(fmt.Sprintf("Download %s", fs.FilePath), fs.Meta),
ExternalDocs: docsFromExpr(fs.Docs),
OperationID: operationID,
Parameters: param,
Responses: responses,
Schemes: schemes,
}
key := expr.HTTPWildcardRegex.ReplaceAllStringFunc(
path,
func(w string) string {
return fmt.Sprintf("/{%s}", w[2:])
},
)
if key == "" {
key = "/"
}
var path interface{}
var ok bool
if path, ok = s.Paths[key]; !ok {
path = new(Path)
s.Paths[key] = path
}
p := path.(*Path)
p.Get = operation
p.Extensions = ExtensionsFromExpr(fs.Meta)
}
}
func buildPathFromExpr(s *V2, root *expr.RootExpr, h *expr.HostExpr, route *expr.RouteExpr, basePath string) {
endpoint := route.Endpoint
tagNames := tagNamesFromExpr(endpoint.Service.Meta, endpoint.Meta)
if len(tagNames) == 0 {
// By default tag with service name
tagNames = []string{route.Endpoint.Service.Name()}
}
for _, key := range route.FullPaths() {
params := paramsFromExpr(endpoint.Params, key)
params = append(params, paramsFromHeaders(endpoint)...)
produces := []string{}
responses := make(map[string]*Response, len(endpoint.Responses))
for _, r := range endpoint.Responses {
if endpoint.MethodExpr.IsStreaming() {
// A streaming endpoint allows at most one successful response
// definition. So it is okay to change the first successful
// response to a HTTP 101 response for openapi docs.
if _, ok := responses[strconv.Itoa(expr.StatusSwitchingProtocols)]; !ok {
r = r.Dup()
r.StatusCode = expr.StatusSwitchingProtocols
}
}
resp := responseSpecFromExpr(s, root, r, endpoint.Service.Name())
responses[strconv.Itoa(r.StatusCode)] = resp
if r.ContentType != "" {
foundCT := false
for _, ct := range produces {
if ct == r.ContentType {
foundCT = true
break
}
}
if !foundCT {
produces = append(produces, r.ContentType)
}
}
}
for _, er := range endpoint.HTTPErrors {
resp := responseSpecFromExpr(s, root, er.Response, endpoint.Service.Name())
responses[strconv.Itoa(er.Response.StatusCode)] = resp
}
if endpoint.Body.Type != expr.Empty {
pp := &Parameter{
Name: endpoint.Body.Type.Name(),
In: "body",
Description: endpoint.Body.Description,
Required: true,
Schema: AttributeTypeSchemaWithPrefix(root.API, endpoint.Body, codegen.Goify(endpoint.Service.Name(), true)),
}
params = append(params, pp)
}
operationID := fmt.Sprintf("%s#%s", endpoint.Service.Name(), endpoint.Name())
index := 0
for i, rt := range endpoint.Routes {
if rt == route {
index = i
break
}
}
if index > 0 {
operationID = fmt.Sprintf("%s#%d", operationID, index)
}
schemes := h.Schemes()
// remove grpc and grpcs from schemes since it is not a valid scheme in
// openapi.
for i := len(schemes) - 1; i >= 0; i-- {
if schemes[i] == "grpc" || schemes[i] == "grpcs" {
schemes = append(schemes[:i], schemes[i+1:]...)
}
}
// replace http with ws for streaming endpoints
if endpoint.MethodExpr.IsStreaming() {
for i := len(schemes) - 1; i >= 0; i-- {
if schemes[i] == "http" {
news := append([]string{"ws"}, schemes[i+1:]...)
schemes = append(schemes[:i], news...)
}
if schemes[i] == "https" {
news := append([]string{"wss"}, schemes[i+1:]...)
schemes = append(schemes[:i], news...)
}
}
}
description := endpoint.Description()
requirements := make([]map[string][]string, len(endpoint.Requirements))
for i, req := range endpoint.Requirements {
requirement := make(map[string][]string)
for _, s := range req.Schemes {
requirement[s.Hash()] = []string{}
switch s.Kind {
case expr.OAuth2Kind:
requirement[s.Hash()] = append(requirement[s.Hash()], req.Scopes...)
case expr.JWTKind:
lines := make([]string, 0, len(req.Scopes))
for _, scope := range req.Scopes {
lines = append(lines, fmt.Sprintf(" * `%s`", scope))
}
if description != "" {
description += "\n"
}
description += fmt.Sprintf("\nRequired security scopes:\n%s", strings.Join(lines, "\n"))
}
}
requirements[i] = requirement
}
operation := &Operation{
Tags: tagNames,
Description: description,
Summary: summaryFromExpr(endpoint.Name()+" "+endpoint.Service.Name(), endpoint),
ExternalDocs: docsFromExpr(endpoint.MethodExpr.Docs),
OperationID: operationID,
Parameters: params,
Produces: produces,
Responses: responses,
Schemes: schemes,
Deprecated: false,
Extensions: ExtensionsFromExpr(route.Meta),
Security: requirements,
}
if key == "" {
key = "/"
}
bp := expr.HTTPWildcardRegex.ReplaceAllStringFunc(
basePath,
func(w string) string {
return fmt.Sprintf("/{%s}", w[2:])
},
)
if bp != "/" {
key = strings.TrimPrefix(key, bp)
}
var path interface{}
var ok bool
if path, ok = s.Paths[key]; !ok {
path = new(Path)
s.Paths[key] = path
}
p := path.(*Path)
switch route.Method {
case "GET":
p.Get = operation
case "PUT":
p.Put = operation
case "POST":
p.Post = operation
case "DELETE":
p.Delete = operation
case "OPTIONS":
p.Options = operation
case "HEAD":
p.Head = operation
case "PATCH":
p.Patch = operation
}
p.Extensions = ExtensionsFromExpr(route.Endpoint.Meta)
}
}
func docsFromExpr(docs *expr.DocsExpr) *ExternalDocs {
if docs == nil {
return nil
}
return &ExternalDocs{
Description: docs.Description,
URL: docs.URL,
}
}
func initEnumValidation(def interface{}, values []interface{}) {
switch actual := def.(type) {
case *Parameter:
actual.Enum = values
case *Header:
actual.Enum = values
case *Items:
actual.Enum = values
}
}
func initFormatValidation(def interface{}, format string) {
switch actual := def.(type) {
case *Parameter:
actual.Format = format
case *Header:
actual.Format = format
case *Items:
actual.Format = format
}
}
func initPatternValidation(def interface{}, pattern string) {
switch actual := def.(type) {
case *Parameter:
actual.Pattern = pattern
case *Header:
actual.Pattern = pattern
case *Items:
actual.Pattern = pattern
}
}
func initMinimumValidation(def interface{}, min *float64) {
switch actual := def.(type) {
case *Parameter:
actual.Minimum = min
actual.ExclusiveMinimum = false
case *Header:
actual.Minimum = min
actual.ExclusiveMinimum = false
case *Items:
actual.Minimum = min
actual.ExclusiveMinimum = false
}
}
func initMaximumValidation(def interface{}, max *float64) {
switch actual := def.(type) {
case *Parameter:
actual.Maximum = max
actual.ExclusiveMaximum = false
case *Header:
actual.Maximum = max
actual.ExclusiveMaximum = false
case *Items:
actual.Maximum = max
actual.ExclusiveMaximum = false
}
}
func initMinLengthValidation(def interface{}, isArray bool, min *int) {
switch actual := def.(type) {
case *Parameter:
if isArray {
actual.MinItems = min
} else {
actual.MinLength = min
}
case *Header:
actual.MinLength = min
case *Items:
actual.MinLength = min
}
}
func initMaxLengthValidation(def interface{}, isArray bool, max *int) {
switch actual := def.(type) {
case *Parameter:
if isArray {
actual.MaxItems = max
} else {
actual.MaxLength = max
}
case *Header:
actual.MaxLength = max
case *Items:
actual.MaxLength = max
}
}
func initValidations(attr *expr.AttributeExpr, def interface{}) {
val := attr.Validation
if val == nil {
return
}
initEnumValidation(def, val.Values)
initFormatValidation(def, string(val.Format))
initPatternValidation(def, val.Pattern)
if val.Minimum != nil {
initMinimumValidation(def, val.Minimum)
}
if val.Maximum != nil {
initMaximumValidation(def, val.Maximum)
}
if val.MinLength != nil {
initMinLengthValidation(def, expr.IsArray(attr.Type), val.MinLength)
}
if val.MaxLength != nil {
initMaxLengthValidation(def, expr.IsArray(attr.Type), val.MaxLength)
}
}