Skip to content

Commit c1c5bb1

Browse files
author
Jamie Tanna
committed
sq
1 parent a488f2c commit c1c5bb1

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

oapi_validate_example_test.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"net/http"
1010
"net/http/httptest"
1111
"reflect"
12-
"strings"
1312

1413
"github.com/getkin/kin-openapi/openapi3"
1514
"github.com/getkin/kin-openapi/openapi3filter"
@@ -357,6 +356,10 @@ paths:
357356
id:
358357
type: string
359358
minLength: 100
359+
name:
360+
type: string
361+
enum:
362+
- Marcin
360363
additionalProperties: false
361364
/protected_resource:
362365
get:
@@ -468,10 +471,6 @@ components:
468471
case *openapi3filter.RequestError:
469472
out := fmt.Sprintf("A RequestError was returned when attempting to validate the request to %s %s: %s\n", opts.MatchedRoute.Route.Method, opts.MatchedRoute.Route.Path, e.Error())
470473

471-
fmt.Printf("e: %#v\n", e)
472-
// fmt.Printf("ErrorHandlerWithOpts: An HTTP %d was returned by the middleware with error message: request body has an error: value is required but missing TODO TODO TODO\n", opts.StatusCode)
473-
// http.Error(w, e.Error(), opts.StatusCode)
474-
475474
if e.RequestBody != nil {
476475
out += "This operation has a request body, which was "
477476
if !e.RequestBody.Required {
@@ -480,6 +479,9 @@ components:
480479
out += "required\n"
481480
}
482481

482+
fmt.Printf("e: %#v\n", e)
483+
fmt.Printf("e.Err: %#v\n", e.Err)
484+
483485
if childErr := e.Unwrap(); childErr != nil {
484486
out += "There was a child error, which was "
485487
switch e := childErr.(type) {
@@ -492,7 +494,7 @@ components:
492494

493495
fmt.Printf("ErrorHandlerWithOpts: %s\n", out)
494496

495-
http.Error(w, "TODO TODO TODO", opts.StatusCode)
497+
http.Error(w, "A bad request was made - but I'm not going to tell you where or how", opts.StatusCode)
496498
return
497499
}
498500

@@ -528,7 +530,15 @@ components:
528530
// ================================================================================
529531
fmt.Println("# A request that is malformed is rejected with HTTP 400 Bad Request (with an invalid request body), and is then logged by the ErrorHandlerWithOpts")
530532

531-
req, err = http.NewRequest(http.MethodPost, "/resource", strings.NewReader(`{"id":"not-long-enough"}`))
533+
body := map[string]string{
534+
"id": "not-long-enough",
535+
"name": "Jamie",
536+
}
537+
538+
data, err := json.Marshal(body)
539+
must(err)
540+
541+
req, err = http.NewRequest(http.MethodPost, "/resource", bytes.NewReader(data))
532542
must(err)
533543
req.Header.Set("Content-Type", "application/json")
534544

@@ -570,9 +580,11 @@ components:
570580

571581
// Output:
572582
// # A request that is malformed is rejected with HTTP 400 Bad Request (with no request body), and is then logged by the ErrorHandlerWithOpts
573-
// ErrorHandlerWithOpts: An HTTP 400 was returned by the middleware with error message: request body has an error: value is required but missing TODO TODO TODO
583+
// ErrorHandlerWithOpts: A RequestError was returned when attempting to validate the request to POST /resource: request body has an error: value is required but missing
584+
// This operation has a request body, which was required
585+
// There was a child error, which was an unknown type (*errors.errorString)
574586
// Received an HTTP 400 response. Expected HTTP 400
575-
// Response body: This was rewritten by the ErrorHandlerWithOpts TODO
587+
// Response body: A bad request was made - but I'm not going to tell you where or how
576588
//
577589
// # A request that is malformed is rejected with HTTP 400 Bad Request (with an invalid request body), and is then logged by the ErrorHandlerWithOpts
578590
// ErrorHandlerWithOpts: A RequestError was returned when attempting to validate the request to POST /resource: request body has an error: doesn't match schema: Error at "/id": minimum string length is 100
@@ -585,12 +597,13 @@ components:
585597
// Value:
586598
// "not-long-enough"
587599
//
588-
//
600+
// This operation has a request body, which was required
601+
// There was a child error, which was a SchemaError, which failed to validate on the minLength field
589602
// Received an HTTP 400 response. Expected HTTP 400
590-
// Response body: This was rewritten by the ErrorHandlerWithOpts TODO
603+
// Response body: A bad request was made - but I'm not going to tell you where or how
591604
//
592605
// # A request that to an unknown path is rejected with HTTP 404 Not Found, and is then logged by the ErrorHandlerWithOpts
593-
// ErrorHandlerWithOpts: An HTTP 400 was returned by the middleware with error message: no matching operation was found
606+
// ErrorHandlerWithOpts: An HTTP 404 was returned by the middleware with error message: no matching operation was found
594607
// Received an HTTP 404 response. Expected HTTP 404
595608
// Response body: No route was found (according to ErrorHandlerWithOpts), and we changed the HTTP status code to 404
596609
//

0 commit comments

Comments
 (0)