You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: oapi_validate_example_test.go
+207-5Lines changed: 207 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -479,9 +479,6 @@ components:
479
479
out+="required\n"
480
480
}
481
481
482
-
fmt.Printf("e: %#v\n", e)
483
-
fmt.Printf("e.Err: %#v\n", e.Err)
484
-
485
482
ifchildErr:=e.Unwrap(); childErr!=nil {
486
483
out+="There was a child error, which was "
487
484
switche:=childErr.(type) {
@@ -531,8 +528,7 @@ components:
531
528
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")
532
529
533
530
body:=map[string]string{
534
-
"id": "not-long-enough",
535
-
"name": "Jamie",
531
+
"id": "not-long-enough",
536
532
}
537
533
538
534
data, err:=json.Marshal(body)
@@ -617,3 +613,209 @@ components:
617
613
// Received an HTTP 401 response. Expected HTTP 401
// NOTE that we need to make sure that the `Servers` aren't set, otherwise the OpenAPI validation middleware will validate that the `Host` header (of incoming requests) are targeting known `Servers` in the OpenAPI spec
errorHandlerFunc:=func(ctx context.Context, w http.ResponseWriter, r*http.Request, opts middleware.ErrorHandlerOpts) {
695
+
err:=opts.Error
696
+
697
+
ifopts.MatchedRoute==nil {
698
+
fmt.Printf("ErrorHandlerWithOpts: An HTTP %d was returned by the middleware with error message: %s\n", opts.StatusCode, err.Error())
699
+
700
+
// NOTE that you may want to override the default (an HTTP 400 Bad Request) to an HTTP 404 Not Found (or maybe an HTTP 405 Method Not Allowed, depending on what the requested resource was)
701
+
http.Error(w, fmt.Sprintf("No route was found (according to ErrorHandlerWithOpts), and we changed the HTTP status code to %d", http.StatusNotFound), http.StatusNotFound)
702
+
return
703
+
}
704
+
705
+
switche:=err.(type) {
706
+
case*openapi3filter.SecurityRequirementsError:
707
+
out:=fmt.Sprintf("A SecurityRequirementsError was returned when attempting to authenticate the request to %s %s against %d Security Schemes: %s\n", opts.MatchedRoute.Route.Method, opts.MatchedRoute.Route.Path, len(e.SecurityRequirements), e.Error())
708
+
for_, sr:=rangee.SecurityRequirements {
709
+
fork, v:=rangesr {
710
+
out+=fmt.Sprintf("- %s: %v\n", k, v)
711
+
}
712
+
}
713
+
714
+
fmt.Printf("ErrorHandlerWithOpts: %s\n", out)
715
+
716
+
http.Error(w, "You're not allowed!", opts.StatusCode)
717
+
return
718
+
case*openapi3filter.RequestError:
719
+
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())
720
+
721
+
ife.RequestBody!=nil {
722
+
out+="This operation has a request body, which was "
723
+
if!e.RequestBody.Required {
724
+
out+="not "
725
+
}
726
+
out+="required\n"
727
+
}
728
+
729
+
ifchildErr:=e.Unwrap(); childErr!=nil {
730
+
out+="There was a child error, which was "
731
+
switche:=childErr.(type) {
732
+
case*openapi3.SchemaError:
733
+
out+="a SchemaError, which failed to validate on the "+e.SchemaField+" field"
734
+
default:
735
+
out+="an unknown type ("+reflect.TypeOf(e).String() +")"
736
+
}
737
+
}
738
+
739
+
fmt.Printf("ErrorHandlerWithOpts: %s\n", out)
740
+
741
+
http.Error(w, "A bad request was made - but I'm not going to tell you where or how", opts.StatusCode)
fmt.Println("# A request that is malformed is rejected with HTTP 400 Bad Request (with no request body), and is then logged by the ErrorHandlerWithOpts")
fmt.Println("# A request that is malformed is rejected with HTTP 400 Bad Request (with an invalid request body, with multiple issues), and is then logged by the ErrorHandlerWithOpts")
fmt.Printf("Received an HTTP %d response. Expected HTTP 400\n", rr.Code)
795
+
logResponseBody(rr)
796
+
fmt.Println()
797
+
798
+
// Output:
799
+
// # A request that is malformed is rejected with HTTP 400 Bad Request (with no request body), and is then logged by the ErrorHandlerWithOpts
800
+
// ErrorHandlerWithOpts: A RequestError was returned when attempting to validate the request to POST /resource: request body has an error: value is required but missing
801
+
// This operation has a request body, which was required
802
+
// There was a child error, which was an unknown type (*errors.errorString)
803
+
// Received an HTTP 400 response. Expected HTTP 400
804
+
// Response body: A bad request was made - but I'm not going to tell you where or how
805
+
//
806
+
// # A request that is malformed is rejected with HTTP 400 Bad Request (with an invalid request body), and is then logged by the ErrorHandlerWithOpts
807
+
// 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
808
+
// Schema:
809
+
// {
810
+
// "minLength": 100,
811
+
// "type": "string"
812
+
// }
813
+
//
814
+
// Value:
815
+
// "not-long-enough"
816
+
//
817
+
// This operation has a request body, which was required
818
+
// There was a child error, which was a SchemaError, which failed to validate on the minLength field
819
+
// Received an HTTP 400 response. Expected HTTP 400
820
+
// Response body: A bad request was made - but I'm not going to tell you where or how
0 commit comments