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: 3 additions & 10 deletions examples/petstore-expanded/gin/api/petstore-server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions examples/petstore-expanded/gin/petstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
18 changes: 3 additions & 15 deletions internal/test/strict-server/gin/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions internal/test/strict-server/strict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
15 changes: 7 additions & 8 deletions pkg/codegen/templates/gin/gin-register.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
Expand All @@ -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 -}}
}