Skip to content

Commit

Permalink
fix bad error message on invalid value parse on query parameter (#541)
Browse files Browse the repository at this point in the history
Co-authored-by: Kanda <kanda@synctera.com>
  • Loading branch information
kandaaaaa and Kanda committed May 30, 2022
1 parent c35b46e commit 770fcc5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
6 changes: 3 additions & 3 deletions openapi3filter/req_resp_decoder.go
Expand Up @@ -795,19 +795,19 @@ func parsePrimitive(raw string, schema *openapi3.SchemaRef) (interface{}, error)
case "integer":
v, err := strconv.ParseFloat(raw, 64)
if err != nil {
return nil, &ParseError{Kind: KindInvalidFormat, Value: raw, Reason: "an invalid integer", Cause: err}
return nil, &ParseError{Kind: KindInvalidFormat, Value: raw, Reason: "an invalid " + schema.Value.Type, Cause: err.(*strconv.NumError).Err}
}
return v, nil
case "number":
v, err := strconv.ParseFloat(raw, 64)
if err != nil {
return nil, &ParseError{Kind: KindInvalidFormat, Value: raw, Reason: "an invalid number", Cause: err}
return nil, &ParseError{Kind: KindInvalidFormat, Value: raw, Reason: "an invalid " + schema.Value.Type, Cause: err.(*strconv.NumError).Err}
}
return v, nil
case "boolean":
v, err := strconv.ParseBool(raw)
if err != nil {
return nil, &ParseError{Kind: KindInvalidFormat, Value: raw, Reason: "an invalid number", Cause: err}
return nil, &ParseError{Kind: KindInvalidFormat, Value: raw, Reason: "an invalid " + schema.Value.Type, Cause: err.(*strconv.NumError).Err}
}
return v, nil
case "string":
Expand Down
5 changes: 2 additions & 3 deletions openapi3filter/validation_error_test.go
Expand Up @@ -187,16 +187,15 @@ func getValidationTests(t *testing.T) []*validationTest {
Title: `parameter "status" in query is required`},
},
{
name: "error - wrong query string parameter type",
name: "error - wrong query string parameter type as integer",
args: validationArgs{
r: newPetstoreRequest(t, http.MethodGet, "/pet/findByIds?ids=1,notAnInt", nil),
},
wantErrParam: "ids",
wantErrParamIn: "query",
// This is a nested ParseError. The outer error is a KindOther with no details.
// So we'd need to look at the inner one which is a KindInvalidFormat. So just check the error body.
wantErrBody: `parameter "ids" in query has an error: path 1: value notAnInt: an invalid integer: ` +
"strconv.ParseFloat: parsing \"notAnInt\": invalid syntax",
wantErrBody: `parameter "ids" in query has an error: path 1: value notAnInt: an invalid integer: invalid syntax`,
// TODO: Should we treat query params of the wrong type like a 404 instead of a 400?
wantErrResponse: &ValidationError{Status: http.StatusBadRequest,
Title: `parameter "ids" in query is invalid: notAnInt is an invalid integer`},
Expand Down

0 comments on commit 770fcc5

Please sign in to comment.