-
Notifications
You must be signed in to change notification settings - Fork 28
/
validate_and_fix.go
232 lines (222 loc) · 7.04 KB
/
validate_and_fix.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package openapi3edit
import (
"fmt"
"reflect"
"regexp"
"strings"
oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/mogo/net/httputilmore"
"github.com/grokify/mogo/type/stringsutil"
"github.com/grokify/spectrum/openapi3"
)
var rxParens = regexp.MustCompile(`{([^}{}]+)}`)
func ParsePathParametersParens(urlPath string) []string {
paramNames := []string{}
m := rxParens.FindAllStringSubmatch(urlPath, -1)
if len(m) == 0 {
return paramNames
}
for _, n := range m {
if len(n) == 2 {
varName := strings.TrimSpace(n[1])
paramNames = append(paramNames, varName)
}
}
if len(paramNames) > 0 {
paramNames = stringsutil.SliceCondenseSpace(paramNames, true, false)
}
return paramNames
}
/*
func OperationHasParameter(paramNameWant string, op *oas3.Operation) bool {
paramNameWantLc := strings.ToLower(strings.TrimSpace(paramNameWant))
for _, paramRef := range op.Parameters {
if paramRef.Value == nil {
continue
}
param := paramRef.Value
param.Name = strings.TrimSpace(param.Name)
paramNameTryLc := strings.ToLower(param.Name)
if paramNameWantLc == paramNameTryLc {
return true
}
}
return false
}
*/
// SortParameters sorts parameters according to an input name list.
// This used to resort parameters inline with path path parameters
// so they line up properly when rendered.
func SortParameters(sourceParams oas3.Parameters, sortOrder []string) oas3.Parameters {
sortedParams := oas3.Parameters{}
addedNames := map[string]int{}
oldParamsMap := map[string]*oas3.ParameterRef{}
for _, pRef := range sourceParams {
if pRef.Value != nil {
oldParamsMap[pRef.Value.Name] = pRef
}
}
for _, sortedName := range sortOrder {
if pRef, ok := oldParamsMap[sortedName]; ok {
sortedParams = append(sortedParams, pRef)
addedNames[sortedName] = 1
}
}
for _, pRef := range sourceParams {
if pRef.Value == nil {
sortedParams = append(sortedParams, pRef)
} else if _, ok := addedNames[pRef.Value.Name]; !ok {
sortedParams = append(sortedParams, pRef)
}
}
if len(sortedParams) != len(sourceParams) {
panic("E_SORT_LEN_MISMATCH")
}
return sortedParams
}
// ValidateFixOperationPathParameters will add missing path parameters
// and re-sort parameters so required path parameters are on top and
// sorted by their position in the path.
func ValidateFixOperationPathParameters(spec *openapi3.Spec, fix bool) ([]*openapi3.OperationMeta, error) {
errorOperations := []*openapi3.OperationMeta{}
openapi3.VisitOperations(
spec,
func(path, method string, op *oas3.Operation) {
if op == nil {
return
}
varNamesPath := ParsePathParametersParens(path)
if len(varNamesPath) == 0 {
return
}
varNamesMissing := []string{}
fixed := false
opm := OperationMore{Operation: op}
for _, varName := range varNamesPath {
if !opm.HasParameter(varName) {
if fix {
newParamRef := &oas3.ParameterRef{
Value: &oas3.Parameter{
Name: varName,
In: "path",
Required: true,
Schema: &oas3.SchemaRef{
Value: &oas3.Schema{
Type: "string",
},
},
},
}
if op.Parameters == nil {
params := make(oas3.Parameters, 0, 1)
params = append(params, newParamRef)
op.Parameters = params
} else {
op.Parameters = append(op.Parameters, newParamRef)
}
fixed = true
} else {
varNamesMissing = append(varNamesMissing, varName)
}
}
}
if fixed {
op.Parameters = SortParameters(op.Parameters, varNamesPath)
}
if len(varNamesMissing) > 0 {
om := openapi3.OperationToMeta(path, method, op)
om.MetaNotes = append(om.MetaNotes,
fmt.Sprintf("E_OP_MISSING_PATH_PARAMETER PARAM_NAMES[%s]",
strings.Join(varNamesMissing, ",")))
errorOperations = append(errorOperations, &om)
}
},
)
if len(errorOperations) > 0 {
return errorOperations, fmt.Errorf("E_NUM_VALIDATION_ERRORS [%v]", len(errorOperations))
}
return errorOperations, nil
}
// MoveRequestBodies moves `requestBody` `$ref` to the operation
// which appears to be supported by more tools.
func MoveRequestBodies(spec *openapi3.Spec, move bool) ([]*openapi3.OperationMeta, error) {
errorOperations := []*openapi3.OperationMeta{}
specMore := openapi3.SpecMore{Spec: spec}
openapi3.VisitOperations(
spec,
func(path, method string, op *oas3.Operation) {
if op == nil || op.RequestBody == nil {
return
}
if len(op.RequestBody.Ref) > 0 {
if move {
requestBodyRef := specMore.ComponentRequestBody(op.RequestBody.Ref)
if requestBodyRef != nil {
op.RequestBody = requestBodyRef
}
} else {
om := openapi3.OperationToMeta(path, method, op)
om.MetaNotes = append(om.MetaNotes,
fmt.Sprintf("E_REQUEST_BODY_DEFINITION REF[%s]", op.RequestBody.Ref))
errorOperations = append(errorOperations, &om)
}
}
},
)
if len(errorOperations) > 0 {
return errorOperations, fmt.Errorf("E_NUM_VALIDATION_ERRORS [%v]", len(errorOperations))
}
return errorOperations, nil
}
// ValidateFixOperationResponseTypes looks for `application/json` responses
// with response schema types that are not `array` or `object`. If the responses
// is a string or integer, it will reset the response mime type to `text/plain`.
func ValidateFixOperationResponseTypes(spec *openapi3.Spec, fix bool) ([]*openapi3.OperationMeta, error) {
errorOperations := []*openapi3.OperationMeta{}
openapi3.VisitOperations(
spec,
func(path, method string, op *oas3.Operation) {
if op == nil {
return
}
for _, resRef := range op.Responses {
if resRef == nil || resRef.Value == nil {
continue
}
response := resRef
for mediaTypeOrig, mtRef := range response.Value.Content {
mediaType := strings.ToLower(strings.TrimSpace(mediaTypeOrig))
if mediaType == httputilmore.ContentTypeAppJson {
schemaRef := mtRef.Schema
if len(schemaRef.Ref) == 0 {
schema := schemaRef.Value
schemaType := schema.Type
if fix && (schemaType == "string" || schemaType == "integer") {
delete(response.Value.Content, mediaTypeOrig)
if mtRefTry, ok := response.Value.Content[httputilmore.ContentTypeTextPlain]; ok {
if !reflect.DeepEqual(mtRef, mtRefTry) {
om := openapi3.OperationToMeta(path, method, op)
om.MetaNotes = append(om.MetaNotes,
fmt.Sprintf("E_BAD_MIME_TYPE_AND_SCHEMA_COLLISION MT[%s] type[%s]", mediaType, schemaType))
errorOperations = append(errorOperations, &om)
}
} else {
response.Value.Content[httputilmore.ContentTypeTextPlain] = mtRef
}
} else if schemaType != "object" && schemaType != "array" {
om := openapi3.OperationToMeta(path, method, op)
om.MetaNotes = append(om.MetaNotes,
fmt.Sprintf("E_BAD_MIME_TYPE_AND_SCHEMA MT[%s] type[%s]", mediaType, schemaType))
errorOperations = append(errorOperations, &om)
}
}
}
}
}
},
)
if len(errorOperations) > 0 {
return errorOperations, fmt.Errorf("E_NUM_VALIDATION_ERRORS [%v]", len(errorOperations))
}
return errorOperations, nil
}