Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a struct from gin's RegisterHandlers instead of an interface #555

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
41 changes: 14 additions & 27 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.

3 changes: 3 additions & 0 deletions examples/petstore-expanded/gin/api/petstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --config=types.cfg.yaml ../../petstore-expanded.yaml
//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --config=server.cfg.yaml ../../petstore-expanded.yaml

package api

import (
Expand Down
2 changes: 1 addition & 1 deletion examples/petstore-expanded/gin/api/server.cfg.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output: examples/petstore-expanded/gin/api/petstore-server.gen.go
output: petstore-server.gen.go
generate:
- gin
- spec
Expand Down
2 changes: 1 addition & 1 deletion examples/petstore-expanded/gin/api/types.cfg.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output: examples/petstore-expanded/gin/api/petstore-types.gen.go
output: petstore-types.gen.go
generate:
- types
package: api
5 changes: 4 additions & 1 deletion examples/petstore-expanded/gin/petstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ 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)

// Then we attach the registered handlers to the Gin Engine
r.Use(apiRoutes.Handlers...)

s := &http.Server{
Handler: r,
Expand Down
6 changes: 3 additions & 3 deletions pkg/codegen/templates/gin/gin-register.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ type GinServerOptions struct {
type MiddlewareFunc = gin.HandlerFunc

// RegisterHandlers creates http.Handler with routing matching OpenAPI spec.
func RegisterHandlers(router gin.IRouter, si ServerInterface) gin.IRouter {
func RegisterHandlers(router gin.IRouter, si ServerInterface) *gin.RouterGroup {
return RegisterHandlersWithOptions(router, si, GinServerOptions{})
}

// RegisterHandlersWithOptions creates http.Handler with additional options
func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options GinServerOptions) gin.IRouter {
func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options GinServerOptions) *gin.RouterGroup {
{{if .}}wrapper := ServerInterfaceWrapper{
Handler: si,
}
Expand All @@ -24,5 +24,5 @@ router = router.Group("/", options.Middlewares...)
{{range .}}
router.{{.Method }}(options.BaseURL+"{{.Path | swaggerUriToGinUri }}", wrapper.{{.OperationId}})
{{end}}
return router
return router.(*gin.RouterGroup)
}