Skip to content

Commit 45b4399

Browse files
crissi98ap7u5
and
ap7u5
authored
openapi3filter: add context to Validator Middleware's ErrFunc and LogFunc functions (#953)
* add context to Validator Middleware's ErrFunc and LogFunc functions * update existing ErrFunc and LogFunc instead of creating new ones and updated docs --------- Co-authored-by: ap7u5 <[email protected]>
1 parent f170f8c commit 45b4399

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

.github/docs/openapi3filter.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ type ErrCode int
182182
occur during validation. These may be used to write an appropriate response
183183
in ErrFunc.
184184

185-
type ErrFunc func(w http.ResponseWriter, status int, code ErrCode, err error)
185+
type ErrFunc func(ctx context.Context, w http.ResponseWriter, status int, code ErrCode, err error)
186186
ErrFunc handles errors that may occur during validation.
187187

188188
type ErrorEncoder func(ctx context.Context, err error, w http.ResponseWriter)
@@ -198,7 +198,7 @@ type Headerer interface {
198198
Headerer, the provided headers will be applied to the response writer,
199199
after the Content-Type is set.
200200

201-
type LogFunc func(message string, err error)
201+
type LogFunc func(ctx context.Context, message string, err error)
202202
LogFunc handles log messages that may occur during validation.
203203

204204
type Options struct {

openapi3filter/middleware.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package openapi3filter
22

33
import (
44
"bytes"
5+
"context"
56
"io"
67
"log"
78
"net/http"
@@ -19,10 +20,10 @@ type Validator struct {
1920
}
2021

2122
// ErrFunc handles errors that may occur during validation.
22-
type ErrFunc func(w http.ResponseWriter, status int, code ErrCode, err error)
23+
type ErrFunc func(ctx context.Context, w http.ResponseWriter, status int, code ErrCode, err error)
2324

2425
// LogFunc handles log messages that may occur during validation.
25-
type LogFunc func(message string, err error)
26+
type LogFunc func(ctx context.Context, message string, err error)
2627

2728
// ErrCode is used for classification of different types of errors that may
2829
// occur during validation. These may be used to write an appropriate response
@@ -61,10 +62,10 @@ func (e ErrCode) responseText() string {
6162
func NewValidator(router routers.Router, options ...ValidatorOption) *Validator {
6263
v := &Validator{
6364
router: router,
64-
errFunc: func(w http.ResponseWriter, status int, code ErrCode, _ error) {
65+
errFunc: func(_ context.Context, w http.ResponseWriter, status int, code ErrCode, _ error) {
6566
http.Error(w, code.responseText(), status)
6667
},
67-
logFunc: func(message string, err error) {
68+
logFunc: func(_ context.Context, message string, err error) {
6869
log.Printf("%s: %v", message, err)
6970
},
7071
}
@@ -117,10 +118,11 @@ func ValidationOptions(options Options) ValidatorOption {
117118
// request and response validation.
118119
func (v *Validator) Middleware(h http.Handler) http.Handler {
119120
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
121+
ctx := r.Context()
120122
route, pathParams, err := v.router.FindRoute(r)
121123
if err != nil {
122-
v.logFunc("validation error: failed to find route for "+r.URL.String(), err)
123-
v.errFunc(w, http.StatusNotFound, ErrCodeCannotFindRoute, err)
124+
v.logFunc(ctx, "validation error: failed to find route for "+r.URL.String(), err)
125+
v.errFunc(ctx, w, http.StatusNotFound, ErrCodeCannotFindRoute, err)
124126
return
125127
}
126128
requestValidationInput := &RequestValidationInput{
@@ -129,9 +131,9 @@ func (v *Validator) Middleware(h http.Handler) http.Handler {
129131
Route: route,
130132
Options: &v.options,
131133
}
132-
if err = ValidateRequest(r.Context(), requestValidationInput); err != nil {
133-
v.logFunc("invalid request", err)
134-
v.errFunc(w, http.StatusBadRequest, ErrCodeRequestInvalid, err)
134+
if err = ValidateRequest(ctx, requestValidationInput); err != nil {
135+
v.logFunc(ctx, "invalid request", err)
136+
v.errFunc(ctx, w, http.StatusBadRequest, ErrCodeRequestInvalid, err)
135137
return
136138
}
137139

@@ -144,22 +146,22 @@ func (v *Validator) Middleware(h http.Handler) http.Handler {
144146

145147
h.ServeHTTP(wr, r)
146148

147-
if err = ValidateResponse(r.Context(), &ResponseValidationInput{
149+
if err = ValidateResponse(ctx, &ResponseValidationInput{
148150
RequestValidationInput: requestValidationInput,
149151
Status: wr.statusCode(),
150152
Header: wr.Header(),
151153
Body: io.NopCloser(bytes.NewBuffer(wr.bodyContents())),
152154
Options: &v.options,
153155
}); err != nil {
154-
v.logFunc("invalid response", err)
156+
v.logFunc(ctx, "invalid response", err)
155157
if v.strict {
156-
v.errFunc(w, http.StatusInternalServerError, ErrCodeResponseInvalid, err)
158+
v.errFunc(ctx, w, http.StatusInternalServerError, ErrCodeResponseInvalid, err)
157159
}
158160
return
159161
}
160162

161163
if err = wr.flushBodyContents(); err != nil {
162-
v.logFunc("failed to write response", err)
164+
v.logFunc(ctx, "failed to write response", err)
163165
}
164166
})
165167
}

openapi3filter/middleware_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package openapi3filter_test
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -489,7 +490,7 @@ paths:
489490
// testing a service against its spec in development and CI. In production,
490491
// availability may be more important than strictness.
491492
v := openapi3filter.NewValidator(router, openapi3filter.Strict(true),
492-
openapi3filter.OnErr(func(w http.ResponseWriter, status int, code openapi3filter.ErrCode, err error) {
493+
openapi3filter.OnErr(func(_ context.Context, w http.ResponseWriter, status int, code openapi3filter.ErrCode, err error) {
493494
// Customize validation error responses to use JSON
494495
w.Header().Set("Content-Type", "application/json")
495496
w.WriteHeader(status)

0 commit comments

Comments
 (0)