-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.go
203 lines (170 loc) · 6.22 KB
/
components.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
package openapi
import (
"fmt"
"regexp"
"github.com/MarkRosemaker/errpath"
)
var reKey = regexp.MustCompile(`^[a-zA-Z0-9\.\-_]+$`)
// The Components object holds a set of reusable objects for different aspects of the OAS.
// All objects defined within the components object will have no effect on the API unless they are explicitly referenced from properties outside the components object.
// ([Specification])
//
// All the fixed fields are objects that MUST use keys that match the regular expression:
//
// ^[a-zA-Z0-9\.\-_]+$
//
// Field name examples:
//
// User
// User_1
// User_Name
// user-name
// my.org.User
//
// [Specification]: https://spec.openapis.org/oas/v3.1.0#components-object
type Components struct {
// An object to hold reusable Schema Objects.
Schemas Schemas `json:"schemas,omitempty" yaml:"schemas,omitempty"`
// An object to hold reusable Response Objects.
Responses ResponsesByName `json:"responses,omitempty" yaml:"responses,omitempty"`
// An object to hold reusable Parameter Objects.
Parameters Parameters `json:"parameters,omitempty" yaml:"parameters,omitempty"`
// An object to hold reusable Example Objects.
Examples Examples `json:"examples,omitempty" yaml:"examples,omitempty"`
// An object to hold reusable Request Body Objects.
RequestBodies RequestBodies `json:"requestBodies,omitempty" yaml:"requestBodies,omitempty"`
// An object to hold reusable Header Objects.
Headers Headers `json:"headers,omitempty" yaml:"headers,omitempty"`
// An object to hold reusable Security Scheme Objects.
SecuritySchemes SecuritySchemes `json:"securitySchemes,omitempty" yaml:"securitySchemes,omitempty"`
// An object to hold reusable Link Objects.
Links Links `json:"links,omitempty" yaml:"links,omitempty"`
// An object to hold reusable Callback Objects.
Callbacks CallbackRefs `json:"callbacks,omitempty" yaml:"callbacks,omitempty"`
// An object to hold reusable Path Item Object.
PathItems PathItems `json:"pathItems,omitempty" yaml:"pathItems,omitempty"`
// This object MAY be extended with Specification Extensions.
Extensions Extensions `json:",inline" yaml:",inline"`
}
func validateKey(key string) error {
if reKey.MatchString(key) {
return nil
}
return &errpath.ErrKey{Key: key, Err: &errpath.ErrInvalid[string]{
Value: key,
Message: fmt.Sprintf(`must match the regular expression %q`, reKey),
}}
}
func (c *Components) Validate() error {
if err := c.Schemas.Validate(); err != nil {
return &errpath.ErrField{Field: "schemas", Err: err}
}
// validate the key: check if it is a valid key
for name := range c.Responses.ByIndex() {
if err := validateKey(name); err != nil {
return &errpath.ErrField{Field: "responses", Err: err}
}
}
if err := c.Responses.Validate(); err != nil {
return &errpath.ErrField{Field: "responses", Err: err}
}
if err := c.Parameters.Validate(); err != nil {
return &errpath.ErrField{Field: "parameters", Err: err}
}
if err := c.Examples.Validate(); err != nil {
return &errpath.ErrField{Field: "examples", Err: err}
}
if err := c.RequestBodies.Validate(); err != nil {
return &errpath.ErrField{Field: "requestBodies", Err: err}
}
if err := c.Headers.Validate(); err != nil {
return &errpath.ErrField{Field: "headers", Err: err}
}
if err := c.SecuritySchemes.Validate(); err != nil {
return &errpath.ErrField{Field: "securitySchemes", Err: err}
}
if err := c.Links.Validate(); err != nil {
return &errpath.ErrField{Field: "links", Err: err}
}
if err := c.Callbacks.Validate(); err != nil {
return &errpath.ErrField{Field: "callbacks", Err: err}
}
if err := c.PathItems.Validate(); err != nil {
return &errpath.ErrField{Field: "pathItems", Err: err}
}
if err := validateExtensions(c.Extensions); err != nil {
return err
}
return nil
}
// For each field that is a map, sorts the map by key.
func (c *Components) SortMaps() {
c.Schemas.Sort()
c.Responses.Sort()
c.Parameters.Sort()
c.Examples.Sort()
c.RequestBodies.Sort()
c.Headers.Sort()
c.SecuritySchemes.Sort()
c.Links.Sort()
c.Callbacks.Sort()
c.PathItems.Sort()
}
func (c Components) isEmpty() bool {
return len(c.Schemas) == 0 &&
len(c.Responses) == 0 &&
len(c.Parameters) == 0 &&
len(c.Examples) == 0 &&
len(c.RequestBodies) == 0 &&
len(c.Headers) == 0 &&
len(c.SecuritySchemes) == 0 &&
len(c.Links) == 0 &&
len(c.Callbacks) == 0 &&
len(c.PathItems) == 0 &&
len(c.Extensions) == 0
}
func (l *loader) collectComponents(cs Components, ref ref) {
l.collectSchemas(cs.Schemas, append(ref, "schemas"))
l.collectResponses(cs.Responses, append(ref, "responses"))
l.collectParameters(cs.Parameters, append(ref, "parameters"))
l.collectExamples(cs.Examples, append(ref, "examples"))
l.collectRequestBodies(cs.RequestBodies, append(ref, "requestBodies"))
l.collectHeaders(cs.Headers, append(ref, "headers"))
l.collectSecuritySchemes(cs.SecuritySchemes, append(ref, "securitySchemes"))
l.collectLinks(cs.Links, append(ref, "links"))
l.collectCallbackRefs(cs.Callbacks, append(ref, "callbacks"))
l.collectPathItems(cs.PathItems, append(ref, "pathItems"))
}
func (l *loader) resolveComponents(c Components) error {
if err := l.resolveSchemas(c.Schemas); err != nil {
return &errpath.ErrField{Field: "schemas", Err: err}
}
if err := l.resolveResponses(c.Responses); err != nil {
return &errpath.ErrField{Field: "responses", Err: err}
}
if err := l.resolveParameters(c.Parameters); err != nil {
return &errpath.ErrField{Field: "parameters", Err: err}
}
if err := l.resolveExamples(c.Examples); err != nil {
return &errpath.ErrField{Field: "examples", Err: err}
}
if err := l.resolveRequestBodies(c.RequestBodies); err != nil {
return &errpath.ErrField{Field: "requestBodies", Err: err}
}
if err := l.resolveHeaders(c.Headers); err != nil {
return &errpath.ErrField{Field: "headers", Err: err}
}
if err := l.resolveSecuritySchemes(c.SecuritySchemes); err != nil {
return &errpath.ErrField{Field: "securitySchemes", Err: err}
}
if err := l.resolveLinks(c.Links); err != nil {
return &errpath.ErrField{Field: "links", Err: err}
}
if err := l.resolveCallbackRefs(c.Callbacks); err != nil {
return &errpath.ErrField{Field: "callbacks", Err: err}
}
if err := l.resolvePathItems(c.PathItems); err != nil {
return &errpath.ErrField{Field: "pathItems", Err: err}
}
return nil
}