/
oas.go
92 lines (82 loc) · 2.32 KB
/
oas.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
package oas
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
"github.com/getkin/kin-openapi/openapi2"
"github.com/getkin/kin-openapi/openapi3"
)
// ParseOAS2 converts a JSON spec into an OpenAPI2 object.
func ParseOAS2(spec []byte) (*openapi2.T, error) {
swaggerObj := &openapi2.T{}
err := json.Unmarshal(spec, swaggerObj)
if err != nil {
return nil, err
}
if !strings.Contains(swaggerObj.Swagger, "2.") {
return nil, errors.New(oasParseError("2.0", "'swagger' must be version '2.0'."))
}
if swaggerObj.Info.Title == "" {
return nil, errors.New(oasParseError("2.0", "'info.title' key not found."))
}
if swaggerObj.Paths == nil {
return nil, errors.New(oasParseError("2.0", "'paths' key not found."))
}
return swaggerObj, nil
}
// ParseOAS3 converts a JSON or YAML spec into an OpenAPI3 object.
func ParseOAS3(spec []byte) (*openapi3.T, error) {
oas3Obj, err := openapi3.NewLoader().LoadFromData(spec)
if err != nil {
return nil, err
}
if !strings.Contains(oas3Obj.OpenAPI, "3.") {
return nil, fmt.Errorf(oasParseError("3", ("'openapi' key is invalid.")))
}
if oas3Obj.Paths == nil {
return nil, fmt.Errorf(oasParseError("3", "'paths' key not found."))
}
if oas3Obj.Info == nil {
return nil, fmt.Errorf(oasParseError("3", "'info' key not found."))
}
if oas3Obj.Info.Title == "" {
return nil, fmt.Errorf(oasParseError("3", "'info.title' key not found."))
}
return oas3Obj, nil
}
// SetOAS2HostDetails Updates the Host, BasePath, and Schemes fields on an OpenAPI2 object.
func SetOAS2HostDetails(spec *openapi2.T, endpointURL string) error {
endpoint, err := url.Parse(endpointURL)
if err != nil {
return err
}
basePath := ""
if endpoint.Path == "" {
basePath = "/"
} else {
basePath = endpoint.Path
}
host := endpoint.Host
schemes := []string{endpoint.Scheme}
spec.Host = host
spec.BasePath = basePath
spec.Schemes = schemes
return nil
}
// SetOAS3Servers replaces the servers array on the OpenAPI3 object.
func SetOAS3Servers(hosts []string, spec *openapi3.T) {
var oas3Servers []*openapi3.Server
for _, s := range hosts {
oas3Servers = append(oas3Servers, &openapi3.Server{
URL: s,
})
}
if len(oas3Servers) > 0 {
spec.Servers = oas3Servers
}
}
func oasParseError(version string, msg string) string {
return fmt.Sprintf("invalid openapi %s specification. %s", version, msg)
}