Skip to content

Commit 83818fa

Browse files
committed
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.
1 parent f21cdce commit 83818fa

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

oapi_validate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,18 @@ type Options struct {
8282
MultiErrorHandler MultiErrorHandler
8383
// SilenceServersWarning allows silencing a warning for https://github.com/oapi-codegen/oapi-codegen/issues/882 that reports when an OpenAPI spec has `spec.Servers != nil`
8484
SilenceServersWarning bool
85+
// DoNotValidateServers ensures that there is no Host validation performed (see `SilenceServersWarning` and https://github.com/deepmap/oapi-codegen/issues/882 for more details)
86+
DoNotValidateServers bool
8587
}
8688

8789
// OapiRequestValidatorWithOptions Creates the middleware to validate that incoming requests match the given OpenAPI 3.x spec, allowing explicit configuration.
8890
//
8991
// NOTE that this may panic if the OpenAPI spec isn't valid, or if it cannot be used to create the middleware
9092
func OapiRequestValidatorWithOptions(spec *openapi3.T, options *Options) echo.MiddlewareFunc {
93+
if options != nil && options.DoNotValidateServers {
94+
spec.Servers = nil
95+
}
96+
9197
if spec.Servers != nil && (options == nil || !options.SilenceServersWarning) {
9298
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/oapi-codegen/oapi-codegen/issues/882 for more information.")
9399
}

0 commit comments

Comments
 (0)