Skip to content

Commit

Permalink
Deprecate existing middleware
Browse files Browse the repository at this point in the history
To nudge people to move along to the new versions in their own repos.
  • Loading branch information
jamietanna committed Sep 17, 2023
1 parent 2d349f4 commit 58e3d16
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
8 changes: 8 additions & 0 deletions pkg/chi-middleware/oapi_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ import (
)

// ErrorHandler is called when there is an error in validation
// Deprecated: This has been replaced by github.com/oapi-codegen/nethttp-middleware#ErrorHandler
type ErrorHandler func(w http.ResponseWriter, message string, statusCode int)

// MultiErrorHandler is called when oapi returns a MultiError type
// Deprecated: This has been replaced by github.com/oapi-codegen/nethttp-middleware#
type MultiErrorHandler func(openapi3.MultiError) (int, error)

// Options to customize request validation, openapi3filter specified options will be passed through.
// Deprecated: This has been replaced by github.com/oapi-codegen/nethttp-middleware#Options
type Options struct {
Options openapi3filter.Options
ErrorHandler ErrorHandler
Expand All @@ -33,12 +36,14 @@ type Options struct {

// OapiRequestValidator Creates middleware to validate request by swagger spec.
// This middleware is good for net/http either since go-chi is 100% compatible with net/http.
// Deprecated: This has been replaced by github.com/oapi-codegen/nethttp-middleware#OapiRequestValidator
func OapiRequestValidator(swagger *openapi3.T) func(next http.Handler) http.Handler {
return OapiRequestValidatorWithOptions(swagger, nil)
}

// OapiRequestValidatorWithOptions Creates middleware to validate request by swagger spec.
// This middleware is good for net/http either since go-chi is 100% compatible with net/http.
// Deprecated: This has been replaced by github.com/oapi-codegen/nethttp-middleware#OapiRequestValidatorWithOptions
func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) func(next http.Handler) http.Handler {
if swagger.Servers != nil && (options == nil || !options.SilenceServersWarning) {
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.")
Expand Down Expand Up @@ -71,6 +76,7 @@ func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) func

// validateRequest is called from the middleware above and actually does the work
// of validating a request.
// Deprecated: This has been replaced by github.com/oapi-codegen/nethttp-middleware#validateRequest
func validateRequest(r *http.Request, router routers.Router, options *Options) (int, error) {

// Find route
Expand Down Expand Up @@ -118,6 +124,7 @@ func validateRequest(r *http.Request, router routers.Router, options *Options) (

// attempt to get the MultiErrorHandler from the options. If it is not set,
// return a default handler
// Deprecated: This has been replaced by github.com/oapi-codegen/nethttp-middleware#getMultiErrorHandlerFromOptions
func getMultiErrorHandlerFromOptions(options *Options) MultiErrorHandler {
if options == nil {
return defaultMultiErrorHandler
Expand All @@ -133,6 +140,7 @@ func getMultiErrorHandlerFromOptions(options *Options) MultiErrorHandler {
// defaultMultiErrorHandler returns a StatusBadRequest (400) and a list
// of all the errors. This method is called if there are no other
// methods defined on the options.
// Deprecated: This has been replaced by github.com/oapi-codegen/nethttp-middleware#defaultMultiErrorHandler
func defaultMultiErrorHandler(me openapi3.MultiError) (int, error) {
return http.StatusBadRequest, me
}
11 changes: 11 additions & 0 deletions pkg/fiber-middleware/oapi_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ctxKeyFiberContext struct{}
type ctxKeyUserData struct{}

// OapiValidatorFromYamlFile creates a validator middleware from a YAML file path
// Deprecated: This has been replaced by github.com/oapi-codegen/fiber-middleware#OapiValidatorFromYamlFile
func OapiValidatorFromYamlFile(path string) (fiber.Handler, error) {

data, err := os.ReadFile(path)
Expand All @@ -42,18 +43,22 @@ func OapiValidatorFromYamlFile(path string) (fiber.Handler, error) {
// OapiRequestValidator is a fiber middleware function which validates incoming HTTP requests
// to make sure that they conform to the given OAPI 3.0 specification. When
// OAPI validation fails on the request, we return an HTTP/400 with error message
// Deprecated: This has been replaced by github.com/oapi-codegen/fiber-middleware#OapiRequestValidator
func OapiRequestValidator(swagger *openapi3.T) fiber.Handler {
return OapiRequestValidatorWithOptions(swagger, nil)
}

// ErrorHandler is called when there is an error in validation
// Deprecated: This has been replaced by github.com/oapi-codegen/fiber-middleware#ErrorHandler
type ErrorHandler func(c *fiber.Ctx, message string, statusCode int)

// MultiErrorHandler is called when oapi returns a MultiError type
// Deprecated: This has been replaced by github.com/oapi-codegen/fiber-middleware#MultiErrorHandler
type MultiErrorHandler func(openapi3.MultiError) error

// Options to customize request validation. These are passed through to
// openapi3filter.
// Deprecated: This has been replaced by github.com/oapi-codegen/fiber-middleware#Options
type Options struct {
Options openapi3filter.Options
ErrorHandler ErrorHandler
Expand All @@ -63,6 +68,7 @@ type Options struct {
}

// OapiRequestValidatorWithOptions creates a validator from a swagger object, with validation options
// Deprecated: This has been replaced by github.com/oapi-codegen/fiber-middleware#OapiRequestValidatorWithOptions
func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) fiber.Handler {

router, err := gorillamux.NewRouter(swagger)
Expand All @@ -89,6 +95,7 @@ func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) fibe

// ValidateRequestFromContext is called from the middleware above and actually does the work
// of validating a request.
// Deprecated: This has been replaced by github.com/oapi-codegen/fiber-middleware#
func ValidateRequestFromContext(c *fiber.Ctx, router routers.Router, options *Options) error {

r, err := adaptor.ConvertRequest(c, false)
Expand Down Expand Up @@ -157,6 +164,7 @@ func ValidateRequestFromContext(c *fiber.Ctx, router routers.Router, options *Op

// GetFiberContext gets the fiber context from within requests. It returns
// nil if not found or wrong type.
// Deprecated: This has been replaced by github.com/oapi-codegen/fiber-middleware#GetFiberContext
func GetFiberContext(c context.Context) *fiber.Ctx {
iface := c.Value(ctxKeyFiberContext{})
if iface == nil {
Expand All @@ -170,12 +178,14 @@ func GetFiberContext(c context.Context) *fiber.Ctx {
return nil
}

// Deprecated: This has been replaced by github.com/oapi-codegen/fiber-middleware#GetUserData
func GetUserData(c context.Context) interface{} {
return c.Value(ctxKeyUserData{})
}

// getMultiErrorHandlerFromOptions attempts to get the MultiErrorHandler from the options. If it is not set,
// return a default handler
// Deprecated: This has been replaced by github.com/oapi-codegen/fiber-middleware#getMultiErrorHandlerFromOptions
func getMultiErrorHandlerFromOptions(options *Options) MultiErrorHandler {
if options == nil {
return defaultMultiErrorHandler
Expand All @@ -191,6 +201,7 @@ func getMultiErrorHandlerFromOptions(options *Options) MultiErrorHandler {
// defaultMultiErrorHandler returns a StatusBadRequest (400) and a list
// of all the errors. This method is called if there are no other
// methods defined on the options.
// Deprecated: This has been replaced by github.com/oapi-codegen/fiber-middleware#defaultMultiErrorHandler
func defaultMultiErrorHandler(me openapi3.MultiError) error {
return fmt.Errorf("multiple errors encountered: %s", me)
}
16 changes: 13 additions & 3 deletions pkg/gin-middleware/oapi_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
)

// OapiValidatorFromYamlFile creates a validator middleware from a YAML file path
// Deprecated: This has been replaced by github.com/oapi-codegen/gin-middleware#OapiValidatorFromYamlFile
func OapiValidatorFromYamlFile(path string) (gin.HandlerFunc, error) {
data, err := os.ReadFile(path)
if err != nil {
Expand All @@ -53,18 +54,22 @@ func OapiValidatorFromYamlFile(path string) (gin.HandlerFunc, error) {
// OapiRequestValidator is an gin middleware function which validates incoming HTTP requests
// to make sure that they conform to the given OAPI 3.0 specification. When
// OAPI validation fails on the request, we return an HTTP/400 with error message
// Deprecated: This has been replaced by github.com/oapi-codegen/gin-middleware#OapiRequestValidator
func OapiRequestValidator(swagger *openapi3.T) gin.HandlerFunc {
return OapiRequestValidatorWithOptions(swagger, nil)
}

// ErrorHandler is called when there is an error in validation
// Deprecated: This has been replaced by github.com/oapi-codegen/gin-middleware#ErrorHandler
type ErrorHandler func(c *gin.Context, message string, statusCode int)

// MultiErrorHandler is called when oapi returns a MultiError type
// Deprecated: This has been replaced by github.com/oapi-codegen/gin-middleware#MultiErrorHandler
type MultiErrorHandler func(openapi3.MultiError) error

// Options to customize request validation. These are passed through to
// openapi3filter.
// Deprecated: This has been replaced by github.com/oapi-codegen/gin-middleware#Options
type Options struct {
ErrorHandler ErrorHandler
Options openapi3filter.Options
Expand All @@ -76,6 +81,7 @@ type Options struct {
}

// OapiRequestValidatorWithOptions creates a validator from a swagger object, with validation options
// Deprecated: This has been replaced by github.com/oapi-codegen/gin-middleware#OapiRequestValidatorWithOptions
func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) gin.HandlerFunc {
if swagger.Servers != nil && (options == nil || !options.SilenceServersWarning) {
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.")
Expand All @@ -94,9 +100,9 @@ func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) gin.
// in case the handler didn't internally call Abort, stop the chain
c.Abort()
} else if options != nil && options.ErrorHandler != nil {
options.ErrorHandler(c, err.Error(), http.StatusBadRequest)
// in case the handler didn't internally call Abort, stop the chain
c.Abort()
options.ErrorHandler(c, err.Error(), http.StatusBadRequest)
// in case the handler didn't internally call Abort, stop the chain
c.Abort()
} else if err.Error() == routers.ErrPathNotFound.Error() {
// note: i am not sure if this is the best way to handle this
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": err.Error()})
Expand All @@ -111,6 +117,7 @@ func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) gin.

// ValidateRequestFromContext is called from the middleware above and actually does the work
// of validating a request.
// Deprecated: This has been replaced by github.com/oapi-codegen/gin-middleware#ValidateRequestFromContext
func ValidateRequestFromContext(c *gin.Context, router routers.Router, options *Options) error {
req := c.Request
route, pathParams, err := router.FindRoute(req)
Expand Down Expand Up @@ -173,6 +180,7 @@ func ValidateRequestFromContext(c *gin.Context, router routers.Router, options *

// GetGinContext gets the echo context from within requests. It returns
// nil if not found or wrong type.
// Deprecated: This has been replaced by github.com/oapi-codegen/gin-middleware#GetGinContext
func GetGinContext(c context.Context) *gin.Context {
iface := c.Value(GinContextKey)
if iface == nil {
Expand All @@ -191,6 +199,7 @@ func GetUserData(c context.Context) interface{} {

// attempt to get the MultiErrorHandler from the options. If it is not set,
// return a default handler
// Deprecated: This has been replaced by github.com/oapi-codegen/gin-middleware#getMultiErrorHandlerFromOptions
func getMultiErrorHandlerFromOptions(options *Options) MultiErrorHandler {
if options == nil {
return defaultMultiErrorHandler
Expand All @@ -206,6 +215,7 @@ func getMultiErrorHandlerFromOptions(options *Options) MultiErrorHandler {
// defaultMultiErrorHandler returns a StatusBadRequest (400) and a list
// of all of the errors. This method is called if there are no other
// methods defined on the options.
// Deprecated: This has been replaced by github.com/oapi-codegen/gin-middleware#defaultMultiErrorHandler
func defaultMultiErrorHandler(me openapi3.MultiError) error {
return fmt.Errorf("multiple errors encountered: %s", me)
}
11 changes: 11 additions & 0 deletions pkg/iris-middleware/oapi_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
)

// OapiValidatorFromYamlFile creates a validator middleware from a YAML file path
// Deprecated: This has been replaced by github.com/oapi-codegen/iris-middleware#OapiValidatorFromYamlFile
func OapiValidatorFromYamlFile(path string) (iris.Handler, error) {
data, err := os.ReadFile(path)
if err != nil {
Expand All @@ -39,18 +40,22 @@ func OapiValidatorFromYamlFile(path string) (iris.Handler, error) {
// OapiRequestValidator is a iris middleware function which validates incoming HTTP requests
// to make sure that they conform to the given OAPI 3.0 specification. When
// OAPI validation fails on the request, we return an HTTP/400 with error message
// Deprecated: This has been replaced by github.com/oapi-codegen/iris-middleware#OapiRequestValidator
func OapiRequestValidator(swagger *openapi3.T) iris.Handler {
return OapiRequestValidatorWithOptions(swagger, nil)
}

// ErrorHandler is called when there is an error in validation
// Deprecated: This has been replaced by github.com/oapi-codegen/iris-middleware#ErrorHandler
type ErrorHandler func(ctx iris.Context, message string, statusCode int)

// MultiErrorHandler is called when oapi returns a MultiError type
// Deprecated: This has been replaced by github.com/oapi-codegen/iris-middleware#MultiErrorHandler
type MultiErrorHandler func(openapi3.MultiError) error

// Options to customize request validation. These are passed through to
// openapi3filter.
// Deprecated: This has been replaced by github.com/oapi-codegen/iris-middleware#Options
type Options struct {
Options openapi3filter.Options
ErrorHandler ErrorHandler
Expand All @@ -62,6 +67,7 @@ type Options struct {
}

// OapiRequestValidatorWithOptions creates a validator from a swagger object, with validation options
// Deprecated: This has been replaced by github.com/oapi-codegen/iris-middleware#OapiRequestValidatorWithOptions
func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) iris.Handler {
router, err := gorillamux.NewRouter(swagger)
if err != nil {
Expand All @@ -83,6 +89,7 @@ func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) iris

// ValidateRequestFromContext is called from the middleware above and actually does the work
// of validating a request.
// Deprecated: This has been replaced by github.com/oapi-codegen/iris-middleware#ValidateRequestFromContext
func ValidateRequestFromContext(ctx iris.Context, router routers.Router, options *Options) error {
req := ctx.Request()
route, pathParams, err := router.FindRoute(req)
Expand Down Expand Up @@ -145,6 +152,7 @@ func ValidateRequestFromContext(ctx iris.Context, router routers.Router, options

// GetIrisContext gets the iris context from within requests. It returns
// nil if not found or wrong type.
// Deprecated: This has been replaced by github.com/oapi-codegen/iris-middleware#GetIrisContext
func GetIrisContext(ctx context.Context) iris.Context {
iface := ctx.Value(IrisContextKey)
if iface == nil {
Expand All @@ -158,12 +166,14 @@ func GetIrisContext(ctx context.Context) iris.Context {
return nil
}

// Deprecated: This has been replaced by github.com/oapi-codegen/iris-middleware#GetUserData
func GetUserData(ctx context.Context) interface{} {
return ctx.Value(UserDataKey)
}

// getMultiErrorHandlerFromOptions attempts to get the MultiErrorHandler from the options. If it is not set,
// return a default handler
// Deprecated: This has been replaced by github.com/oapi-codegen/iris-middleware#getMultiErrorHandlerFromOptions
func getMultiErrorHandlerFromOptions(options *Options) MultiErrorHandler {
if options == nil {
return defaultMultiErrorHandler
Expand All @@ -179,6 +189,7 @@ func getMultiErrorHandlerFromOptions(options *Options) MultiErrorHandler {
// defaultMultiErrorHandler returns a StatusBadRequest (400) and a list
// of all the errors. This method is called if there are no other
// methods defined on the options.
// Deprecated: This has been replaced by github.com/oapi-codegen/iris-middleware#defaultMultiErrorHandler
func defaultMultiErrorHandler(me openapi3.MultiError) error {
return fmt.Errorf("multiple errors encountered: %s", me)
}
Loading

0 comments on commit 58e3d16

Please sign in to comment.