-
Notifications
You must be signed in to change notification settings - Fork 5
/
ref_alias_resolver.go
77 lines (68 loc) · 2 KB
/
ref_alias_resolver.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
package vervet
import (
"encoding/json"
"reflect"
"strings"
"github.com/getkin/kin-openapi/jsoninfo"
"github.com/getkin/kin-openapi/openapi3"
"github.com/mitchellh/reflectwalk"
)
// refAliasResolver rewrites references in an OpenAPI document object to local
// references, so that the spec is self-contained.
type refAliasResolver struct {
doc *openapi3.T
refAliases map[string]string
curRefType reflect.Value
curRefField reflect.Value
}
// newRefAliasResolver returns a new refAliasResolver.
func newRefAliasResolver(doc *openapi3.T) (*refAliasResolver, error) {
refAliases := map[string]string{}
for refAlias, extValue := range doc.Components.ExtensionProps.Extensions {
contents, ok := extValue.(json.RawMessage)
if !ok {
continue
}
dec, err := jsoninfo.NewObjectDecoder(contents)
if err != nil {
return nil, err
}
var ref openapi3.Ref
if err := doc.Components.ExtensionProps.DecodeWith(dec, &ref); err == nil && ref.Ref != "" {
refAliases["#/components/"+refAlias] = ref.Ref
}
}
return &refAliasResolver{doc: doc, refAliases: refAliases}, nil
}
func (l *refAliasResolver) resolveRefAlias(ref string) string {
if ref != "" && ref[0] == '#' {
for refAlias, refTarget := range l.refAliases {
if strings.HasPrefix(ref, refAlias) {
return strings.Replace(ref, refAlias, refTarget+"#", 1)
}
}
}
return ref
}
// resolve rewrites all references in the OpenAPI document to local references.
func (l *refAliasResolver) resolve() error {
return reflectwalk.Walk(l.doc, l)
}
// Struct implements reflectwalk.StructWalker
func (l *refAliasResolver) Struct(v reflect.Value) error {
l.curRefType, l.curRefField = v, v.FieldByName("Ref")
return nil
}
// StructField implements reflectwalk.StructWalker
func (l *refAliasResolver) StructField(sf reflect.StructField, v reflect.Value) error {
if !l.curRefField.IsValid() {
return nil
}
ref := l.curRefField.String()
if ref == "" {
return nil
}
ref = l.resolveRefAlias(ref)
l.curRefField.Set(reflect.ValueOf(ref))
return nil
}