-
Notifications
You must be signed in to change notification settings - Fork 5
/
ref_index.go
66 lines (60 loc) · 1.94 KB
/
ref_index.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
package vervet
import (
"github.com/getkin/kin-openapi/openapi3"
)
// RefIndex indexes the distinct references used in an OpenAPI document.
type RefIndex struct {
refs map[string]struct{}
}
// NewRefIndex returns a new reference index on an OpenAPI document.
func NewRefIndex(doc *openapi3.T) (*RefIndex, error) {
ix := &RefIndex{refs: map[string]struct{}{}}
ix.index(doc)
return ix, nil
}
func (ix *RefIndex) index(doc *openapi3.T) {
for _, schemaRef := range doc.Components.Schemas {
ix.refs[extractRef(schemaRef)] = struct{}{}
}
for _, parameterRef := range doc.Components.Parameters {
ix.refs[extractRef(parameterRef)] = struct{}{}
}
for _, headerRef := range doc.Components.Headers {
ix.refs[extractRef(headerRef)] = struct{}{}
}
for _, requestBodyRef := range doc.Components.RequestBodies {
ix.refs[extractRef(requestBodyRef)] = struct{}{}
}
for _, responseRef := range doc.Components.Responses {
ix.refs[extractRef(responseRef)] = struct{}{}
for _, content := range responseRef.Value.Content {
ix.refs[extractRef(content.Schema)] = struct{}{}
for _, propertyRef := range content.Schema.Value.Properties {
ix.refs[extractRef(propertyRef)] = struct{}{}
}
}
}
for _, securitySchemesRef := range doc.Components.SecuritySchemes {
ix.refs[extractRef(securitySchemesRef)] = struct{}{}
}
for _, exampleRef := range doc.Components.Examples {
ix.refs[extractRef(exampleRef)] = struct{}{}
}
for _, linkRef := range doc.Components.Links {
ix.refs[extractRef(linkRef)] = struct{}{}
}
for _, callbackRef := range doc.Components.Callbacks {
ix.refs[extractRef(callbackRef)] = struct{}{}
}
}
func extractRef(componentRef openapi3.ComponentRef) string {
if componentRef == nil || componentRef.RefPath() == nil {
return ""
}
return "#" + componentRef.RefPath().Fragment
}
// HasRef returns whether the indexed document contains the given ref.
func (ix *RefIndex) HasRef(ref string) bool {
_, ok := ix.refs[ref]
return ok
}