-
Notifications
You must be signed in to change notification settings - Fork 42
/
swagger.go
85 lines (72 loc) · 2.02 KB
/
swagger.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
package importer
import (
"encoding/json"
"strings"
"github.com/anz-bank/sysl/pkg/importer/openapi2conv"
"github.com/ghodss/yaml"
"github.com/getkin/kin-openapi/openapi2"
"github.com/getkin/kin-openapi/openapi3"
"github.com/sirupsen/logrus"
)
func LoadSwaggerText(args OutputData, text string, logger *logrus.Logger) (out string, err error) {
openapiv3, basePath, err := convertToOpenapiv3([]byte(text))
if err != nil {
return "", err
}
return importOpenAPI(args, openapiv3, logger, basePath)
}
func convertToOpenapiv3(data []byte) (*openapi3.Swagger, string, error) {
var swagger2 openapi2.Swagger
jsondata, err := yaml.YAMLToJSON(data)
if err != nil {
return nil, "", err
}
err = json.Unmarshal(jsondata, &swagger2)
if err != nil {
return nil, "", err
}
if len(swagger2.Schemes) == 0 {
swagger2.Schemes = []string{"https"}
}
openapiv3, err := openapi2conv.ToV3Swagger(&swagger2)
if err != nil {
return nil, "", err
}
return openapiv3, swagger2.BasePath, nil
}
// nolint:gochecknoglobals
var swaggerFormats = []string{"int32", "int64", "float", "double", "date", "date-time", "byte"}
func mapSwaggerTypeAndFormatToType(typeName, format string, logger *logrus.Logger) string {
typeName = strings.ToLower(typeName)
format = strings.ToLower(format)
if format != "" && !contains(format, swaggerFormats) {
logger.Errorf("unknown format '%s' being used, ignoring...\n", format)
format = ""
}
conversions := map[string]map[string]string{
StringTypeName: {
"": StringTypeName,
"date": "date",
"date-time": "datetime",
"byte": StringTypeName,
},
"integer": {
"": "int",
"int32": "int32",
"int64": "int64",
},
"number": {
"": "float",
"double": "float",
"float": "float",
},
}
if formatMap, ok := conversions[typeName]; ok {
if result, ok := formatMap[format]; ok {
return result
}
logger.Warnf("Unhandled (type, format) -> (%s, %s)\n", typeName, format)
return mapSwaggerTypeAndFormatToType(typeName, "", logger)
}
return typeName
}