Skip to content

Commit

Permalink
feat: add OptionShow
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanhitt committed Dec 20, 2024
1 parent 6a7c0cb commit 57f4c1e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func Register[T, B any](s *Server, route Route[T, B], controller http.Handler, o
route.Middlewares = append(s.middlewares, route.Middlewares...)
s.Mux.Handle(fullPath, withMiddlewares(route.Handler, route.Middlewares...))

if s.DisableOpenapi || route.Hidden || route.Method == "" {
if route.Hidden || route.Method == "" {
return &route
}

Expand Down
5 changes: 0 additions & 5 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ func TestHideOpenapiRoutes(t *testing.T) {
s.Hide()
Get(s, "/test", func(ctx ContextNoBody) (string, error) { return "", nil })

require.Equal(t, s.DisableOpenapi, true)
require.True(t, s.OpenAPI.Description().Paths.Find("/not-hidden") != nil)
require.True(t, s.OpenAPI.Description().Paths.Find("/test") == nil)
})
Expand All @@ -499,7 +498,6 @@ func TestHideOpenapiRoutes(t *testing.T) {
g := Group(s, "/group").Hide()
Get(g, "/test", func(ctx ContextNoBody) (string, error) { return "", nil })

require.Equal(t, g.DisableOpenapi, true)
require.True(t, s.OpenAPI.Description().Paths.Find("/not-hidden") != nil)
require.True(t, s.OpenAPI.Description().Paths.Find("/group/test") == nil)
})
Expand All @@ -512,8 +510,6 @@ func TestHideOpenapiRoutes(t *testing.T) {
g2 := Group(s, "/group2")
Get(g2, "/test", func(ctx ContextNoBody) (string, error) { return "test", nil })

require.Equal(t, true, g.DisableOpenapi)
require.Equal(t, false, g2.DisableOpenapi)
require.True(t, s.OpenAPI.Description().Paths.Find("/group/test") == nil)
require.True(t, s.OpenAPI.Description().Paths.Find("/group2/test") != nil)
})
Expand All @@ -526,7 +522,6 @@ func TestHideOpenapiRoutes(t *testing.T) {
g2 := Group(g, "/sub").Show()
Get(g2, "/test", func(ctx ContextNoBody) (string, error) { return "test", nil })

require.Equal(t, true, g.DisableOpenapi)
require.True(t, s.OpenAPI.Description().Paths.Find("/group/test") == nil)
require.True(t, s.OpenAPI.Description().Paths.Find("/group/sub/test") != nil)
})
Expand Down
10 changes: 8 additions & 2 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,21 @@ func NewOpenApiSpec() openapi3.T {
}

// Hide prevents the routes in this server or group from being included in the OpenAPI spec.
// Deprecated: Please use [OptionHide] with [WithRouteOptions]
func (s *Server) Hide() *Server {
s.DisableOpenapi = true
WithRouteOptions(
OptionHide(),
)(s)
return s
}

// Show allows displaying the routes. Activated by default so useless in most cases,
// but this can be useful if you deactivated the parent group.
// Deprecated: Please use [OptionShow] with [WithRouteOptions]
func (s *Server) Show() *Server {
s.DisableOpenapi = false
WithRouteOptions(
OptionShow(),
)(s)
return s
}

Expand Down
13 changes: 13 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ func OptionHide() func(*BaseRoute) {
}
}

// Show shows the route from the OpenAPI spec.
func OptionShow() func(*BaseRoute) {
return func(r *BaseRoute) {
r.Hidden = false
}
}

// OptionDefaultStatusCode sets the default status code for the route.
func OptionDefaultStatusCode(defaultStatusCode int) func(*BaseRoute) {
return func(r *BaseRoute) {
Expand Down Expand Up @@ -431,3 +438,9 @@ func OptionSecurity(securityRequirements ...openapi3.SecurityRequirement) func(*
*r.Operation.Security = append(*r.Operation.Security, securityRequirements...)
}
}

func OptionShowInOpenAPI(show bool) func(*BaseRoute) {
return func(r *BaseRoute) {

}
}
3 changes: 3 additions & 0 deletions option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,8 @@ var RequestContentType = fuego.OptionRequestContentType
// Hide hides the route from the OpenAPI spec.
var Hide = fuego.OptionHide

// Hide hides the route from the OpenAPI spec.
var Show = fuego.OptionShow

// DefaultStatusCode sets the default status code for the route.
var DefaultStatusCode = fuego.OptionDefaultStatusCode
6 changes: 4 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ type Server struct {
fs fs.FS
template *template.Template // TODO: use preparsed templates

DisallowUnknownFields bool // If true, the server will return an error if the request body contains unknown fields. Useful for quick debugging in development.
DisableOpenapi bool // If true, the routes within the server will not generate an OpenAPI spec.
acceptedContentTypes []string

// If true, the server will return an error if the request body contains unknown fields. Useful for quick debugging in development.
DisallowUnknownFields bool
maxBodySize int64

// Custom serializer that overrides the default one.
Expand Down

0 comments on commit 57f4c1e

Please sign in to comment.