Skip to content

Commit a923c76

Browse files
authored
Warn when using a non-empty spec.Servers (#883)
It's a fairly common mistake when using the OpenAPI request validation filter from kin-openapi to receive `no matching operation was found` errors if using the `servers` directive in your OpenAPI specification. This is a valid usescase, but you may not always want to be enforcing the validation that the `Host` header matches one of the `servers` mentioned in your specification. This makes it clearer to operators that there may be a configuration issue here, as a way to avoid as many folks falling into the same trap that many have before. This also provides an option to silence the warning, as logging out can be annoying and costly. Closes #882.
1 parent c1f722e commit a923c76

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

oapi_validate.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"errors"
2020
"fmt"
21+
"log"
2122
"net/http"
2223
"os"
2324
"strings"
@@ -73,10 +74,16 @@ type Options struct {
7374
UserData interface{}
7475
Skipper echomiddleware.Skipper
7576
MultiErrorHandler MultiErrorHandler
77+
// SilenceServersWarning allows silencing a warning for https://github.com/deepmap/oapi-codegen/issues/882 that reports when an OpenAPI spec has `spec.Servers != nil`
78+
SilenceServersWarning bool
7679
}
7780

7881
// OapiRequestValidatorWithOptions creates a validator from a swagger object, with validation options
7982
func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) echo.MiddlewareFunc {
83+
if swagger.Servers != nil && (options == nil || options.SilenceServersWarning) {
84+
log.Println("WARN: OapiRequestValidatorWithOptions called with an OpenAPI spec that has `Servers` set. This may lead to an HTTP 400 with `no matching operation was found` when sending a valid request, as the validator performs `Host` header validation. If you're expecting `Host` header validation, you can silence this warning by setting `Options.SilenceServersWarning = true`. See https://github.com/deepmap/oapi-codegen/issues/882 for more information.")
85+
}
86+
8087
router, err := gorillamux.NewRouter(swagger)
8188
if err != nil {
8289
panic(err)

0 commit comments

Comments
 (0)