1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
package openapi3filter_test
import (
"fmt"
"net/http"
"net/http/httptest"
"sort"
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/getkin/kin-openapi/routers/gorillamux"
)
func Example() {
doc, err := openapi3.NewLoader().LoadFromFile("./testdata/petstore.yaml")
if err != nil {
panic(err)
}
router, err := gorillamux.NewRouter(doc)
if err != nil {
panic(err)
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route, pathParams, err := router.FindRoute(r)
if err != nil {
fmt.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
err = openapi3filter.ValidateRequest(r.Context(), &openapi3filter.RequestValidationInput{
Request: r,
PathParams: pathParams,
Route: route,
Options: &openapi3filter.Options{
MultiError: true,
},
})
switch err := err.(type) {
case nil:
case openapi3.MultiError:
issues := convertError(err)
names := make([]string, 0, len(issues))
for k := range issues {
names = append(names, k)
}
sort.Strings(names)
for _, k := range names {
msgs := issues[k]
fmt.Println("===== Start New Error =====")
fmt.Println(k + ":")
for _, msg := range msgs {
fmt.Printf("\t%s\n", msg)
}
}
w.WriteHeader(http.StatusBadRequest)
default:
fmt.Println(err.Error())
w.WriteHeader(http.StatusBadRequest)
}
}))
defer ts.Close()
// (note invalid type for name and invalid status)
body := strings.NewReader(`{"name": 100, "photoUrls": [], "status": "invalidStatus"}`)
req, err := http.NewRequest("POST", ts.URL+"/pet?num=0", body)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Printf("response: %d %s\n", resp.StatusCode, resp.Body)
// Output:
// ===== Start New Error =====
// @body.name:
// Error at "/name": field must be set to string or not be present
// Schema:
// {
// "example": "doggie",
// "type": "string"
// }
//
// Value:
// "number, integer"
//
// ===== Start New Error =====
// @body.status:
// Error at "/status": value "invalidStatus" is not one of the allowed values
// Schema:
// {
// "description": "pet status in the store",
// "enum": [
// "available",
// "pending",
// "sold"
// ],
// "type": "string"
// }
//
// Value:
// "invalidStatus"
//
// ===== Start New Error =====
// query.num:
// parameter "num" in query has an error: number must be at least 1
// Schema:
// {
// "minimum": 1,
// "type": "integer"
// }
//
// Value:
// 0
//
// response: 400 {}
}
func convertError(me openapi3.MultiError) map[string][]string {
issues := make(map[string][]string)
for _, err := range me {
const prefixBody = "@body"
switch err := err.(type) {
case *openapi3.SchemaError:
// Can inspect schema validation errors here, e.g. err.Value
field := prefixBody
if path := err.JSONPointer(); len(path) > 0 {
field = fmt.Sprintf("%s.%s", field, strings.Join(path, "."))
}
issues[field] = append(issues[field], err.Error())
case *openapi3filter.RequestError: // possible there were multiple issues that failed validation
// check if invalid HTTP parameter
if err.Parameter != nil {
prefix := err.Parameter.In
name := fmt.Sprintf("%s.%s", prefix, err.Parameter.Name)
issues[name] = append(issues[name], err.Error())
continue
}
if err, ok := err.Err.(openapi3.MultiError); ok {
for k, v := range convertError(err) {
issues[k] = append(issues[k], v...)
}
continue
}
// check if requestBody
if err.RequestBody != nil {
issues[prefixBody] = append(issues[prefixBody], err.Error())
continue
}
default:
const unknown = "@unknown"
issues[unknown] = append(issues[unknown], err.Error())
}
}
return issues
}
|