Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bad error message on invalid value parse on query parameter #541

Merged
merged 1 commit into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions openapi3filter/req_resp_decoder.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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