Skip to content

Commit 6c80420

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 c7be8fd commit 6c80420

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

pkg/chi-middleware/oapi_validate.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"errors"
99
"fmt"
10+
"log"
1011
"net/http"
1112
"strings"
1213

@@ -27,6 +28,8 @@ type Options struct {
2728
Options openapi3filter.Options
2829
ErrorHandler ErrorHandler
2930
MultiErrorHandler MultiErrorHandler
31+
// SilenceServersWarning allows silencing a warning for https://github.com/deepmap/oapi-codegen/issues/882 that reports when an OpenAPI spec has `spec.Servers != nil`
32+
SilenceServersWarning bool
3033
}
3134

3235
// OapiRequestValidator Creates middleware to validate request by swagger spec.
@@ -38,6 +41,10 @@ func OapiRequestValidator(swagger *openapi3.T) func(next http.Handler) http.Hand
3841
// OapiRequestValidatorWithOptions Creates middleware to validate request by swagger spec.
3942
// This middleware is good for net/http either since go-chi is 100% compatible with net/http.
4043
func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) func(next http.Handler) http.Handler {
44+
if swagger.Servers != nil && (options == nil || options.SilenceServersWarning) {
45+
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.")
46+
}
47+
4148
router, err := gorillamux.NewRouter(swagger)
4249
if err != nil {
4350
panic(err)

pkg/gin-middleware/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"
@@ -70,10 +71,16 @@ type Options struct {
7071
ParamDecoder openapi3filter.ContentParameterDecoder
7172
UserData interface{}
7273
MultiErrorHandler MultiErrorHandler
74+
// SilenceServersWarning allows silencing a warning for https://github.com/deepmap/oapi-codegen/issues/882 that reports when an OpenAPI spec has `spec.Servers != nil`
75+
SilenceServersWarning bool
7376
}
7477

7578
// OapiRequestValidatorWithOptions creates a validator from a swagger object, with validation options
7679
func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) gin.HandlerFunc {
80+
if swagger.Servers != nil && (options == nil || options.SilenceServersWarning) {
81+
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.")
82+
}
83+
7784
router, err := gorillamux.NewRouter(swagger)
7885
if err != nil {
7986
panic(err)

pkg/middleware/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)