From 770fcc5473f2dfa270d12fba59836523f3ddb195 Mon Sep 17 00:00:00 2001 From: K Zhang Date: Mon, 30 May 2022 12:13:22 -0400 Subject: [PATCH] fix bad error message on invalid value parse on query parameter (#541) Co-authored-by: Kanda --- openapi3filter/req_resp_decoder.go | 6 +++--- openapi3filter/validation_error_test.go | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/openapi3filter/req_resp_decoder.go b/openapi3filter/req_resp_decoder.go index 0408d8da3..94edf75a1 100644 --- a/openapi3filter/req_resp_decoder.go +++ b/openapi3filter/req_resp_decoder.go @@ -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": diff --git a/openapi3filter/validation_error_test.go b/openapi3filter/validation_error_test.go index aae26e56b..10d46f855 100644 --- a/openapi3filter/validation_error_test.go +++ b/openapi3filter/validation_error_test.go @@ -187,7 +187,7 @@ 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), }, @@ -195,8 +195,7 @@ func getValidationTests(t *testing.T) []*validationTest { 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`},