Repository: getkin/kin-openapi
Pull request #608 (2022-09-20 16:17:17)

Expose request/response validation options in the middleware Validator
Added new ValidatorOptions funtion to pass the validation options.

The context is that while ValidateRequest and ValidateResponse have configurable Options exposed through their input objects, the same Options cannot be passed when using the middleware/Validator approach.
The usecase that triggered the change was the validation of a response status code that is not in the API spec. ValidateResponse would check for it if the right option was passed, but there was no way to do it through the Validator.


Patch:
diff --git a/openapi3filter/middleware.go b/openapi3filter/middleware.go
index 3709faf..3bcb9db 100644
--- a/openapi3filter/middleware.go
+++ b/openapi3filter/middleware.go
@@ -16,6 +16,7 @@ type Validator struct {
 	errFunc ErrFunc
 	logFunc LogFunc
 	strict  bool
+	options Options
 }
 
 // ErrFunc handles errors that may occur during validation.
@@ -106,6 +107,13 @@ func Strict(strict bool) ValidatorOption {
 	}
 }
 
+// ValidationOptions sets request/response validation options on the validator.
+func ValidationOptions(options Options) ValidatorOption {
+	return func(v *Validator) {
+		v.options = options
+	}
+}
+
 // Middleware returns an http.Handler which wraps the given handler with
 // request and response validation.
 func (v *Validator) Middleware(h http.Handler) http.Handler {
@@ -120,6 +128,7 @@ func (v *Validator) Middleware(h http.Handler) http.Handler {
 			Request:    r,
 			PathParams: pathParams,
 			Route:      route,
+			Options:    &v.options,
 		}
 		if err = ValidateRequest(r.Context(), requestValidationInput); err != nil {
 			v.logFunc("invalid request", err)
@@ -141,6 +150,7 @@ func (v *Validator) Middleware(h http.Handler) http.Handler {
 			Status:                 wr.statusCode(),
 			Header:                 wr.Header(),
 			Body:                   ioutil.NopCloser(bytes.NewBuffer(wr.bodyContents())),
+			Options:                &v.options,
 		}); err != nil {
 			v.logFunc("invalid response", err)
 			if v.strict {


Test patch:
diff --git a/openapi3filter/middleware_test.go b/openapi3filter/middleware_test.go
index c6a5e9b..ff6059c 100644
--- a/openapi3filter/middleware_test.go
+++ b/openapi3filter/middleware_test.go
@@ -328,6 +328,27 @@ func TestValidator(t *testing.T) {
 			body:       `{"id": "42", "contents": {"name": "foo", "expected": 9, "actual": 10}, "extra": true}`,
 		},
 		strict: false,
+	}, {
+		name: "POST response status code not in spec (return 200, spec only has 201)",
+		handler: validatorTestHandler{
+			postBody:      `{"id": "42", "contents": {"name": "foo", "expected": 9, "actual": 10}, "extra": true}`,
+			errStatusCode: 200,
+			errBody:       `{"id": "42", "contents": {"name": "foo", "expected": 9, "actual": 10}, "extra": true}`,
+		}.withDefaults(),
+		options: []openapi3filter.ValidatorOption{openapi3filter.ValidationOptions(openapi3filter.Options{
+			IncludeResponseStatus: true,
+		})},
+		request: testRequest{
+			method:      "POST",
+			path:        "/test?version=1",
+			body:        `{"name": "foo", "expected": 9, "actual": 10}`,
+			contentType: "application/json",
+		},
+		response: testResponse{
+			statusCode: 200,
+			body:       `{"id": "42", "contents": {"name": "foo", "expected": 9, "actual": 10}, "extra": true}`,
+		},
+		strict: false,
 	}}
 	for i, test := range tests {
 		t.Logf("test#%d: %s", i, test.name)