Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions pkg/codegen/templates/gin/gin-register.tmpl
Original file line number Diff line number Diff line change
@@ -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}}
Expand Down
7 changes: 0 additions & 7 deletions pkg/codegen/templates/gin/gin-wrappers.tmpl
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}}
5 changes: 5 additions & 0 deletions pkg/gin-middleware/oapi_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down