From b37e5f77ce1cfe4caadbbcf7db7cbc2b8b5ec86f Mon Sep 17 00:00:00 2001 From: zachwood222 <30605559+zachwood222@users.noreply.github.com> Date: Wed, 15 Jan 2025 15:33:21 -0600 Subject: [PATCH] Update engine.go --- engine.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/engine.go b/engine.go index 9be261b3..7c69d7fb 100644 --- a/engine.go +++ b/engine.go @@ -43,6 +43,13 @@ type Engine struct { } type OpenAPIConfig struct { + MiddlewareDisplayLimit int +} + +func DefaultOpenAPIConfig() OpenAPIConfig { + return OpenAPIConfig{ + MiddlewareDisplayLimit: 5, + } // Local path to save the OpenAPI JSON spec JSONFilePath string // If true, the server will not serve nor generate any OpenAPI resources @@ -58,6 +65,12 @@ type OpenAPIConfig struct { var defaultOpenAPIConfig = OpenAPIConfig{ JSONFilePath: "doc/openapi.json", } +//implemnt action +func WithOpenAPIConfig(config OpenAPIConfig) func(*Engine) { + return func(e *Engine) { + e.openAPIConfig = config + } +} // WithRequestContentType sets the accepted content types for the engine. // By default, the accepted content types is */*. @@ -77,6 +90,14 @@ func WithOpenAPIConfig(config OpenAPIConfig) func(*Engine) { } } +func (e *Engine) DisplayMiddlewares() []Middleware { + limit := e.openAPIConfig.MiddlewareDisplayLimit + if len(e.middlewares) < limit { + return e.middlewares + } + return e.middlewares[:limit] +} + // WithErrorHandler sets a customer error handler for the server func WithErrorHandler(errorHandler func(err error) error) func(*Engine) { return func(e *Engine) { @@ -150,6 +171,28 @@ func (s *Engine) marshalSpec() ([]byte, error) { return json.Marshal(s.OpenAPI.Description()) } +func TestDefaultMiddlewareLimit(t *testing.T) { + engine := NewEngine(DefaultOpenAPIConfig()) + engine.middlewares = []Middleware{"A", "B", "C", "D", "E", "F"} + + displayed := engine.DisplayMiddlewares() + if len(displayed) != 5 { + t.Errorf("Expected 5 middlewares, got %d", len(displayed)) + } +} + +func TestCustomMiddlewareLimit(t *testing.T) { + config := DefaultOpenAPIConfig() + config.MiddlewareDisplayLimit = 3 + engine := NewEngine(config) + engine.middlewares = []Middleware{"A", "B", "C", "D", "E"} + + displayed := engine.DisplayMiddlewares() + if len(displayed) != 3 { + t.Errorf("Expected 3 middlewares, got %d", len(displayed)) + } +} + func (e *Engine) printOpenAPIMessage(msg string) { if !e.OpenAPIConfig.DisableMessages { slog.Info(msg)