Skip to content

Commit 1687ac0

Browse files
derfenixdanicc097
authored andcommitted
Use gin.IRouter instead of *gin.Engine (oapi-codegen#879)
* Use gin.IRouter instead of *gin.Engine * Regenerate, fix tests
1 parent 6474938 commit 1687ac0

5 files changed

Lines changed: 23 additions & 41 deletions

File tree

examples/petstore-expanded/gin/api/petstore-server.gen.go

Lines changed: 3 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/petstore-expanded/gin/petstore.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import (
1111
"net/http"
1212
"os"
1313

14+
"github.com/gin-gonic/gin"
15+
1416
"github.com/deepmap/oapi-codegen/examples/petstore-expanded/gin/api"
1517
middleware "github.com/deepmap/oapi-codegen/pkg/gin-middleware"
16-
"github.com/gin-gonic/gin"
1718
)
1819

1920
func NewGinPetServer(petStore *api.PetStore, port int) *http.Server {
@@ -35,7 +36,7 @@ func NewGinPetServer(petStore *api.PetStore, port int) *http.Server {
3536
r.Use(middleware.OapiRequestValidator(swagger))
3637

3738
// We now register our petStore above as the handler for the interface
38-
r = api.RegisterHandlers(r, petStore)
39+
api.RegisterHandlers(r, petStore)
3940

4041
s := &http.Server{
4142
Handler: r,

internal/test/strict-server/gin/server.gen.go

Lines changed: 3 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/test/strict-server/strict_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ import (
1111
"strings"
1212
"testing"
1313

14+
"github.com/gin-gonic/gin"
15+
"github.com/go-chi/chi/v5"
16+
"github.com/labstack/echo/v4"
17+
"github.com/stretchr/testify/assert"
18+
1419
"github.com/deepmap/oapi-codegen/internal/test/strict-server/chi"
1520
api3 "github.com/deepmap/oapi-codegen/internal/test/strict-server/client"
1621
api4 "github.com/deepmap/oapi-codegen/internal/test/strict-server/echo"
1722
api2 "github.com/deepmap/oapi-codegen/internal/test/strict-server/gin"
1823
"github.com/deepmap/oapi-codegen/pkg/runtime"
1924
"github.com/deepmap/oapi-codegen/pkg/testutil"
20-
"github.com/gin-gonic/gin"
21-
"github.com/go-chi/chi/v5"
22-
"github.com/labstack/echo/v4"
23-
"github.com/stretchr/testify/assert"
2425
)
2526

2627
func TestChiServer(t *testing.T) {
@@ -44,8 +45,8 @@ func TestGinServer(t *testing.T) {
4445
strictHandler := api2.NewStrictHandler(server, nil)
4546
gin.SetMode(gin.ReleaseMode)
4647
r := gin.New()
47-
handler := api2.RegisterHandlers(r, strictHandler)
48-
testImpl(t, handler)
48+
api2.RegisterHandlers(r, strictHandler)
49+
testImpl(t, r)
4950
}
5051

5152
func testImpl(t *testing.T, handler http.Handler) {

pkg/codegen/templates/gin/gin-register.tmpl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ type GinServerOptions struct {
66
}
77

88
// RegisterHandlers creates http.Handler with routing matching OpenAPI spec.
9-
func RegisterHandlers(router *gin.Engine, si ServerInterface) *gin.Engine {
10-
return RegisterHandlersWithOptions(router, si, GinServerOptions{})
9+
func RegisterHandlers(router gin.IRouter, si ServerInterface) {
10+
RegisterHandlersWithOptions(router, si, GinServerOptions{})
1111
}
1212

1313
// RegisterHandlersWithOptions creates http.Handler with additional options
14-
func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options GinServerOptions) *gin.Engine {
15-
{{if .}}
14+
func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options GinServerOptions) {
15+
{{- if . -}}
1616
errorHandler := options.ErrorHandler
17-
1817
if errorHandler == nil {
1918
errorHandler = func(c *gin.Context, err error, statusCode int) {
2019
c.JSON(statusCode, gin.H{"msg": err.Error()})
@@ -27,8 +26,8 @@ func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options
2726
ErrorHandler: errorHandler,
2827
}
2928
{{end}}
30-
{{range .}}
29+
30+
{{range . -}}
3131
router.{{.Method }}(options.BaseURL+"{{.Path | swaggerUriToGinUri }}", wrapper.{{.OperationId}})
32-
{{end}}
33-
return router
32+
{{end -}}
3433
}

0 commit comments

Comments
 (0)