From 5c067b3dae980ec13471527b50ec00712c1fc058 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 12:38:54 +0000 Subject: [PATCH] refactor(api): normalise config snapshots Co-Authored-By: Virgil --- spec_builder_helper.go | 10 ++++++++-- spec_builder_helper_internal_test.go | 29 ++++++++++++++++++++++++++++ transport.go | 2 +- 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 spec_builder_helper_internal_test.go diff --git a/spec_builder_helper.go b/spec_builder_helper.go index e8524b3..7242a69 100644 --- a/spec_builder_helper.go +++ b/spec_builder_helper.go @@ -5,6 +5,7 @@ package api import ( "reflect" "slices" + "strings" ) // SwaggerConfig captures the configured Swagger/OpenAPI metadata for an Engine. @@ -104,9 +105,8 @@ func (e *Engine) SwaggerConfig() SwaggerConfig { return SwaggerConfig{} } - return SwaggerConfig{ + cfg := SwaggerConfig{ Enabled: e.swaggerEnabled, - Path: e.swaggerPath, Title: e.swaggerTitle, Summary: e.swaggerSummary, Description: e.swaggerDesc, @@ -122,6 +122,12 @@ func (e *Engine) SwaggerConfig() SwaggerConfig { ExternalDocsDescription: e.swaggerExternalDocsDescription, ExternalDocsURL: e.swaggerExternalDocsURL, } + + if strings.TrimSpace(e.swaggerPath) != "" { + cfg.Path = normaliseSwaggerPath(e.swaggerPath) + } + + return cfg } func cloneSecuritySchemes(schemes map[string]any) map[string]any { diff --git a/spec_builder_helper_internal_test.go b/spec_builder_helper_internal_test.go new file mode 100644 index 0000000..eb47f7d --- /dev/null +++ b/spec_builder_helper_internal_test.go @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: EUPL-1.2 + +package api + +import "testing" + +func TestEngine_SwaggerConfig_Good_NormalisesPathAtSnapshot(t *testing.T) { + e := &Engine{ + swaggerPath: " /docs/ ", + } + + cfg := e.SwaggerConfig() + if cfg.Path != "/docs" { + t.Fatalf("expected normalised Swagger path /docs, got %q", cfg.Path) + } +} + +func TestEngine_TransportConfig_Good_NormalisesGraphQLPathAtSnapshot(t *testing.T) { + e := &Engine{ + graphql: &graphqlConfig{ + path: " /gql/ ", + }, + } + + cfg := e.TransportConfig() + if cfg.GraphQLPath != "/gql" { + t.Fatalf("expected normalised GraphQL path /gql, got %q", cfg.GraphQLPath) + } +} diff --git a/transport.go b/transport.go index 1ff2ea5..5a5de5c 100644 --- a/transport.go +++ b/transport.go @@ -53,7 +53,7 @@ func (e *Engine) TransportConfig() TransportConfig { cfg.SwaggerPath = resolveSwaggerPath(e.swaggerPath) } if e.graphql != nil { - cfg.GraphQLPath = e.graphql.path + cfg.GraphQLPath = normaliseGraphQLPath(e.graphql.path) } if e.wsHandler != nil || strings.TrimSpace(e.wsPath) != "" { cfg.WSPath = resolveWSPath(e.wsPath)