-
Notifications
You must be signed in to change notification settings - Fork 5
/
document.go
216 lines (190 loc) · 5.48 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
package vervet
import (
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/ghodss/yaml"
"github.com/google/uuid"
)
func init() {
// Necessary for `format: uuid` to validate.
openapi3.DefineStringFormatCallback("uuid", func(v string) error {
_, err := uuid.Parse(v)
return err
})
openapi3.DefineStringFormatCallback("url", func(v string) error {
_, err := url.Parse(v)
return err
})
}
// Document is an OpenAPI 3 document object model.
type Document struct {
*openapi3.T
path string
url *url.URL
}
// NewDocumentFile loads an OpenAPI spec file from the given file path,
// returning a document object.
func NewDocumentFile(specFile string) (_ *Document, returnErr error) {
// Restore current working directory upon returning
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
defer func() {
err := os.Chdir(cwd)
if err != nil {
log.Println("warning: failed to restore working directory: %w", err)
if returnErr == nil {
returnErr = err
}
}
}()
specFile, err = filepath.Abs(specFile)
if err != nil {
return nil, fmt.Errorf("failed to get absolute path: %w", err)
}
// `cd` to the path containing the spec file, so that relative paths
// resolve.
specBase, specDir := filepath.Base(specFile), filepath.Dir(specFile)
err = os.Chdir(specDir)
if err != nil {
return nil, fmt.Errorf("failed to chdir %q: %w", specDir, err)
}
specURL, err := url.Parse(specFile)
if err != nil {
return nil, err
}
var t openapi3.T
contents, err := os.ReadFile(specFile)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(contents, &t)
if err != nil {
return nil, err
}
err = newRefAliasResolver(&t).resolve()
if err != nil {
return nil, err
}
l := openapi3.NewLoader()
l.IsExternalRefsAllowed = true
err = l.ResolveRefsIn(&t, specURL)
if err != nil {
return nil, fmt.Errorf("failed to load %q: %w", specBase, err)
}
if t.Components == nil {
t.Components = &openapi3.Components{}
}
return &Document{
T: &t,
path: specFile,
url: specURL,
}, nil
}
// NewResolvedDocument returns a Document that has already been loaded and
// references resolved from the given URL. The URL is provided to indicate the
// document's origin in logging and error messages.
func NewResolvedDocument(t *openapi3.T, url *url.URL) *Document {
return &Document{
T: t,
path: url.String(),
url: url,
}
}
// MarshalJSON implements json.Marshaler.
func (d *Document) MarshalJSON() ([]byte, error) {
return d.T.MarshalJSON()
}
// RelativePath returns the relative path for resolving references from the
// file path location of the top-level document: the directory which contains
// the file from which the top-level document was loaded.
func (d *Document) RelativePath() string {
return filepath.Dir(d.path)
}
// Location returns the URL from where the document was loaded.
func (d *Document) Location() *url.URL {
return d.url
}
// ResolveRefs resolves all Ref types in the document, causing the Value field
// of each Ref to be loaded and populated from its referenced location.
func (d *Document) ResolveRefs() error {
l := openapi3.NewLoader()
l.IsExternalRefsAllowed = true
return l.ResolveRefsIn(d.T, d.url)
}
// LoadReference loads a reference from refPath, relative to relPath, into
// target. The relative path of the reference is returned, so that references
// may be chain-loaded with successive calls.
func (d *Document) LoadReference(relPath, refPath string, target interface{}) (_ string, returnErr error) {
refUrl, err := url.Parse(refPath)
if err != nil {
return "", err
}
if refUrl.Scheme == "" || refUrl.Scheme == "file" {
refPath, err = filepath.Abs(filepath.Join(relPath, refUrl.Path))
if err != nil {
return "", err
}
refUrl.Path = refPath
}
// Parse and load the contents of the referenced document.
l := openapi3.NewLoader()
l.IsExternalRefsAllowed = true
contents, err := openapi3.DefaultReadFromURI(l, refUrl)
if err != nil {
return "", fmt.Errorf("failed to read %q: %w", refUrl, err)
}
// If the reference is to an element in the referenced document, further resolve that.
if refUrl.Fragment != "" {
parts := strings.Split(refUrl.Fragment, "/")
// TODO: support actual jsonpaths if/when needed. For now only
// top-level properties are supported.
if parts[0] != "" || len(parts) > 2 {
return "", fmt.Errorf("URL %q not supported", refUrl)
}
elements := map[string]interface{}{}
err := yaml.Unmarshal(contents, &elements)
if err != nil {
return "", err
}
elementDoc, ok := elements[parts[1]]
if !ok {
return "", fmt.Errorf("element %q not found in %q", parts[1], refUrl.Path)
}
contents, err = json.Marshal(elementDoc)
if err != nil {
return "", err
}
}
// Unmarshal the resolved reference into target object.
err = yaml.Unmarshal(contents, target)
if err != nil {
return "", err
}
return filepath.Abs(filepath.Dir(refUrl.Path))
}
// Version returns the version of the document.
func (d *Document) Version() (Version, error) {
versionDir := filepath.Dir(d.path)
versionStr := filepath.Base(versionDir)
return ParseVersion(versionStr)
}
// Lifecycle returns the lifecycle of the document.
func (d *Document) Lifecycle() (Lifecycle, error) {
ls, err := ExtensionString(d.Extensions, ExtSnykApiLifecycle)
if err != nil {
if IsExtensionNotFound(err) {
// If it's not marked as deprecated or sunset, assume it's released.
return LifecycleReleased, err
}
return lifecycleUndefined, err
}
return ParseLifecycle(ls)
}