Skip to content

Commit

Permalink
fix: NewRoute/NewBaseRoute to take engine not just OpeAPI for accepte…
Browse files Browse the repository at this point in the history
…d content types
  • Loading branch information
dylanhitt committed Jan 7, 2025
1 parent 8092551 commit c36f782
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
13 changes: 9 additions & 4 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -58,10 +59,14 @@ func TestWithRequestContentType(t *testing.T) {
route := Post(s, "/test", dummyController)

content := route.Operation.RequestBody.Value.Content
require.NotNil(t, content.Get("application/json"))
require.NotNil(t, content.Get("application/xml"))
require.Equal(t, "#/components/schemas/ReqBody", content.Get("application/json").Schema.Ref)
require.Equal(t, "#/components/schemas/ReqBody", content.Get("application/xml").Schema.Ref)
require.NotNil(t, content["application/json"])
assert.Equal(t, "#/components/schemas/ReqBody", content["application/json"].Schema.Ref)

require.NotNil(t, content["application/xml"])
assert.Equal(t, "#/components/schemas/ReqBody", content["application/xml"].Schema.Ref)

require.Nil(t, content["application/x-yaml"])

_, ok := s.OpenAPI.Description().Components.RequestBodies["ReqBody"]
require.False(t, ok)
})
Expand Down
4 changes: 2 additions & 2 deletions extra/fuegogin/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Post[T, B any](engine *fuego.Engine, ginRouter gin.IRouter, path string, ha
}

func handleFuego[T, B any](engine *fuego.Engine, ginRouter gin.IRouter, method, path string, fuegoHandler func(c fuego.ContextWithBody[B]) (T, error), options ...func(*fuego.BaseRoute)) *fuego.Route[T, B] {
baseRoute := fuego.NewBaseRoute(method, path, fuegoHandler, engine.OpenAPI, options...)
baseRoute := fuego.NewBaseRoute(method, path, fuegoHandler, engine, options...)
return fuego.Registers(engine, ginRouteRegisterer[T, B]{
ginRouter: ginRouter,
route: fuego.Route[T, B]{BaseRoute: baseRoute},
Expand All @@ -35,7 +35,7 @@ func handleFuego[T, B any](engine *fuego.Engine, ginRouter gin.IRouter, method,
}

func handleGin(engine *fuego.Engine, ginRouter gin.IRouter, method, path string, ginHandler gin.HandlerFunc, options ...func(*fuego.BaseRoute)) *fuego.Route[any, any] {
baseRoute := fuego.NewBaseRoute(method, path, ginHandler, engine.OpenAPI, options...)
baseRoute := fuego.NewBaseRoute(method, path, ginHandler, engine, options...)
return fuego.Registers(engine, ginRouteRegisterer[any, any]{
ginRouter: ginRouter,
route: fuego.Route[any, any]{BaseRoute: baseRoute},
Expand Down
4 changes: 2 additions & 2 deletions net_http_mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func PatchStd(s *Server, path string, controller func(http.ResponseWriter, *http
}

func registerFuegoController[T, B any](s *Server, method, path string, controller func(ContextWithBody[B]) (T, error), options ...func(*BaseRoute)) *Route[T, B] {
route := NewRoute[T, B](method, path, controller, s.OpenAPI, append(s.routeOptions, options...)...)
route := NewRoute[T, B](method, path, controller, s.Engine, append(s.routeOptions, options...)...)

acceptHeaderParameter := openapi3.NewHeaderParameter("Accept")
acceptHeaderParameter.Schema = openapi3.NewStringSchema().NewRef()
Expand All @@ -149,7 +149,7 @@ func registerFuegoController[T, B any](s *Server, method, path string, controlle
}

func registerStdController(s *Server, method, path string, controller func(http.ResponseWriter, *http.Request), options ...func(*BaseRoute)) *Route[any, any] {
route := NewRoute[any, any](method, path, controller, s.OpenAPI, append(s.routeOptions, options...)...)
route := NewRoute[any, any](method, path, controller, s.Engine, append(s.routeOptions, options...)...)

return Registers(s.Engine, netHttpRouteRegisterer[any, any]{
s: s,
Expand Down
19 changes: 10 additions & 9 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/getkin/kin-openapi/openapi3"
)

func NewRoute[T, B any](method, path string, handler any, openapi *OpenAPI, options ...func(*BaseRoute)) Route[T, B] {
func NewRoute[T, B any](method, path string, handler any, e *Engine, options ...func(*BaseRoute)) Route[T, B] {
return Route[T, B]{
BaseRoute: NewBaseRoute(method, path, handler, openapi, options...),
BaseRoute: NewBaseRoute(method, path, handler, e, options...),
}
}

Expand All @@ -20,14 +20,15 @@ type Route[ResponseBody any, RequestBody any] struct {
BaseRoute
}

func NewBaseRoute(method, path string, handler any, openapi *OpenAPI, options ...func(*BaseRoute)) BaseRoute {
func NewBaseRoute(method, path string, handler any, e *Engine, options ...func(*BaseRoute)) BaseRoute {
baseRoute := BaseRoute{
Method: method,
Path: path,
Params: make(map[string]OpenAPIParam),
FullName: FuncName(handler),
Operation: openapi3.NewOperation(),
OpenAPI: openapi,
Method: method,
Path: path,
Params: make(map[string]OpenAPIParam),
FullName: FuncName(handler),
Operation: openapi3.NewOperation(),
OpenAPI: e.OpenAPI,
AcceptedContentTypes: e.acceptedContentTypes,
}

for _, o := range options {
Expand Down

0 comments on commit c36f782

Please sign in to comment.