-
Notifications
You must be signed in to change notification settings - Fork 4
/
path_item.go
123 lines (113 loc) · 2.95 KB
/
path_item.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
package openapi
import (
"net/http"
"strings"
)
// codebeat:disable[TOO_MANY_IVARS]
// PathItem Object
type PathItem struct {
Ref string `yaml:"$ref"`
Summary string
Description string
Get *Operation
Put *Operation
Post *Operation
Delete *Operation
Options *Operation
Head *Operation
Patch *Operation
Trace *Operation
Servers []*Server
Parameters []*Parameter
}
var methods = []string{
http.MethodGet,
http.MethodPut,
http.MethodPost,
http.MethodDelete,
http.MethodOptions,
http.MethodHead,
http.MethodPatch,
http.MethodTrace,
}
// GetOperationByMethod returns a operation object associated with given method.
// The method is case-insensitive, converted to upper case in this function.
// If the method is invalid, this function will return nil.
func (pathItem *PathItem) GetOperationByMethod(method string) *Operation {
switch strings.ToUpper(method) {
case http.MethodGet:
return pathItem.Get
case http.MethodPost:
return pathItem.Post
case http.MethodPut:
return pathItem.Put
case http.MethodDelete:
return pathItem.Delete
case http.MethodOptions:
return pathItem.Options
case http.MethodHead:
return pathItem.Head
case http.MethodPatch:
return pathItem.Patch
case http.MethodTrace:
return pathItem.Trace
default:
return nil
}
}
// GetOperationByID returns an operation object which matches given operationId.
// If the pathItem object has duplicated operationId, this function returns one
// which match first.
func (pathItem PathItem) GetOperationByID(operationID string) *Operation {
for _, method := range methods {
if op := pathItem.GetOperationByMethod(method); op != nil {
if op.OperationID == operationID {
return op
}
}
}
return nil
}
// Operations returns a map containing operation object as a
// value associated with a HTTP method as a key.
// If an operation is nil, it won't be added returned map, so
// the size of returned map is not same always.
func (pathItem PathItem) Operations() map[string]*Operation {
ops := map[string]*Operation{}
for _, method := range methods {
if op := pathItem.GetOperationByMethod(method); op != nil {
ops[method] = op
}
}
return ops
}
// Validate the values of PathItem object.
func (pathItem PathItem) Validate() error {
validaters := []validater{}
for _, op := range pathItem.Operations() {
validaters = append(validaters, op)
}
for _, s := range pathItem.Servers {
validaters = append(validaters, s)
}
if hasDuplicatedParameter(pathItem.Parameters) {
return ErrParameterDuplicated
}
for _, p := range pathItem.Parameters {
validaters = append(validaters, p)
}
return validateAll(validaters)
}
func hasDuplicatedParameter(parameters []*Parameter) bool {
for i, p := range parameters {
for _, q := range parameters[i+1:] {
if q.Name == "" && q.Ref != "" {
continue // need to resolve and validate
}
if p.Name == q.Name && p.In == q.In {
return true
}
}
}
return false
}