Skip to content

Commit ba47f30

Browse files
sorintmd-sauer
authored andcommitted
Expose request/response validation options in the middleware Validator (getkin#608)
1 parent 69e5596 commit ba47f30

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

openapi3filter/middleware.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Validator struct {
1616
errFunc ErrFunc
1717
logFunc LogFunc
1818
strict bool
19+
options Options
1920
}
2021

2122
// ErrFunc handles errors that may occur during validation.
@@ -106,6 +107,13 @@ func Strict(strict bool) ValidatorOption {
106107
}
107108
}
108109

110+
// ValidationOptions sets request/response validation options on the validator.
111+
func ValidationOptions(options Options) ValidatorOption {
112+
return func(v *Validator) {
113+
v.options = options
114+
}
115+
}
116+
109117
// Middleware returns an http.Handler which wraps the given handler with
110118
// request and response validation.
111119
func (v *Validator) Middleware(h http.Handler) http.Handler {
@@ -120,6 +128,7 @@ func (v *Validator) Middleware(h http.Handler) http.Handler {
120128
Request: r,
121129
PathParams: pathParams,
122130
Route: route,
131+
Options: &v.options,
123132
}
124133
if err = ValidateRequest(r.Context(), requestValidationInput); err != nil {
125134
v.logFunc("invalid request", err)
@@ -141,6 +150,7 @@ func (v *Validator) Middleware(h http.Handler) http.Handler {
141150
Status: wr.statusCode(),
142151
Header: wr.Header(),
143152
Body: ioutil.NopCloser(bytes.NewBuffer(wr.bodyContents())),
153+
Options: &v.options,
144154
}); err != nil {
145155
v.logFunc("invalid response", err)
146156
if v.strict {

openapi3filter/middleware_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,27 @@ func TestValidator(t *testing.T) {
328328
body: `{"id": "42", "contents": {"name": "foo", "expected": 9, "actual": 10}, "extra": true}`,
329329
},
330330
strict: false,
331+
}, {
332+
name: "POST response status code not in spec (return 200, spec only has 201)",
333+
handler: validatorTestHandler{
334+
postBody: `{"id": "42", "contents": {"name": "foo", "expected": 9, "actual": 10}, "extra": true}`,
335+
errStatusCode: 200,
336+
errBody: `{"id": "42", "contents": {"name": "foo", "expected": 9, "actual": 10}, "extra": true}`,
337+
}.withDefaults(),
338+
options: []openapi3filter.ValidatorOption{openapi3filter.ValidationOptions(openapi3filter.Options{
339+
IncludeResponseStatus: true,
340+
})},
341+
request: testRequest{
342+
method: "POST",
343+
path: "/test?version=1",
344+
body: `{"name": "foo", "expected": 9, "actual": 10}`,
345+
contentType: "application/json",
346+
},
347+
response: testResponse{
348+
statusCode: 200,
349+
body: `{"id": "42", "contents": {"name": "foo", "expected": 9, "actual": 10}, "extra": true}`,
350+
},
351+
strict: false,
331352
}}
332353
for i, test := range tests {
333354
t.Logf("test#%d: %s", i, test.name)

0 commit comments

Comments
 (0)