Skip to content

Commit

Permalink
hide from openapi spec
Browse files Browse the repository at this point in the history
  • Loading branch information
epentland committed Mar 4, 2024
1 parent 08c6520 commit 284d2cd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
34 changes: 34 additions & 0 deletions documentation/docs/guides/openapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,37 @@ Please note that if you embed swagger ui in your build it will increase its size
| ------------- | ------------------ | -------------- | -------- |
| Works offline | No ❌ | Yes ✅ | - |
| Binary Size | Smaller | Larger (+10Mb) | Smaller |

## Hide From Openapi Spec

Certain routes such as web routes you may not want to be part of the openapi spec.

You can prevent them from being added with the server.Hide().

```go
package main

import (
"github.com/go-fuego/fuego"
)

func main() {
s := fuego.NewServer()

// Create a group of routes to be hidden
web := s.Group(s, "/")
web.Hide()

fuego.Get(web, "/", func(c fuego.ContextNoBody) (string, error) {
return "Hello, World!", nil
})

// These routes will still be added to the spec
api := s.Group(s, "/api")
fuego.Get(api, "/hello", func(c fuego.ContextNoBody) (string, error) {
return "Hello, World!", nil
})

s.Run()
}
```
6 changes: 6 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ func register[T any, B any](s *Server, method string, path string, controller ht
allMiddlewares := append(middlewares, s.middlewares...)
s.Mux.Handle(fullPath, withMiddlewares(controller, allMiddlewares...))

if s.DisableOpenapi {
return Route[T, B]{
operation: &openapi3.Operation{},
}
}

operation, err := RegisterOpenAPIOperation[T, B](s, method, s.basePath+path)
if err != nil {
slog.Warn("error documenting openapi operation", "error", err)
Expand Down
14 changes: 14 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"

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

Expand Down Expand Up @@ -245,6 +246,19 @@ func TestDeleteStd(t *testing.T) {
require.Equal(t, w.Body.String(), "test successful")
}

func TestHideOpenapiRoutes(t *testing.T) {
s := NewServer()
s.Hide()
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "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")
}

func BenchmarkRequest(b *testing.B) {
type Resp struct {
Name string `json:"name"`
Expand Down
6 changes: 6 additions & 0 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func NewOpenApiSpec() openapi3.T {
return spec
}

// 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
}

func (s *Server) generateOpenAPI() openapi3.T {
// Validate
err := s.OpenApiSpec.Validate(context.Background())
Expand Down
1 change: 1 addition & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Server struct {
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 the routes within the server will not generate an openapi spec.
maxBodySize int64
Serialize func(w http.ResponseWriter, ans any) // Used to serialize the response. Defaults to [SendJSON].
SerializeError func(w http.ResponseWriter, err error) // Used to serialize the error response. Defaults to [SendJSONError].
Expand Down

0 comments on commit 284d2cd

Please sign in to comment.