Skip to content

Commit 0e6b74d

Browse files
committed
cleaned up panic in path params when schema is missing
1 parent b29caff commit 0e6b74d

File tree

2 files changed

+129
-127
lines changed

2 files changed

+129
-127
lines changed

parameters/path_parameters.go

Lines changed: 128 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -108,163 +108,165 @@ func (v *paramValidator) ValidatePathParams(request *http.Request) (bool, []*err
108108
}
109109

110110
// for each type, check the value.
111-
for typ := range sch.Type {
111+
if sch != nil && sch.Type != nil {
112+
for typ := range sch.Type {
112113

113-
switch sch.Type[typ] {
114-
case helpers.String:
114+
switch sch.Type[typ] {
115+
case helpers.String:
115116

116-
// TODO: label and matrix style validation
117+
// TODO: label and matrix style validation
117118

118-
// check if the param is within the enum
119-
if sch.Enum != nil {
120-
enumCheck(paramValue)
121-
break
122-
}
123-
validationErrors = append(validationErrors,
124-
ValidateSingleParameterSchema(
119+
// check if the param is within the enum
120+
if sch.Enum != nil {
121+
enumCheck(paramValue)
122+
break
123+
}
124+
validationErrors = append(validationErrors,
125+
ValidateSingleParameterSchema(
126+
sch,
127+
paramValue,
128+
"Path parameter",
129+
"The path parameter",
130+
p.Name,
131+
helpers.ParameterValidation,
132+
helpers.ParameterValidationPath,
133+
)...)
134+
135+
case helpers.Integer, helpers.Number:
136+
// simple use case is already handled in find param.
137+
rawParamValue, paramValueParsed, err := v.resolveNumber(sch, p, isLabel, isMatrix, paramValue)
138+
if err != nil {
139+
validationErrors = append(validationErrors, err...)
140+
break
141+
}
142+
// check if the param is within the enum
143+
if sch.Enum != nil {
144+
enumCheck(rawParamValue)
145+
break
146+
}
147+
validationErrors = append(validationErrors, ValidateSingleParameterSchema(
125148
sch,
126-
paramValue,
149+
paramValueParsed,
127150
"Path parameter",
128151
"The path parameter",
129152
p.Name,
130153
helpers.ParameterValidation,
131154
helpers.ParameterValidationPath,
132155
)...)
133156

134-
case helpers.Integer, helpers.Number:
135-
// simple use case is already handled in find param.
136-
rawParamValue, paramValueParsed, err := v.resolveNumber(sch, p, isLabel, isMatrix, paramValue)
137-
if err != nil {
138-
validationErrors = append(validationErrors, err...)
139-
break
140-
}
141-
// check if the param is within the enum
142-
if sch.Enum != nil {
143-
enumCheck(rawParamValue)
144-
break
145-
}
146-
validationErrors = append(validationErrors, ValidateSingleParameterSchema(
147-
sch,
148-
paramValueParsed,
149-
"Path parameter",
150-
"The path parameter",
151-
p.Name,
152-
helpers.ParameterValidation,
153-
helpers.ParameterValidationPath,
154-
)...)
155-
156-
case helpers.Boolean:
157-
if isLabel && p.Style == helpers.LabelStyle {
158-
if _, err := strconv.ParseFloat(paramValue[1:], 64); err != nil {
159-
validationErrors = append(validationErrors,
160-
errors.IncorrectPathParamBool(p, paramValue[1:], sch))
161-
}
162-
}
163-
if isSimple {
164-
if _, err := strconv.ParseBool(paramValue); err != nil {
165-
validationErrors = append(validationErrors,
166-
errors.IncorrectPathParamBool(p, paramValue, sch))
167-
}
168-
}
169-
if isMatrix && p.Style == helpers.MatrixStyle {
170-
// strip off the colon and the parameter name
171-
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
172-
if _, err := strconv.ParseBool(paramValue); err != nil {
173-
validationErrors = append(validationErrors,
174-
errors.IncorrectPathParamBool(p, paramValue, sch))
175-
}
176-
}
177-
case helpers.Object:
178-
var encodedObject interface{}
179-
180-
if p.IsDefaultPathEncoding() {
181-
encodedObject = helpers.ConstructMapFromCSV(paramValue)
182-
} else {
183-
switch p.Style {
184-
case helpers.LabelStyle:
185-
if !p.IsExploded() {
186-
encodedObject = helpers.ConstructMapFromCSV(paramValue[1:])
187-
} else {
188-
encodedObject = helpers.ConstructKVFromLabelEncoding(paramValue)
157+
case helpers.Boolean:
158+
if isLabel && p.Style == helpers.LabelStyle {
159+
if _, err := strconv.ParseFloat(paramValue[1:], 64); err != nil {
160+
validationErrors = append(validationErrors,
161+
errors.IncorrectPathParamBool(p, paramValue[1:], sch))
189162
}
190-
case helpers.MatrixStyle:
191-
if !p.IsExploded() {
192-
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
193-
encodedObject = helpers.ConstructMapFromCSV(paramValue)
194-
} else {
195-
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
196-
encodedObject = helpers.ConstructKVFromMatrixCSV(paramValue)
163+
}
164+
if isSimple {
165+
if _, err := strconv.ParseBool(paramValue); err != nil {
166+
validationErrors = append(validationErrors,
167+
errors.IncorrectPathParamBool(p, paramValue, sch))
197168
}
198-
default:
199-
if p.IsExploded() {
200-
encodedObject = helpers.ConstructKVFromCSV(paramValue)
169+
}
170+
if isMatrix && p.Style == helpers.MatrixStyle {
171+
// strip off the colon and the parameter name
172+
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
173+
if _, err := strconv.ParseBool(paramValue); err != nil {
174+
validationErrors = append(validationErrors,
175+
errors.IncorrectPathParamBool(p, paramValue, sch))
201176
}
202177
}
203-
}
204-
// if a schema was extracted
205-
if sch != nil {
206-
validationErrors = append(validationErrors,
207-
ValidateParameterSchema(sch,
208-
encodedObject,
209-
"",
210-
"Path parameter",
211-
"The path parameter",
212-
p.Name,
213-
helpers.ParameterValidation,
214-
helpers.ParameterValidationPath)...)
215-
}
178+
case helpers.Object:
179+
var encodedObject interface{}
216180

217-
case helpers.Array:
218-
219-
// extract the items schema in order to validate the array items.
220-
if sch.Items != nil && sch.Items.IsA() {
221-
iSch := sch.Items.A.Schema()
222-
for n := range iSch.Type {
223-
// determine how to explode the array
224-
var arrayValues []string
225-
if isSimple {
226-
arrayValues = strings.Split(paramValue, helpers.Comma)
227-
}
228-
if isLabel {
181+
if p.IsDefaultPathEncoding() {
182+
encodedObject = helpers.ConstructMapFromCSV(paramValue)
183+
} else {
184+
switch p.Style {
185+
case helpers.LabelStyle:
229186
if !p.IsExploded() {
230-
arrayValues = strings.Split(paramValue[1:], helpers.Comma)
187+
encodedObject = helpers.ConstructMapFromCSV(paramValue[1:])
231188
} else {
232-
arrayValues = strings.Split(paramValue[1:], helpers.Period)
189+
encodedObject = helpers.ConstructKVFromLabelEncoding(paramValue)
233190
}
234-
}
235-
if isMatrix {
191+
case helpers.MatrixStyle:
236192
if !p.IsExploded() {
237193
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
238-
arrayValues = strings.Split(paramValue, helpers.Comma)
194+
encodedObject = helpers.ConstructMapFromCSV(paramValue)
239195
} else {
240-
paramValue = strings.ReplaceAll(paramValue[1:], fmt.Sprintf("%s=", p.Name), "")
241-
arrayValues = strings.Split(paramValue, helpers.SemiColon)
196+
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
197+
encodedObject = helpers.ConstructKVFromMatrixCSV(paramValue)
198+
}
199+
default:
200+
if p.IsExploded() {
201+
encodedObject = helpers.ConstructKVFromCSV(paramValue)
242202
}
243203
}
244-
switch iSch.Type[n] {
245-
case helpers.Integer, helpers.Number:
246-
for pv := range arrayValues {
247-
if _, err := strconv.ParseFloat(arrayValues[pv], 64); err != nil {
248-
validationErrors = append(validationErrors,
249-
errors.IncorrectPathParamArrayNumber(p, arrayValues[pv], sch, iSch))
204+
}
205+
// if a schema was extracted
206+
if sch != nil {
207+
validationErrors = append(validationErrors,
208+
ValidateParameterSchema(sch,
209+
encodedObject,
210+
"",
211+
"Path parameter",
212+
"The path parameter",
213+
p.Name,
214+
helpers.ParameterValidation,
215+
helpers.ParameterValidationPath)...)
216+
}
217+
218+
case helpers.Array:
219+
220+
// extract the items schema in order to validate the array items.
221+
if sch.Items != nil && sch.Items.IsA() {
222+
iSch := sch.Items.A.Schema()
223+
for n := range iSch.Type {
224+
// determine how to explode the array
225+
var arrayValues []string
226+
if isSimple {
227+
arrayValues = strings.Split(paramValue, helpers.Comma)
228+
}
229+
if isLabel {
230+
if !p.IsExploded() {
231+
arrayValues = strings.Split(paramValue[1:], helpers.Comma)
232+
} else {
233+
arrayValues = strings.Split(paramValue[1:], helpers.Period)
234+
}
235+
}
236+
if isMatrix {
237+
if !p.IsExploded() {
238+
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
239+
arrayValues = strings.Split(paramValue, helpers.Comma)
240+
} else {
241+
paramValue = strings.ReplaceAll(paramValue[1:], fmt.Sprintf("%s=", p.Name), "")
242+
arrayValues = strings.Split(paramValue, helpers.SemiColon)
250243
}
251244
}
252-
case helpers.Boolean:
253-
for pv := range arrayValues {
254-
bc := len(validationErrors)
255-
if _, err := strconv.ParseBool(arrayValues[pv]); err != nil {
256-
validationErrors = append(validationErrors,
257-
errors.IncorrectPathParamArrayBoolean(p, arrayValues[pv], sch, iSch))
258-
continue
245+
switch iSch.Type[n] {
246+
case helpers.Integer, helpers.Number:
247+
for pv := range arrayValues {
248+
if _, err := strconv.ParseFloat(arrayValues[pv], 64); err != nil {
249+
validationErrors = append(validationErrors,
250+
errors.IncorrectPathParamArrayNumber(p, arrayValues[pv], sch, iSch))
251+
}
259252
}
260-
if len(validationErrors) == bc {
261-
// ParseBool will parse 0 or 1 as false/true to we
262-
// need to catch this edge case.
263-
if arrayValues[pv] == "0" || arrayValues[pv] == "1" {
253+
case helpers.Boolean:
254+
for pv := range arrayValues {
255+
bc := len(validationErrors)
256+
if _, err := strconv.ParseBool(arrayValues[pv]); err != nil {
264257
validationErrors = append(validationErrors,
265258
errors.IncorrectPathParamArrayBoolean(p, arrayValues[pv], sch, iSch))
266259
continue
267260
}
261+
if len(validationErrors) == bc {
262+
// ParseBool will parse 0 or 1 as false/true to we
263+
// need to catch this edge case.
264+
if arrayValues[pv] == "0" || arrayValues[pv] == "1" {
265+
validationErrors = append(validationErrors,
266+
errors.IncorrectPathParamArrayBoolean(p, arrayValues[pv], sch, iSch))
267+
continue
268+
}
269+
}
268270
}
269271
}
270272
}

requests/validate_body.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (v *requestBodyValidator) ValidateRequestBody(request *http.Request) (bool,
4444
required = *operation.RequestBody.Required
4545
}
4646
if contentType == "" {
47-
if !required{
47+
if !required {
4848
// request body is not required, the validation stop there.
4949
return true, nil
5050
}

0 commit comments

Comments
 (0)