Skip to content

Commit

Permalink
Hide or show group
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Mar 18, 2024
1 parent 39ecb6d commit d7112ea
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/full-app-gourmet/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (rs Ressources) Setup(
})

// Register views (controllers that return HTML pages)
rs.Views.Routes(fuego.Group(app, "/"))
rs.Views.Routes(fuego.Group(app, "/").Hide())

// Register API routes (controllers that return JSON)
rs.API.MountRoutes(fuego.Group(app, "/api"))
Expand Down
56 changes: 47 additions & 9 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"testing"

"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -247,16 +246,55 @@ func TestDeleteStd(t *testing.T) {
}

func TestHideOpenapiRoutes(t *testing.T) {
s := NewServer()
s.Hide()
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
t.Run("hide main server", func(t *testing.T) {
s := NewServer()
Get(s, "/not-hidden", func(ctx *ContextNoBody) (string, error) { return "", nil })
s.Hide()
Get(s, "/test", func(ctx *ContextNoBody) (string, error) { return "", nil })

require.Equal(t, s.DisableOpenapi, true)
require.True(t, s.OpenApiSpec.Paths.Find("/not-hidden") != nil)
require.True(t, s.OpenApiSpec.Paths.Find("/test") == nil)
})

t.Run("hide group", func(t *testing.T) {
s := NewServer()
Get(s, "/not-hidden", func(ctx *ContextNoBody) (string, error) { return "", nil })

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.OpenApiSpec.Paths.Find("/not-hidden") != nil)
require.True(t, s.OpenApiSpec.Paths.Find("/group/test") == nil)
})

require.Equal(t, s.DisableOpenapi, true)
require.Equal(t, s.OpenApiSpec.Components.Schemas, openapi3.Schemas{})
require.Equal(t, s.OpenApiSpec.Components.Responses, openapi3.ResponseBodies{})
require.Equal(t, s.OpenApiSpec.Components.RequestBodies, openapi3.RequestBodies{}, "test successful")
t.Run("hide group but not other group", func(t *testing.T) {
s := NewServer()
g := Group(s, "/group").Hide()
Get(g, "/test", func(ctx *ContextNoBody) (string, error) { return "test", nil })

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.OpenApiSpec.Paths.Find("/group/test") == nil)
require.True(t, s.OpenApiSpec.Paths.Find("/group2/test") != nil)
})

t.Run("hide group but show sub group", func(t *testing.T) {
s := NewServer()
g := Group(s, "/group").Hide()
Get(g, "/test", func(ctx *ContextNoBody) (string, error) { return "test", nil })

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.OpenApiSpec.Paths.Find("/group/test") == nil)
require.True(t, s.OpenApiSpec.Paths.Find("/group/sub/test") != nil)
})
}

func BenchmarkRequest(b *testing.B) {
Expand Down
9 changes: 8 additions & 1 deletion openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,19 @@ func NewOpenApiSpec() openapi3.T {
return spec
}

// Prevents the routes in this server or group from being included in the OpenAPI spec.
// Hide prevents the routes in this server or group from being included in the OpenAPI spec.
func (s *Server) Hide() *Server {
s.DisableOpenapi = true
return s
}

// Show allows to display the routes. Activated by default so useless in most cases,
// but this can be useful if you desactivated the parent group.
func (s *Server) Show() *Server {
s.DisableOpenapi = false
return s
}

// OutputOpenAPISpec takes the OpenAPI spec and outputs it to a JSON file and/or serves it on a URL.
// Also serves a Swagger UI.
// To modify its behavior, use the [WithOpenAPIConfig] option.
Expand Down

0 comments on commit d7112ea

Please sign in to comment.