-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathopenapi.go
52 lines (46 loc) · 1.12 KB
/
openapi.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
package openapi
import (
"bytes"
"io"
"os"
yaml "gopkg.in/yaml.v2"
)
// LoadFile OpenAPI Specification v3.0 spec file.
func LoadFile(filename string) (*Document, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
var buf bytes.Buffer
if _, err := io.Copy(&buf, f); err != nil {
return nil, err
}
b := buf.Bytes()
if err := f.Close(); err != nil {
panic(err)
}
return Load(b)
}
// Load OpenAPI Specification v3.0 spec.
func Load(b []byte) (*Document, error) {
doc := &Document{}
if err := yaml.Unmarshal(b, doc); err != nil {
return nil, err
}
// 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 /.
// see: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#oasObject
if doc.Servers == nil || len(doc.Servers) == 0 {
doc.Servers = []*Server{&Server{URL: "/"}}
}
for i := range doc.Security {
doc.Security[i].setDocument(doc)
}
for _, pi := range doc.Paths {
for _, op := range pi.Operations() {
for _, sr := range op.Security {
sr.setDocument(doc)
}
}
}
return doc, nil
}