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

BREAKING: add WithRequestContentType to Engine from Server #333

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
dylanhitt marked this conversation as resolved.
Show resolved Hide resolved
return func(e *Engine) { e.acceptedContentTypes = consumes }
}

func WithOpenAPIConfig(config OpenAPIConfig) func(*Engine) {
return func(e *Engine) {
if config.JSONFilePath != "" {
Expand Down
28 changes: 28 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/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"]
require.False(t, ok)
})
}
4 changes: 3 additions & 1 deletion option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
6 changes: 0 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 0 additions & 26 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading