Skip to content

Commit 8092551

Browse files
committed
BREAKING: add WithRequestContentType to Engine from Server
1 parent f857cfd commit 8092551

File tree

5 files changed

+37
-33
lines changed

5 files changed

+37
-33
lines changed

engine.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ var defaultOpenAPIConfig = OpenAPIConfig{
5959
JSONFilePath: "doc/openapi.json",
6060
}
6161

62+
// WithRequestContentType sets the accepted content types for the engine.
63+
// By default, the accepted content types is */*.
64+
func WithRequestContentType(consumes ...string) func(*Engine) {
65+
return func(e *Engine) { e.acceptedContentTypes = consumes }
66+
}
67+
6268
func WithOpenAPIConfig(config OpenAPIConfig) func(*Engine) {
6369
return func(e *Engine) {
6470
if config.JSONFilePath != "" {

engine_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,31 @@ func TestWithErrorHandler(t *testing.T) {
3838
})
3939
})
4040
}
41+
42+
func TestWithRequestContentType(t *testing.T) {
43+
t.Run("base", func(t *testing.T) {
44+
e := NewEngine()
45+
require.Nil(t, e.acceptedContentTypes)
46+
})
47+
48+
t.Run("input", func(t *testing.T) {
49+
arr := []string{"application/json", "application/xml"}
50+
e := NewEngine(WithRequestContentType("application/json", "application/xml"))
51+
require.ElementsMatch(t, arr, e.acceptedContentTypes)
52+
})
53+
54+
t.Run("ensure applied to route", func(t *testing.T) {
55+
s := NewServer(WithEngineOptions(
56+
WithRequestContentType("application/json", "application/xml")),
57+
)
58+
route := Post(s, "/test", dummyController)
59+
60+
content := route.Operation.RequestBody.Value.Content
61+
require.NotNil(t, content.Get("application/json"))
62+
require.NotNil(t, content.Get("application/xml"))
63+
require.Equal(t, "#/components/schemas/ReqBody", content.Get("application/json").Schema.Ref)
64+
require.Equal(t, "#/components/schemas/ReqBody", content.Get("application/xml").Schema.Ref)
65+
_, ok := s.OpenAPI.Description().Components.RequestBodies["ReqBody"]
66+
require.False(t, ok)
67+
})
68+
}

option_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,9 @@ func TestRequestContentType(t *testing.T) {
376376
})
377377

378378
t.Run("override server", func(t *testing.T) {
379-
s := fuego.NewServer(fuego.WithRequestContentType("application/json", "application/xml"))
379+
s := fuego.NewServer(fuego.WithEngineOptions(
380+
fuego.WithRequestContentType("application/json", "application/xml"),
381+
))
380382
route := fuego.Post(
381383
s, "/test", dummyController,
382384
fuego.OptionRequestContentType("my/content-type"),

server.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,6 @@ func WithLogHandler(handler slog.Handler) func(*Server) {
336336
}
337337
}
338338

339-
// WithRequestContentType sets the accepted content types for the server.
340-
// By default, the accepted content types is */*.
341-
func WithRequestContentType(consumes ...string) func(*Server) {
342-
return func(s *Server) { s.acceptedContentTypes = consumes }
343-
}
344-
345339
// WithSerializer sets a custom serializer of type Sender that overrides the default one.
346340
// Please send a PR if you think the default serializer should be improved, instead of jumping to this option.
347341
func WithSerializer(serializer Sender) func(*Server) {

server_test.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -320,32 +320,6 @@ func dummyController(_ ContextWithBody[ReqBody]) (Resp, error) {
320320
return Resp{Message: "hello world"}, nil
321321
}
322322

323-
func TestWithRequestContentType(t *testing.T) {
324-
t.Run("base", func(t *testing.T) {
325-
s := NewServer()
326-
require.Nil(t, s.acceptedContentTypes)
327-
})
328-
329-
t.Run("input", func(t *testing.T) {
330-
arr := []string{"application/json", "application/xml"}
331-
s := NewServer(WithRequestContentType("application/json", "application/xml"))
332-
require.ElementsMatch(t, arr, s.acceptedContentTypes)
333-
})
334-
335-
t.Run("ensure applied to route", func(t *testing.T) {
336-
s := NewServer(WithRequestContentType("application/json", "application/xml"))
337-
route := Post(s, "/test", dummyController)
338-
339-
content := route.Operation.RequestBody.Value.Content
340-
require.NotNil(t, content.Get("application/json"))
341-
require.NotNil(t, content.Get("application/xml"))
342-
require.Equal(t, "#/components/schemas/ReqBody", content.Get("application/json").Schema.Ref)
343-
require.Equal(t, "#/components/schemas/ReqBody", content.Get("application/xml").Schema.Ref)
344-
_, ok := s.OpenAPI.Description().Components.RequestBodies["ReqBody"]
345-
require.False(t, ok)
346-
})
347-
}
348-
349323
func TestCustomSerialization(t *testing.T) {
350324
s := NewServer(
351325
WithSerializer(func(w http.ResponseWriter, r *http.Request, a any) error {

0 commit comments

Comments
 (0)