-
Notifications
You must be signed in to change notification settings - Fork 13
/
oas2Swagger.go
50 lines (42 loc) · 1.07 KB
/
oas2Swagger.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
package apic
import (
"strings"
"github.com/getkin/kin-openapi/openapi2"
"gopkg.in/yaml.v3"
)
// oas2Swagger Wrapper type for the openapi2.T struct
type oas2Swagger struct {
openapi2.T
}
// UnmarshalYAML - custom unmarshaler for oas2 swagger to ensure keys, at the top level, are lowercased
func (o *oas2Swagger) UnmarshalYAML(unmarshal func(interface{}) error) error {
// first unmarshall it into a map[string]interface
var keyInterface map[string]interface{}
err := unmarshal(&keyInterface)
if err != nil {
return err
}
// now loop the keys and lowercase them all
for key, val := range keyInterface {
if strings.ToLower(key) == key {
continue
}
// store the val in the lowercase key
keyInterface[strings.ToLower(key)] = val
// delete the non-lowercase val
delete(keyInterface, key)
}
// convert keyInterface back to byte array
newBytes, err := yaml.Marshal(keyInterface)
if err != nil {
return err
}
// unmarshal new byte array
var newVal openapi2.T
err = yaml.Unmarshal(newBytes, &newVal)
if err != nil {
return err
}
o.T = newVal
return nil
}