Skip to content

Commit a7a0f51

Browse files
Jamie Tannajamietanna
authored andcommitted
feat: allow not validating Servers at all
As some folks may not want to perform the workaround of having to mangle their `spec.Servers`, as well as removing potentially noisy log lines. Closes #26.
1 parent c74e132 commit a7a0f51

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

oapi_validate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ type Options struct {
4040
MultiErrorHandler MultiErrorHandler
4141
// SilenceServersWarning allows silencing a warning for https://github.com/deepmap/oapi-codegen/issues/882 that reports when an OpenAPI spec has `spec.Servers != nil`
4242
SilenceServersWarning bool
43+
// DoNotValidateServers ensures that there is no Host validation performed (see `SilenceServersWarning` and https://github.com/deepmap/oapi-codegen/issues/882 for more details)
44+
DoNotValidateServers bool
4345
}
4446

4547
// OapiRequestValidator Creates the middleware to validate that incoming requests match the given OpenAPI 3.x spec, with a default set of configuration.
@@ -51,6 +53,10 @@ func OapiRequestValidator(spec *openapi3.T) func(next http.Handler) http.Handler
5153
//
5254
// NOTE that this may panic if the OpenAPI spec isn't valid, or if it cannot be used to create the middleware
5355
func OapiRequestValidatorWithOptions(spec *openapi3.T, options *Options) func(next http.Handler) http.Handler {
56+
if options != nil && options.DoNotValidateServers {
57+
spec.Servers = nil
58+
}
59+
5460
if spec.Servers != nil && (options == nil || !options.SilenceServersWarning) {
5561
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.")
5662
}

0 commit comments

Comments
 (0)