-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspec.go
executable file
·48 lines (43 loc) · 1.13 KB
/
spec.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
package gomvc
import (
"os"
"strings"
"github.com/getkin/kin-openapi/openapi2"
"github.com/getkin/kin-openapi/openapi2conv"
"github.com/getkin/kin-openapi/openapi3"
yaml "github.com/ghodss/yaml"
)
// LoadWithKin loads an OpenAPI spec into memory using the kin-openapi library
func LoadWithKin(specPath string) *openapi3.T {
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
oa3, err := loader.LoadFromFile(specPath)
if err != nil {
panic(err)
}
return oa3
}
// LoadSwaggerV2AsV3 takes the file path of a v2 Swagger file and returns a
// V3 representation
func LoadSwaggerV2AsV3(specPath string) *openapi3.T {
swaggerSpec := openapi2.T{}
c, err := os.ReadFile(specPath)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(c, &swaggerSpec)
if err != nil {
panic(err)
}
oa3, err := openapi2conv.ToV3(&swaggerSpec)
if err != nil {
panic(err)
}
return oa3
}
// assumes usage of OpenAPI 3.x spec in which component refs are formatted as
// '#/componeents/<sub component type>/<user defined component name>'
func getComponentName(ref string) string {
parts := strings.Split(ref, "/")
return parts[len(parts)-1]
}