diff --git a/pkg/codegen/templates/gin/gin-register.tmpl b/pkg/codegen/templates/gin/gin-register.tmpl index 5362dd7ab..cdd11e869 100644 --- a/pkg/codegen/templates/gin/gin-register.tmpl +++ b/pkg/codegen/templates/gin/gin-register.tmpl @@ -1,21 +1,26 @@ // GinServerOptions provides options for the Gin server. type GinServerOptions struct { BaseURL string - Middlewares []MiddlewareFunc + Middlewares []gin.HandlerFunc } +// keeping for backward compatibility +type MiddlewareFunc = gin.HandlerFunc + // RegisterHandlers creates http.Handler with routing matching OpenAPI spec. -func RegisterHandlers(router *gin.Engine, si ServerInterface) *gin.Engine { +func RegisterHandlers(router gin.IRouter, si ServerInterface) gin.IRouter { return RegisterHandlersWithOptions(router, si, GinServerOptions{}) } // RegisterHandlersWithOptions creates http.Handler with additional options -func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options GinServerOptions) *gin.Engine { +func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options GinServerOptions) gin.IRouter { {{if .}}wrapper := ServerInterfaceWrapper{ Handler: si, -HandlerMiddlewares: options.Middlewares, } {{end}} + +router = router.Group("/", options.Middlewares...) + {{range .}} router.{{.Method }}(options.BaseURL+"{{.Path | swaggerUriToGinUri }}", wrapper.{{.OperationId}}) {{end}} diff --git a/pkg/codegen/templates/gin/gin-wrappers.tmpl b/pkg/codegen/templates/gin/gin-wrappers.tmpl index 6eb787c09..0ddc686b9 100644 --- a/pkg/codegen/templates/gin/gin-wrappers.tmpl +++ b/pkg/codegen/templates/gin/gin-wrappers.tmpl @@ -1,11 +1,8 @@ // ServerInterfaceWrapper converts contexts to parameters. type ServerInterfaceWrapper struct { Handler ServerInterface - HandlerMiddlewares []MiddlewareFunc } -type MiddlewareFunc func(c *gin.Context) - {{range .}}{{$opid := .OperationId}} // {{$opid}} operation middleware @@ -165,10 +162,6 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *gin.Context) { {{end}} {{end}} - for _, middleware := range siw.HandlerMiddlewares { - middleware(c) - } - siw.Handler.{{.OperationId}}(c{{genParamNames .PathParams}}{{if .RequiresParamObject}}, params{{end}}) } {{end}} diff --git a/pkg/gin-middleware/oapi_validate.go b/pkg/gin-middleware/oapi_validate.go index 82a885e83..44075c37e 100644 --- a/pkg/gin-middleware/oapi_validate.go +++ b/pkg/gin-middleware/oapi_validate.go @@ -72,6 +72,8 @@ func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) gin. err := ValidateRequestFromContext(c, router, options) if err != nil { // note: i am not sure if this is the best way to handle this + // todo: the error message should be customizable by the user + c.Error(err) c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } c.Next() @@ -124,6 +126,9 @@ func ValidateRequestFromContext(c *gin.Context, router routers.Router, options * errorLines := strings.Split(e.Error(), "\n") return fmt.Errorf("error in openapi3filter.RequestError: %s", errorLines[0]) case *openapi3filter.SecurityRequirementsError: + for _, e := range e.Errors { + c.Error(e) + } return fmt.Errorf("error in openapi3filter.SecurityRequirementsError: %s", e.Error()) default: // This should never happen today, but if our upstream code changes,