Skip to content

Validation error in query parameter value reported as "@body" by example in unpack_errors_test.go #589

@gward

Description

@gward

I'm trying to write my own code to interpret validation errors returned by openapi3filter.ValidateRequest(), and I'm closely following the example convertError() in openapi3filter/unpack_errors_test.go. I've found a validation error that is not handled well by that example: if a query parameter has type integer and a minimum, then violating the minimum results in the error being reported as a body error.

Here's what I've done: first, add a query param to the spec with

diff --git a/openapi3filter/testdata/petstore.yaml b/openapi3filter/testdata/petstore.yaml
index e3b61bf..fa550bd 100644
--- a/openapi3filter/testdata/petstore.yaml
+++ b/openapi3filter/testdata/petstore.yaml
@@ -30,6 +30,12 @@ paths:
       summary: "Add a new pet to the store"
       description: ""
       operationId: "addPet"
+      parameters:
+        - name: num
+          in: query
+          schema:
+            type: integer
+            minimum: 1
       requestBody:
         required: true
         content: 

Now I modify the example code to violate the spec for that query param, and to expect a reasonably clear error message:

diff --git a/openapi3filter/unpack_errors_test.go b/openapi3filter/unpack_errors_test.go
index 4242177..66a1921 100644
--- a/openapi3filter/unpack_errors_test.go
+++ b/openapi3filter/unpack_errors_test.go
@@ -66,7 +66,7 @@ func Example() {
 
 	// (note invalid type for name and invalid status)
 	body := strings.NewReader(`{"name": 100, "photoUrls": [], "status": "invalidStatus"}`)
-	req, err := http.NewRequest("POST", ts.URL+"/pet", body)
+	req, err := http.NewRequest("POST", ts.URL+"/pet?num=0", body)
 	if err != nil {
 		panic(err)
 	}
@@ -108,6 +108,9 @@ func Example() {
 	// Value:
 	//   "invalidStatus"
 	//
+	// ===== Start New Error =====
+	// query.num:
+	//	parameter "num" in query has an error: value 0: number must be at least 1
 	// response: 400 {}
 }

I'm not picky about the exact error, but the above seems like a reasonable guess at what kin-openapi might return.

With this patch in place, the test fails because the actual output of the example does not match the expected output. Here is the test failure, edited to show the difference:

=== RUN   Example
--- FAIL: Example (0.00s)
got:
===== Start New Error =====
@body:
	number must be at least 1
Schema:
  {
    "minimum": 1,
    "type": "integer"
  }

Value:
  0

[...other validation errors...]

want:
[...other expected errors...]

===== Start New Error =====
query.num:
	parameter "num" in query has an error: value 0: number must be at least 1

Not clear if this is a problem in the validation code, or just a limitation of the example code. Either way, it would be nice if the example convertError() handled every type of validation error that kin-openapi can return.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions