Repository: deepmap/oapi-codegen
Pull request #560 (2022-04-25 15:41:18)

Add gin to imports template
Gin should be imported by default (as other frameworks) and removed only if need by the `go fmt` step.


Patch:
diff --git a/examples/petstore-expanded/gin/api/petstore-server.gen.go b/examples/petstore-expanded/gin/api/petstore-server.gen.go
index 9ed0d50..0cec0c1 100644
--- a/examples/petstore-expanded/gin/api/petstore-server.gen.go
+++ b/examples/petstore-expanded/gin/api/petstore-server.gen.go
@@ -1,6 +1,6 @@
 // Package api provides primitives to interact with the openapi HTTP API.
 //
-// Code generated by github.com/deepmap/oapi-codegen version (devel) DO NOT EDIT.
+// Code generated by github.com/deepmap/oapi-codegen version v1.10.0 DO NOT EDIT.
 package api
 
 import (
@@ -36,12 +36,9 @@ type ServerInterface interface {
 
 // ServerInterfaceWrapper converts contexts to parameters.
 type ServerInterfaceWrapper struct {
-	Handler            ServerInterface
-	HandlerMiddlewares []MiddlewareFunc
+	Handler ServerInterface
 }
 
-type MiddlewareFunc func(c *gin.Context)
-
 // FindPets operation middleware
 func (siw *ServerInterfaceWrapper) FindPets(c *gin.Context) {
 
@@ -72,20 +69,12 @@ func (siw *ServerInterfaceWrapper) FindPets(c *gin.Context) {
 		return
 	}
 
-	for _, middleware := range siw.HandlerMiddlewares {
-		middleware(c)
-	}
-
 	siw.Handler.FindPets(c, params)
 }
 
 // AddPet operation middleware
 func (siw *ServerInterfaceWrapper) AddPet(c *gin.Context) {
 
-	for _, middleware := range siw.HandlerMiddlewares {
-		middleware(c)
-	}
-
 	siw.Handler.AddPet(c)
 }
 
@@ -103,10 +92,6 @@ func (siw *ServerInterfaceWrapper) DeletePet(c *gin.Context) {
 		return
 	}
 
-	for _, middleware := range siw.HandlerMiddlewares {
-		middleware(c)
-	}
-
 	siw.Handler.DeletePet(c, id)
 }
 
@@ -124,31 +109,31 @@ func (siw *ServerInterfaceWrapper) FindPetByID(c *gin.Context) {
 		return
 	}
 
-	for _, middleware := range siw.HandlerMiddlewares {
-		middleware(c)
-	}
-
 	siw.Handler.FindPetByID(c, id)
 }
 
 // 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 {
 	wrapper := ServerInterfaceWrapper{
-		Handler:            si,
-		HandlerMiddlewares: options.Middlewares,
+		Handler: si,
 	}
 
+	router = router.Group("/", options.Middlewares...)
+
 	router.GET(options.BaseURL+"/pets", wrapper.FindPets)
 
 	router.POST(options.BaseURL+"/pets", wrapper.AddPet)
@@ -158,7 +143,9 @@ func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options
 	router.GET(options.BaseURL+"/pets/:id", wrapper.FindPetByID)
 
 	return router
-} // Base64 encoded, gzipped, json marshaled Swagger object
+}
+
+// Base64 encoded, gzipped, json marshaled Swagger object
 var swaggerSpec = []string{
 
 	"H4sIAAAAAAAC/+RXW48budH9KwV+32OnNbEXedBTvB4vICBrT+LdvKznoYZdkmrBSw9Z1FgY6L8HRbZu",
diff --git a/examples/petstore-expanded/gin/api/petstore-types.gen.go b/examples/petstore-expanded/gin/api/petstore-types.gen.go
index 24add64..d56ecc7 100644
--- a/examples/petstore-expanded/gin/api/petstore-types.gen.go
+++ b/examples/petstore-expanded/gin/api/petstore-types.gen.go
@@ -1,6 +1,6 @@
 // Package api provides primitives to interact with the openapi HTTP API.
 //
-// Code generated by github.com/deepmap/oapi-codegen version (devel) DO NOT EDIT.
+// Code generated by github.com/deepmap/oapi-codegen version v1.10.0 DO NOT EDIT.
 package api
 
 // Error defines model for Error.
diff --git a/examples/petstore-expanded/gin/petstore.go b/examples/petstore-expanded/gin/petstore.go
index ea12fdf..ba8d968 100644
--- a/examples/petstore-expanded/gin/petstore.go
+++ b/examples/petstore-expanded/gin/petstore.go
@@ -36,7 +36,9 @@ 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)
+	apiRoutes := api.RegisterHandlers(r, petStore).(*gin.RouterGroup)
+
+	r.Use(apiRoutes.Handlers...)
 
 	s := &http.Server{
 		Handler: r,
diff --git a/pkg/codegen/templates/imports.tmpl b/pkg/codegen/templates/imports.tmpl
index a55c15d..064e370 100644
--- a/pkg/codegen/templates/imports.tmpl
+++ b/pkg/codegen/templates/imports.tmpl
@@ -26,6 +26,7 @@ import (
 	"github.com/getkin/kin-openapi/openapi3"
 	"github.com/go-chi/chi/v5"
 	"github.com/labstack/echo/v4"
+	"github.com/gin-gonic/gin"
 	{{- range .ExternalImports}}
 	{{ . }}
 	{{- end}}
diff --git a/pkg/types/uuid.go b/pkg/types/uuid.go
index a4fb36d..50515d8 100644
--- a/pkg/types/uuid.go
+++ b/pkg/types/uuid.go
@@ -7,13 +7,10 @@ import (
 	"github.com/google/uuid"
 )
 
-type UUID string
+type UUID uuid.UUID
 
 func (u UUID) MarshalJSON() ([]byte, error) {
-	if _, err := uuid.Parse(string(u)); err != nil {
-		return nil, errors.New("uuid: failed to pass validation")
-	}
-	return json.Marshal(string(u))
+	return json.Marshal(uuid.UUID(u).String())
 }
 
 func (u *UUID) UnmarshalJSON(data []byte) error {
@@ -21,9 +18,10 @@ func (u *UUID) UnmarshalJSON(data []byte) error {
 	if err := json.Unmarshal(data, &s); err != nil {
 		return err
 	}
-	if _, err := uuid.Parse(s); err != nil {
+	parsed, err := uuid.Parse(s)
+	if err != nil {
 		return errors.New("uuid: failed to pass validation")
 	}
-	*u = UUID(s)
+	*u = UUID(parsed)
 	return nil
 }


Test patch:
diff --git a/pkg/types/uuid_test.go b/pkg/types/uuid_test.go
index 7f19aaa..4ac689a 100644
--- a/pkg/types/uuid_test.go
+++ b/pkg/types/uuid_test.go
@@ -4,22 +4,24 @@ import (
 	"encoding/json"
 	"testing"
 
+	"github.com/google/uuid"
 	"github.com/stretchr/testify/assert"
 )
 
-func TestUUID_MarshalJSON_Fail(t *testing.T) {
-	testUUID := "this-is-not-a-uuid"
+func TestUUID_MarshalJSON_Zero(t *testing.T) {
+	var testUUID UUID
 	b := struct {
 		UUIDField UUID `json:"uuid"`
 	}{
-		UUIDField: UUID(testUUID),
+		UUIDField: testUUID,
 	}
-	_, err := json.Marshal(b)
-	assert.Error(t, err)
+	marshaled, err := json.Marshal(b)
+	assert.NoError(t, err)
+	assert.JSONEq(t, `{"uuid":"00000000-0000-0000-0000-000000000000"}`, string(marshaled))
 }
 
 func TestUUID_MarshalJSON_Pass(t *testing.T) {
-	testUUID := "9cb14230-b640-11ec-b909-0242ac120002"
+	testUUID := uuid.MustParse("9cb14230-b640-11ec-b909-0242ac120002")
 	b := struct {
 		UUIDField UUID `json:"uuid"`
 	}{
@@ -40,7 +42,7 @@ func TestUUID_UnmarshalJSON_Fail(t *testing.T) {
 }
 
 func TestUUID_UnmarshalJSON_Pass(t *testing.T) {
-	testUUID := UUID("9cb14230-b640-11ec-b909-0242ac120002")
+	testUUID := UUID(uuid.MustParse("9cb14230-b640-11ec-b909-0242ac120002"))
 	jsonStr := `{"uuid":"9cb14230-b640-11ec-b909-0242ac120002"}`
 	b := struct {
 		UUIDField UUID `json:"uuid"`