Skip to content

Commit

Permalink
Revert "improve gin support (deepmap#530)"
Browse files Browse the repository at this point in the history
This reverts commit f10cd14.

It caused a breaking change to existing users.
  • Loading branch information
marcinromaszewicz committed Apr 19, 2022
1 parent 5154135 commit c6e70ac
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
13 changes: 4 additions & 9 deletions pkg/codegen/templates/gin/gin-register.tmpl
@@ -1,26 +1,21 @@
// GinServerOptions provides options for the Gin server.
type GinServerOptions struct {
BaseURL string
Middlewares []gin.HandlerFunc
Middlewares []MiddlewareFunc
}

// keeping for backward compatibility
type MiddlewareFunc = gin.HandlerFunc

// RegisterHandlers creates http.Handler with routing matching OpenAPI spec.
func RegisterHandlers(router gin.IRouter, si ServerInterface) gin.IRouter {
func RegisterHandlers(router *gin.Engine, si ServerInterface) *gin.Engine {
return RegisterHandlersWithOptions(router, si, GinServerOptions{})
}

// RegisterHandlersWithOptions creates http.Handler with additional options
func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options GinServerOptions) gin.IRouter {
func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options GinServerOptions) *gin.Engine {
{{if .}}wrapper := ServerInterfaceWrapper{
Handler: si,
HandlerMiddlewares: options.Middlewares,
}
{{end}}

router = router.Group("/", options.Middlewares...)

{{range .}}
router.{{.Method }}(options.BaseURL+"{{.Path | swaggerUriToGinUri }}", wrapper.{{.OperationId}})
{{end}}
Expand Down
7 changes: 7 additions & 0 deletions pkg/codegen/templates/gin/gin-wrappers.tmpl
@@ -1,8 +1,11 @@
// ServerInterfaceWrapper converts contexts to parameters.
type ServerInterfaceWrapper struct {
Handler ServerInterface
HandlerMiddlewares []MiddlewareFunc
}

type MiddlewareFunc func(c *gin.Context)

{{range .}}{{$opid := .OperationId}}

// {{$opid}} operation middleware
Expand Down Expand Up @@ -162,6 +165,10 @@ 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}}
5 changes: 0 additions & 5 deletions pkg/gin-middleware/oapi_validate.go
Expand Up @@ -72,8 +72,6 @@ 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()
Expand Down Expand Up @@ -126,9 +124,6 @@ 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,
Expand Down

0 comments on commit c6e70ac

Please sign in to comment.