diff --git a/examples/petstore-expanded/gorilla/api/petstore.gen.go b/examples/petstore-expanded/gorilla/api/petstore.gen.go index 2d3f21d35..f5b07ba15 100644 --- a/examples/petstore-expanded/gorilla/api/petstore.gen.go +++ b/examples/petstore-expanded/gorilla/api/petstore.gen.go @@ -83,7 +83,7 @@ type ServerInterfaceWrapper struct { ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error) } -type MiddlewareFunc func(http.HandlerFunc) http.HandlerFunc +type MiddlewareFunc func(http.Handler) http.Handler // FindPets operation middleware func (siw *ServerInterfaceWrapper) FindPets(w http.ResponseWriter, r *http.Request) { @@ -110,30 +110,30 @@ func (siw *ServerInterfaceWrapper) FindPets(w http.ResponseWriter, r *http.Reque return } - handler := func(w http.ResponseWriter, r *http.Request) { + handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { siw.Handler.FindPets(w, r, params) - } + })) for i := len(siw.HandlerMiddlewares) - 1; i >= 0; i-- { handler = siw.HandlerMiddlewares[i](handler) } - handler(w, r.WithContext(ctx)) + handler.ServeHTTP(w, r.WithContext(ctx)) } // AddPet operation middleware func (siw *ServerInterfaceWrapper) AddPet(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - handler := func(w http.ResponseWriter, r *http.Request) { + handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { siw.Handler.AddPet(w, r) - } + })) for i := len(siw.HandlerMiddlewares) - 1; i >= 0; i-- { handler = siw.HandlerMiddlewares[i](handler) } - handler(w, r.WithContext(ctx)) + handler.ServeHTTP(w, r.WithContext(ctx)) } // DeletePet operation middleware @@ -151,15 +151,15 @@ func (siw *ServerInterfaceWrapper) DeletePet(w http.ResponseWriter, r *http.Requ return } - handler := func(w http.ResponseWriter, r *http.Request) { + handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { siw.Handler.DeletePet(w, r, id) - } + })) for i := len(siw.HandlerMiddlewares) - 1; i >= 0; i-- { handler = siw.HandlerMiddlewares[i](handler) } - handler(w, r.WithContext(ctx)) + handler.ServeHTTP(w, r.WithContext(ctx)) } // FindPetByID operation middleware @@ -177,15 +177,15 @@ func (siw *ServerInterfaceWrapper) FindPetByID(w http.ResponseWriter, r *http.Re return } - handler := func(w http.ResponseWriter, r *http.Request) { + handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { siw.Handler.FindPetByID(w, r, id) - } + })) for i := len(siw.HandlerMiddlewares) - 1; i >= 0; i-- { handler = siw.HandlerMiddlewares[i](handler) } - handler(w, r.WithContext(ctx)) + handler.ServeHTTP(w, r.WithContext(ctx)) } type UnescapedCookieParamError struct { diff --git a/pkg/codegen/templates/gorilla/gorilla-middleware.tmpl b/pkg/codegen/templates/gorilla/gorilla-middleware.tmpl index 468ed5dd2..0e0a5e17c 100644 --- a/pkg/codegen/templates/gorilla/gorilla-middleware.tmpl +++ b/pkg/codegen/templates/gorilla/gorilla-middleware.tmpl @@ -5,7 +5,7 @@ type ServerInterfaceWrapper struct { ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error) } -type MiddlewareFunc func(http.HandlerFunc) http.HandlerFunc +type MiddlewareFunc func(http.Handler) http.Handler {{range .}}{{$opid := .OperationId}} @@ -173,9 +173,9 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(w http.ResponseWriter, r *http.Requ {{end}} {{end}} - handler := func(w http.ResponseWriter, r *http.Request) { + handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { siw.Handler.{{.OperationId}}(w, r{{genParamNames .PathParams}}{{if .RequiresParamObject}}, params{{end}}) -} + })) {{if opts.Compatibility.ApplyGorillaMiddlewareFirstToLast}} for i := len(siw.HandlerMiddlewares) -1; i >= 0; i-- { @@ -187,7 +187,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(w http.ResponseWriter, r *http.Requ } {{end}} - handler(w, r.WithContext(ctx)) + handler.ServeHTTP(w, r.WithContext(ctx)) } {{end}}