diff --git a/examples/petstore-expanded/gin/api/petstore-server.gen.go b/examples/petstore-expanded/gin/api/petstore-server.gen.go index 8dabdd9e76..abe997f618 100644 --- a/examples/petstore-expanded/gin/api/petstore-server.gen.go +++ b/examples/petstore-expanded/gin/api/petstore-server.gen.go @@ -134,15 +134,13 @@ type GinServerOptions struct { } // RegisterHandlers creates http.Handler with routing matching OpenAPI spec. -func RegisterHandlers(router *gin.Engine, si ServerInterface) *gin.Engine { - return RegisterHandlersWithOptions(router, si, GinServerOptions{}) +func RegisterHandlers(router gin.IRouter, si ServerInterface) { + 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) { errorHandler := options.ErrorHandler - if errorHandler == nil { errorHandler = func(c *gin.Context, err error, statusCode int) { c.JSON(statusCode, gin.H{"msg": err.Error()}) @@ -156,14 +154,9 @@ func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options } router.GET(options.BaseURL+"/pets", wrapper.FindPets) - router.POST(options.BaseURL+"/pets", wrapper.AddPet) - router.DELETE(options.BaseURL+"/pets/:id", wrapper.DeletePet) - router.GET(options.BaseURL+"/pets/:id", wrapper.FindPetByID) - - return router } // Base64 encoded, gzipped, json marshaled Swagger object diff --git a/examples/petstore-expanded/gin/petstore.go b/examples/petstore-expanded/gin/petstore.go index 2eae15bfca..a5b40a2f4c 100644 --- a/examples/petstore-expanded/gin/petstore.go +++ b/examples/petstore-expanded/gin/petstore.go @@ -11,9 +11,10 @@ import ( "net/http" "os" + "github.com/gin-gonic/gin" + "github.com/deepmap/oapi-codegen/examples/petstore-expanded/gin/api" middleware "github.com/deepmap/oapi-codegen/pkg/gin-middleware" - "github.com/gin-gonic/gin" ) func NewGinPetServer(petStore *api.PetStore, port int) *http.Server { @@ -35,7 +36,7 @@ func NewGinPetServer(petStore *api.PetStore, port int) *http.Server { r.Use(middleware.OapiRequestValidator(swagger)) // We now register our petStore above as the handler for the interface - r = api.RegisterHandlers(r, petStore) + api.RegisterHandlers(r, petStore) s := &http.Server{ Handler: r, diff --git a/internal/test/strict-server/gin/server.gen.go b/internal/test/strict-server/gin/server.gen.go index 52c729007a..a00437d709 100644 --- a/internal/test/strict-server/gin/server.gen.go +++ b/internal/test/strict-server/gin/server.gen.go @@ -208,15 +208,13 @@ type GinServerOptions struct { } // RegisterHandlers creates http.Handler with routing matching OpenAPI spec. -func RegisterHandlers(router *gin.Engine, si ServerInterface) *gin.Engine { - return RegisterHandlersWithOptions(router, si, GinServerOptions{}) +func RegisterHandlers(router gin.IRouter, si ServerInterface) { + 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) { errorHandler := options.ErrorHandler - if errorHandler == nil { errorHandler = func(c *gin.Context, err error, statusCode int) { c.JSON(statusCode, gin.H{"msg": err.Error()}) @@ -230,24 +228,14 @@ func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options } router.POST(options.BaseURL+"/json", wrapper.JSONExample) - router.POST(options.BaseURL+"/multipart", wrapper.MultipartExample) - router.POST(options.BaseURL+"/multiple", wrapper.MultipleRequestAndResponseTypes) - router.POST(options.BaseURL+"/reusable-responses", wrapper.ReusableResponses) - router.POST(options.BaseURL+"/text", wrapper.TextExample) - router.POST(options.BaseURL+"/unknown", wrapper.UnknownExample) - router.POST(options.BaseURL+"/unspecified-content-type", wrapper.UnspecifiedContentType) - router.POST(options.BaseURL+"/urlencoded", wrapper.URLEncodedExample) - router.POST(options.BaseURL+"/with-headers", wrapper.HeadersExample) - - return router } type BadrequestResponse struct { diff --git a/internal/test/strict-server/strict_test.go b/internal/test/strict-server/strict_test.go index 76f236730d..860cc4cb8d 100644 --- a/internal/test/strict-server/strict_test.go +++ b/internal/test/strict-server/strict_test.go @@ -11,16 +11,17 @@ import ( "strings" "testing" + "github.com/gin-gonic/gin" + "github.com/go-chi/chi/v5" + "github.com/labstack/echo/v4" + "github.com/stretchr/testify/assert" + "github.com/deepmap/oapi-codegen/internal/test/strict-server/chi" api3 "github.com/deepmap/oapi-codegen/internal/test/strict-server/client" api4 "github.com/deepmap/oapi-codegen/internal/test/strict-server/echo" api2 "github.com/deepmap/oapi-codegen/internal/test/strict-server/gin" "github.com/deepmap/oapi-codegen/pkg/runtime" "github.com/deepmap/oapi-codegen/pkg/testutil" - "github.com/gin-gonic/gin" - "github.com/go-chi/chi/v5" - "github.com/labstack/echo/v4" - "github.com/stretchr/testify/assert" ) func TestChiServer(t *testing.T) { @@ -44,8 +45,8 @@ func TestGinServer(t *testing.T) { strictHandler := api2.NewStrictHandler(server, nil) gin.SetMode(gin.ReleaseMode) r := gin.New() - handler := api2.RegisterHandlers(r, strictHandler) - testImpl(t, handler) + api2.RegisterHandlers(r, strictHandler) + testImpl(t, r) } func testImpl(t *testing.T, handler http.Handler) { diff --git a/pkg/codegen/templates/gin/gin-register.tmpl b/pkg/codegen/templates/gin/gin-register.tmpl index 188cbb83ea..8f03722ab1 100644 --- a/pkg/codegen/templates/gin/gin-register.tmpl +++ b/pkg/codegen/templates/gin/gin-register.tmpl @@ -6,15 +6,14 @@ type GinServerOptions struct { } // RegisterHandlers creates http.Handler with routing matching OpenAPI spec. -func RegisterHandlers(router *gin.Engine, si ServerInterface) *gin.Engine { - return RegisterHandlersWithOptions(router, si, GinServerOptions{}) +func RegisterHandlers(router gin.IRouter, si ServerInterface) { + RegisterHandlersWithOptions(router, si, GinServerOptions{}) } // RegisterHandlersWithOptions creates http.Handler with additional options -func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options GinServerOptions) *gin.Engine { - {{if .}} +func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options GinServerOptions) { + {{- if . -}} errorHandler := options.ErrorHandler - if errorHandler == nil { errorHandler = func(c *gin.Context, err error, statusCode int) { c.JSON(statusCode, gin.H{"msg": err.Error()}) @@ -27,8 +26,8 @@ func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options ErrorHandler: errorHandler, } {{end}} - {{range .}} + + {{range . -}} router.{{.Method }}(options.BaseURL+"{{.Path | swaggerUriToGinUri }}", wrapper.{{.OperationId}}) - {{end}} - return router + {{end -}} }