From 8092551bf037191087cec5f4279c4a53f02e3c8b Mon Sep 17 00:00:00 2001 From: dylanhitt Date: Tue, 7 Jan 2025 14:00:27 -0500 Subject: [PATCH 1/2] BREAKING: add WithRequestContentType to Engine from Server --- engine.go | 6 ++++++ engine_test.go | 28 ++++++++++++++++++++++++++++ option_test.go | 4 +++- server.go | 6 ------ server_test.go | 26 -------------------------- 5 files changed, 37 insertions(+), 33 deletions(-) diff --git a/engine.go b/engine.go index b171ac7c..f78563fc 100644 --- a/engine.go +++ b/engine.go @@ -59,6 +59,12 @@ var defaultOpenAPIConfig = OpenAPIConfig{ JSONFilePath: "doc/openapi.json", } +// WithRequestContentType sets the accepted content types for the engine. +// By default, the accepted content types is */*. +func WithRequestContentType(consumes ...string) func(*Engine) { + return func(e *Engine) { e.acceptedContentTypes = consumes } +} + func WithOpenAPIConfig(config OpenAPIConfig) func(*Engine) { return func(e *Engine) { if config.JSONFilePath != "" { diff --git a/engine_test.go b/engine_test.go index 3962716f..d51c6a4f 100644 --- a/engine_test.go +++ b/engine_test.go @@ -38,3 +38,31 @@ func TestWithErrorHandler(t *testing.T) { }) }) } + +func TestWithRequestContentType(t *testing.T) { + t.Run("base", func(t *testing.T) { + e := NewEngine() + require.Nil(t, e.acceptedContentTypes) + }) + + t.Run("input", func(t *testing.T) { + arr := []string{"application/json", "application/xml"} + e := NewEngine(WithRequestContentType("application/json", "application/xml")) + require.ElementsMatch(t, arr, e.acceptedContentTypes) + }) + + t.Run("ensure applied to route", func(t *testing.T) { + s := NewServer(WithEngineOptions( + WithRequestContentType("application/json", "application/xml")), + ) + 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) + _, ok := s.OpenAPI.Description().Components.RequestBodies["ReqBody"] + require.False(t, ok) + }) +} diff --git a/option_test.go b/option_test.go index 3efb137a..b6cbf442 100644 --- a/option_test.go +++ b/option_test.go @@ -376,7 +376,9 @@ func TestRequestContentType(t *testing.T) { }) t.Run("override server", func(t *testing.T) { - s := fuego.NewServer(fuego.WithRequestContentType("application/json", "application/xml")) + s := fuego.NewServer(fuego.WithEngineOptions( + fuego.WithRequestContentType("application/json", "application/xml"), + )) route := fuego.Post( s, "/test", dummyController, fuego.OptionRequestContentType("my/content-type"), diff --git a/server.go b/server.go index 53413bae..7c57b9af 100644 --- a/server.go +++ b/server.go @@ -336,12 +336,6 @@ func WithLogHandler(handler slog.Handler) func(*Server) { } } -// WithRequestContentType sets the accepted content types for the server. -// By default, the accepted content types is */*. -func WithRequestContentType(consumes ...string) func(*Server) { - return func(s *Server) { s.acceptedContentTypes = consumes } -} - // WithSerializer sets a custom serializer of type Sender that overrides the default one. // Please send a PR if you think the default serializer should be improved, instead of jumping to this option. func WithSerializer(serializer Sender) func(*Server) { diff --git a/server_test.go b/server_test.go index 2aa85634..adf3931a 100644 --- a/server_test.go +++ b/server_test.go @@ -320,32 +320,6 @@ func dummyController(_ ContextWithBody[ReqBody]) (Resp, error) { return Resp{Message: "hello world"}, nil } -func TestWithRequestContentType(t *testing.T) { - t.Run("base", func(t *testing.T) { - s := NewServer() - require.Nil(t, s.acceptedContentTypes) - }) - - t.Run("input", func(t *testing.T) { - arr := []string{"application/json", "application/xml"} - s := NewServer(WithRequestContentType("application/json", "application/xml")) - require.ElementsMatch(t, arr, s.acceptedContentTypes) - }) - - t.Run("ensure applied to route", func(t *testing.T) { - s := NewServer(WithRequestContentType("application/json", "application/xml")) - 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) - _, ok := s.OpenAPI.Description().Components.RequestBodies["ReqBody"] - require.False(t, ok) - }) -} - func TestCustomSerialization(t *testing.T) { s := NewServer( WithSerializer(func(w http.ResponseWriter, r *http.Request, a any) error { From ee5a72fcf3b34b680c6c37daae88e91e2cf8c84f Mon Sep 17 00:00:00 2001 From: dylanhitt Date: Tue, 7 Jan 2025 17:21:06 -0500 Subject: [PATCH 2/2] chore: commit show break --- engine_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine_test.go b/engine_test.go index d51c6a4f..2f595685 100644 --- a/engine_test.go +++ b/engine_test.go @@ -59,7 +59,7 @@ func TestWithRequestContentType(t *testing.T) { content := route.Operation.RequestBody.Value.Content require.NotNil(t, content.Get("application/json")) - require.NotNil(t, content.Get("application/xml")) + require.NotNil(t, content.Get("application/x-yaml")) require.Equal(t, "#/components/schemas/ReqBody", content.Get("application/json").Schema.Ref) require.Equal(t, "#/components/schemas/ReqBody", content.Get("application/xml").Schema.Ref) _, ok := s.OpenAPI.Description().Components.RequestBodies["ReqBody"]