-
Notifications
You must be signed in to change notification settings - Fork 2
/
components.go
297 lines (276 loc) · 7.98 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
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
package openapi
import (
"encoding/json"
"github.com/chanced/transcode"
"gopkg.in/yaml.v3"
)
// ComponentMap is a pseudo map consisting of Components with type T.
func newComponent[T refable](ref *Reference[T], obj T) Component[T] {
return Component[T]{
Reference: ref,
Object: obj,
}
}
// Components 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.
type Components struct {
// OpenAPI extensions
Extensions `json:"-"`
Location `json:"-"`
Schemas *SchemaMap `json:"schemas,omitempty"`
Responses *ResponseMap `json:"responses,omitempty"`
Parameters *ParameterMap `json:"parameters,omitempty"`
RequestBodies *RequestBodyMap `json:"requestBodies,omitempty"`
Headers *HeaderMap `json:"headers,omitempty"`
SecuritySchemes *SecuritySchemeMap `json:"securitySchemes,omitempty"`
Links *LinkMap `json:"links,omitempty"`
Callbacks *CallbacksMap `json:"callbacks,omitempty"`
PathItems *PathItemMap `json:"pathItems,omitempty"`
Examples *ExampleMap `json:"examples,omitempty"` //
}
func (*Components) Kind() Kind { return KindComponents }
func (c *Components) Refs() []Ref {
if c == nil {
return nil
}
var refs []Ref
if c.Schemas != nil {
refs = append(refs, c.Schemas.Refs()...)
}
if c.Responses != nil {
refs = append(refs, c.Responses.Refs()...)
}
if c.Parameters != nil {
refs = append(refs, c.Parameters.Refs()...)
}
if c.Examples != nil {
refs = append(refs, c.Examples.Refs()...)
}
if c.RequestBodies != nil {
refs = append(refs, c.RequestBodies.Refs()...)
}
if c.Headers != nil {
refs = append(refs, c.Headers.Refs()...)
}
if c.SecuritySchemes != nil {
refs = append(refs, c.SecuritySchemes.Refs()...)
}
if c.Links != nil {
refs = append(refs, c.Links.Refs()...)
}
if c.Callbacks != nil {
refs = append(refs, c.Callbacks.Refs()...)
}
if c.PathItems != nil {
refs = append(refs, c.PathItems.Refs()...)
}
return refs
}
// func (c *Components) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// if c == nil {
// return nil, nil
// }
// return c.resolveNodeByPointer(ptr)
// }
// func (c *Components) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return c, nil
// }
// nxt, tok, _ := ptr.Next()
// switch tok {
// case "schemas":
// if c.Schemas == nil {
// return nil, newErrNotFound(c.AbsoluteLocation(), tok)
// }
// return c.Schemas.resolveNodeByPointer(nxt)
// case "responses":
// if c.Responses == nil {
// return nil, newErrNotFound(c.AbsoluteLocation(), tok)
// }
// return c.Responses.resolveNodeByPointer(nxt)
// case "parameters":
// if c.Parameters == nil {
// return nil, newErrNotFound(c.AbsoluteLocation(), tok)
// }
// return c.Parameters.resolveNodeByPointer(nxt)
// case "examples":
// if c.Examples == nil {
// return nil, newErrNotFound(c.AbsoluteLocation(), tok)
// }
// return c.Examples.resolveNodeByPointer(nxt)
// case "requestBodies":
// if c.RequestBodies == nil {
// return nil, newErrNotFound(c.AbsoluteLocation(), tok)
// }
// return c.RequestBodies.resolveNodeByPointer(nxt)
// case "headers":
// if c.Headers == nil {
// return nil, newErrNotFound(c.AbsoluteLocation(), tok)
// }
// return c.Headers.resolveNodeByPointer(nxt)
// case "securitySchemes":
// if c.SecuritySchemes == nil {
// return nil, newErrNotFound(c.AbsoluteLocation(), tok)
// }
// return c.SecuritySchemes.resolveNodeByPointer(nxt)
// case "links":
// if c.Links == nil {
// return nil, newErrNotFound(c.AbsoluteLocation(), tok)
// }
// return c.Links.resolveNodeByPointer(nxt)
// case "callbacks":
// if c.Callbacks == nil {
// return nil, newErrNotFound(c.AbsoluteLocation(), tok)
// }
// return c.Callbacks.resolveNodeByPointer(nxt)
// case "pathItems":
// if c.PathItems == nil {
// return nil, newErrNotFound(c.AbsoluteLocation(), tok)
// }
// return c.PathItems.resolveNodeByPointer(nxt)
// default:
// return nil, newErrNotResolvable(c.AbsoluteLocation(), tok)
// }
// }
func (c *Components) nodes() []node {
if c == nil {
return nil
}
edges := appendEdges(nil, c.Schemas)
edges = appendEdges(edges, c.Responses)
edges = appendEdges(edges, c.Parameters)
edges = appendEdges(edges, c.Examples)
edges = appendEdges(edges, c.RequestBodies)
edges = appendEdges(edges, c.Headers)
edges = appendEdges(edges, c.SecuritySchemes)
edges = appendEdges(edges, c.Links)
edges = appendEdges(edges, c.Callbacks)
edges = appendEdges(edges, c.PathItems)
return edges
}
func (c *Components) isNil() bool {
return c == nil
}
func (*Components) mapKind() Kind { return KindUndefined }
func (*Components) sliceKind() Kind { return KindUndefined }
func (c *Components) Anchors() (*Anchors, error) {
if c == nil {
return nil, nil
}
var err error
var anchors *Anchors
if anchors, err = anchors.merge(c.Schemas.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(c.Responses.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(c.Parameters.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(c.Examples.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(c.RequestBodies.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(c.Headers.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(c.SecuritySchemes.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(c.Links.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(c.Callbacks.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(c.PathItems.Anchors()); err != nil {
return nil, err
}
return anchors, nil
}
func (c *Components) setLocation(loc Location) error {
if c == nil {
return nil
}
c.Location = loc
var err error
if err = c.Schemas.setLocation(loc.AppendLocation("schemas")); err != nil {
return err
}
if err = c.Responses.setLocation(loc.AppendLocation("responses")); err != nil {
return err
}
if err = c.Parameters.setLocation(loc.AppendLocation("parameters")); err != nil {
return err
}
if err = c.Examples.setLocation(loc.AppendLocation("examples")); err != nil {
return err
}
if err = c.RequestBodies.setLocation(loc.AppendLocation("requestBodies")); err != nil {
return err
}
if err = c.Headers.setLocation(loc.AppendLocation("headers")); err != nil {
return err
}
if err = c.SecuritySchemes.setLocation(loc.AppendLocation("securitySchemes")); err != nil {
return err
}
if err = c.Links.setLocation(loc.AppendLocation("links")); err != nil {
return err
}
if err = c.Callbacks.setLocation(loc.AppendLocation("callbacks")); err != nil {
return err
}
if err = c.PathItems.setLocation(loc.AppendLocation("pathItems")); err != nil {
return err
}
return nil
}
// MarshalJSON marshals JSON
func (c Components) MarshalJSON() ([]byte, error) {
type components Components
return marshalExtendedJSON(components(c))
}
// UnmarshalJSON unmarshals JSON
func (c *Components) UnmarshalJSON(data []byte) error {
type components Components
var v components
if err := unmarshalExtendedJSON(data, &v); err != nil {
return err
}
*c = Components(v)
return nil
}
func (c Components) MarshalYAML() (interface{}, error) {
j, err := c.MarshalJSON()
if err != nil {
return nil, err
}
var v interface{}
err = json.Unmarshal(j, &v)
if err != nil {
return nil, err
}
return v, nil
}
// UnmarshalYAML implements yaml.Unmarshaler
func (c *Components) UnmarshalYAML(value *yaml.Node) error {
v, err := yaml.Marshal(value)
if err != nil {
return err
}
j, err := transcode.JSONFromYAML(v)
if err != nil {
return err
}
return json.Unmarshal(j, c)
}
var _ node = (*Components)(nil)