-
Notifications
You must be signed in to change notification settings - Fork 28
/
visit.go
132 lines (126 loc) · 3.48 KB
/
visit.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package openapi3
import (
"fmt"
"net/http"
"strings"
oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/simplego/encoding/jsonutil"
)
const (
jPtrParamFormat = "#/components/parameters/%s"
jPtrSchemasRoot = "#/components/schemas/"
jPtrSchemaPropertyFormat = "#/components/schemas/%s/properties/%s"
)
func VisitTypesFormats(spec *oas3.Swagger, visitTypeFormat func(jsonPointerRoot, oasType, oasFormat string)) {
for schemaName, schemaRef := range spec.Components.Schemas {
if schemaRef.Value == nil {
continue
}
for propName, propRef := range schemaRef.Value.Properties {
if propRef.Value == nil {
continue
}
visitTypeFormat(
fmt.Sprintf(jPtrSchemaPropertyFormat, schemaName, propName),
propRef.Value.Type,
propRef.Value.Format)
}
}
for paramName, paramRef := range spec.Components.Parameters {
if paramRef.Value == nil ||
paramRef.Value.Schema == nil ||
paramRef.Value.Schema.Value == nil {
continue
}
visitTypeFormat(
fmt.Sprintf(jPtrParamFormat, paramName),
paramRef.Value.Schema.Value.Type,
paramRef.Value.Schema.Value.Format)
}
VisitOperations(
spec,
func(path, method string, op *oas3.Operation) {
if op == nil {
return
}
for i, paramRef := range op.Parameters {
if paramRef.Value == nil ||
paramRef.Value.Schema == nil ||
paramRef.Value.Schema.Value == nil {
continue
}
visitTypeFormat(
jsonutil.PointerSubEscapeAll(
"#/paths/%s/%s/parameters/%d/schema", path, strings.ToLower(method), i),
paramRef.Value.Schema.Value.Type,
paramRef.Value.Schema.Value.Format)
}
},
)
}
func VisitOperationsPathItem(pathURL string, pathItem *oas3.PathItem, visitOp func(path, method string, op *oas3.Operation)) {
if pathItem == nil {
return
}
if pathItem.Connect != nil {
visitOp(pathURL, http.MethodConnect, pathItem.Connect)
}
if pathItem.Delete != nil {
visitOp(pathURL, http.MethodDelete, pathItem.Delete)
}
if pathItem.Get != nil {
visitOp(pathURL, http.MethodGet, pathItem.Get)
}
if pathItem.Head != nil {
visitOp(pathURL, http.MethodHead, pathItem.Head)
}
if pathItem.Options != nil {
visitOp(pathURL, http.MethodOptions, pathItem.Options)
}
if pathItem.Patch != nil {
visitOp(pathURL, http.MethodPatch, pathItem.Patch)
}
if pathItem.Post != nil {
visitOp(pathURL, http.MethodPost, pathItem.Post)
}
if pathItem.Put != nil {
visitOp(pathURL, http.MethodPut, pathItem.Put)
}
if pathItem.Trace != nil {
visitOp(pathURL, http.MethodTrace, pathItem.Trace)
}
}
func VisitOperations(spec *oas3.Swagger, visitOp func(path, method string, op *oas3.Operation)) {
for path, pathItem := range spec.Paths {
VisitOperationsPathItem(path, pathItem, visitOp)
/*
if pathItem.Connect != nil {
visitOp(path, http.MethodConnect, pathItem.Connect)
}
if pathItem.Delete != nil {
visitOp(path, http.MethodDelete, pathItem.Delete)
}
if pathItem.Get != nil {
visitOp(path, http.MethodGet, pathItem.Get)
}
if pathItem.Head != nil {
visitOp(path, http.MethodHead, pathItem.Head)
}
if pathItem.Options != nil {
visitOp(path, http.MethodOptions, pathItem.Options)
}
if pathItem.Patch != nil {
visitOp(path, http.MethodPatch, pathItem.Patch)
}
if pathItem.Post != nil {
visitOp(path, http.MethodPost, pathItem.Post)
}
if pathItem.Put != nil {
visitOp(path, http.MethodPut, pathItem.Put)
}
if pathItem.Trace != nil {
visitOp(path, http.MethodTrace, pathItem.Trace)
}
*/
}
}