-
Notifications
You must be signed in to change notification settings - Fork 42
/
openapi2.go
94 lines (82 loc) · 2.35 KB
/
openapi2.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
package importer
import (
"encoding/json"
"fmt"
"net/url"
"os"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/getkin/kin-openapi/openapi2"
"github.com/getkin/kin-openapi/openapi2conv"
"github.com/getkin/kin-openapi/openapi3"
)
// https://swagger.io/blog/api-strategy/difference-between-swagger-and-openapi/
// OpenAPI = Specification
// Swagger = Tools for implementing the specification
type OpenAPI2Importer struct {
openAPI2Spec string
filepath string
*OpenAPI3Importer
}
func MakeOpenAPI2Importer(logger *logrus.Logger, basePath string, filePath string) *OpenAPI2Importer {
return &OpenAPI2Importer{OpenAPI3Importer: &OpenAPI3Importer{
appName: "",
pkg: "",
basePath: basePath,
logger: logger,
fs: afero.NewOsFs(),
types: TypeList{},
},
filepath: filePath}
}
func (l *OpenAPI2Importer) LoadFile(path string) (string, error) {
bs, err := os.ReadFile(path)
if err != nil {
return "", err
}
return l.Load(string(bs))
}
func (l *OpenAPI2Importer) Load(oas2spec string) (string, error) {
u, err := pathToURL(l.filepath)
if err != nil {
return "", err
}
oas3spec, basePath, err := convertToOpenAPI3([]byte(oas2spec), u, NewOpenAPI3Loader(l.logger, l.fs))
if err != nil {
return "", fmt.Errorf("error converting openapi 2:%w", err)
}
l.openAPI2Spec = oas2spec
l.basePath = basePath
return l.convertSpec(oas3spec)
}
// Configure allows the imported Sysl application name, package and import directories to be specified.
func (l *OpenAPI2Importer) Configure(arg *ImporterArg) (Importer, error) {
if arg.AppName == "" {
return nil, errors.New("application name not provided")
}
l.appName = arg.AppName
l.pkg = arg.PackageName
return l, nil
}
// convertToOpenAPI3 takes a openapi2 spec and converts it to openapi3
func convertToOpenAPI3(data []byte, uri *url.URL, loader *openapi3.Loader) (*openapi3.T, string, error) {
var openapiv2 openapi2.T
jsondata, err := yaml.YAMLToJSON(data)
if err != nil {
return nil, "", err
}
err = json.Unmarshal(jsondata, &openapiv2)
if err != nil {
return nil, "", err
}
if len(openapiv2.Schemes) == 0 {
openapiv2.Schemes = []string{"https"}
}
openapiv3, err := openapi2conv.ToV3WithLoader(&openapiv2, loader, uri)
if err != nil {
return nil, "", err
}
return openapiv3, openapiv2.BasePath, nil
}