-
Notifications
You must be signed in to change notification settings - Fork 2
/
document.go
301 lines (271 loc) · 8.56 KB
/
document.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
package openapi
import (
"encoding/json"
"fmt"
"github.com/Masterminds/semver"
"github.com/chanced/transcode"
"github.com/chanced/uri"
"gopkg.in/yaml.v3"
)
// Document root object of the Document document.
type Document struct {
Location `json:"-"`
Extensions `json:"-"`
// OpenAPI - The OpenAPI Version
//
// This string MUST be the version number of the OpenAPI
// Specification that the OpenAPI document uses. The openapi field SHOULD be
// used by tooling to interpret the OpenAPI document. This is not related to
// the API info.version string.
//
// *required*
OpenAPI *semver.Version `json:"openapi"`
// Provides metadata about the API. The metadata MAY be used by
// tooling as required.
//
// *required*
Info *Info `json:"info"`
// The default value for the $schema keyword within Schema Objects contained
// within this OAS document.
JSONSchemaDialect *uri.URI `json:"jsonSchemaDialect,omitempty"`
// A list of tags used by the document with additional metadata. The order
// of the tags can be used to reflect on their order by the parsing tools.
// Not all tags that are used by the Operation Object must be declared. The
// tags that are not declared MAY be organized randomly or based on the
// tools’ logic. Each tag name in the list MUST be unique.
Tags *TagSlice `json:"tags,omitempty"`
// An array of Server Objects, which provide connectivity information to a
// target server. If the servers property is not provided, or is an empty
// array, the default value would be a Server Object with a url value of /.
Servers *ServerSlice `json:"servers,omitempty" yaml:"servers,omitempty,omtiempty"`
// The available paths and operations for the API.
Paths *Paths `json:"paths,omitempty"`
// The incoming webhooks that MAY be received as part of this API and that
// the API consumer MAY choose to implement. Closely related to the
// callbacks feature, this section describes requests initiated other than
// by an API call, for example by an out of band registration. The key name
// is a unique string to refer to each webhook, while the (optionally
// referenced) Path Item Object describes a request that may be initiated by
// the API provider and the expected responses. An example is available.
Webhooks *PathItemMap `json:"webhooks,omitempty"`
// An element to hold various schemas for the document.
Components *Components `json:"components,omitempty"`
// A declaration of which security mechanisms can be used across the API.
//
// The list of values includes alternative security requirement objects that
// can be used.
//
// Only one of the security requirement objects need to be
// satisfied to authorize a request. Individual operations can override this
// definition.
//
// To make security optional, an empty security requirement ({})
// can be included in the array.
//
Security *SecurityRequirementSlice `json:"security,omitempty"`
// Additional external documentation.
ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"`
}
func (*Document) Kind() Kind { return KindDocument }
func (d *Document) Refs() []Ref {
var refs []Ref
if d.Info != nil {
refs = append(refs, d.Info.Refs()...)
}
if d.Tags != nil {
refs = append(refs, d.Tags.Refs()...)
}
if d.Servers != nil {
refs = append(refs, d.Servers.Refs()...)
}
if d.Paths != nil {
refs = append(refs, d.Paths.Refs()...)
}
if d.Webhooks != nil {
refs = append(refs, d.Webhooks.Refs()...)
}
if d.Components != nil {
refs = append(refs, d.Components.Refs()...)
}
if d.ExternalDocs != nil {
refs = append(refs, d.ExternalDocs.Refs()...)
}
if d.Security != nil {
refs = append(refs, d.Security.Refs()...)
}
return refs
}
func (d *Document) nodes() []node {
edges := appendEdges(nil, d.Info)
edges = appendEdges(edges, d.Tags)
edges = appendEdges(edges, d.Servers)
edges = appendEdges(edges, d.Paths)
edges = appendEdges(edges, d.Webhooks)
edges = appendEdges(edges, d.Components)
edges = appendEdges(edges, d.Security)
edges = appendEdges(edges, d.ExternalDocs)
return edges
}
func (d *Document) isNil() bool {
return d == nil
}
func (*Document) mapKind() Kind { return KindUndefined }
func (d *Document) setLocation(loc Location) error {
if d == nil {
return fmt.Errorf("cannot set location on nil Document")
}
d.Location = loc
if err := d.Info.setLocation(loc.AppendLocation("info")); err != nil {
return err
}
if err := d.Tags.setLocation(loc.AppendLocation("tags")); err != nil {
return err
}
if err := d.Servers.setLocation(loc.AppendLocation("servers")); err != nil {
return err
}
if err := d.Paths.setLocation(loc.AppendLocation("paths")); err != nil {
return err
}
if err := d.Webhooks.setLocation(loc.AppendLocation("webhooks")); err != nil {
return err
}
if err := d.Components.setLocation(loc.AppendLocation("components")); err != nil {
return err
}
if err := d.Security.setLocation(loc.AppendLocation("security")); err != nil {
return err
}
if err := d.ExternalDocs.setLocation(loc.AppendLocation("externalDocs")); err != nil {
return err
}
return nil
}
func (*Document) sliceKind() Kind { return KindUndefined }
// MarshalJSON marshals JSON
func (d Document) MarshalJSON() ([]byte, error) {
type document Document
return marshalExtendedJSON(document(d))
}
// UnmarshalJSON unmarshals JSON
func (d *Document) UnmarshalJSON(data []byte) error {
type openapi Document
v := openapi{}
err := unmarshalExtendedJSON(data, &v)
*d = Document(v)
return err
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Marshaler interface
func (d Document) MarshalYAML() (interface{}, error) {
j, err := d.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 satisfies gopkg.in/yaml.v3 Unmarshaler interface
func (d *Document) 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, d)
}
func (d *Document) Anchors() (*Anchors, error) {
if d == nil {
return nil, nil
}
var anchors *Anchors
var err error
if anchors, err = anchors.merge(d.Paths.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(d.Components.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(d.Webhooks.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(d.Servers.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(d.Tags.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(d.Security.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(d.Info.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(d.ExternalDocs.Anchors()); err != nil {
return nil, err
}
return anchors, nil
}
var _ node = (*Document)(nil)
// func (d *Document) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return d.resolveNodeByPointer(ptr)
// }
// func (d *Document) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return d, nil
// }
// nxt, tok, _ := ptr.Next()
// switch tok {
// case "tags":
// if d.Tags == nil {
// return nil, newErrNotFound(d.AbsoluteLocation(), tok)
// }
// return d.Tags.resolveNodeByPointer(nxt)
// case "servers":
// if d.Servers == nil {
// return nil, newErrNotFound(d.AbsoluteLocation(), tok)
// }
// return d.Servers.resolveNodeByPointer(nxt)
// case "paths":
// if d.Paths == nil {
// return nil, newErrNotFound(d.AbsoluteLocation(), tok)
// }
// return d.Paths.resolveNodeByPointer(nxt)
// case "webhooks":
// if d.Webhooks == nil {
// return nil, newErrNotFound(d.AbsoluteLocation(), tok)
// }
// return d.Webhooks.resolveNodeByPointer(nxt)
// case "components":
// if d.Components == nil {
// return nil, newErrNotFound(d.AbsoluteLocation(), tok)
// }
// return d.Components.resolveNodeByPointer(nxt)
// case "externalDocs":
// if d.ExternalDocs == nil {
// return nil, newErrNotFound(d.AbsoluteLocation(), tok)
// }
// return d.ExternalDocs.resolveNodeByPointer(nxt)
// case "info":
// if d.Info == nil {
// return nil, newErrNotFound(d.AbsoluteLocation(), tok)
// }
// return d.Info.resolveNodeByPointer(nxt)
// case "security":
// if d.Security == nil {
// return nil, newErrNotFound(d.AbsoluteLocation(), tok)
// }
// return d.Security.resolveNodeByPointer(nxt)
// default:
// return nil, newErrNotResolvable(d.AbsoluteLocation(), tok)
// }
// }