Skip to content

Commit e8cbb61

Browse files
mromaszewiczclaude
andcommitted
sanitize param names for http.ServeMux
Go's net/http ServeMux requires wildcard segment names to be valid Go identifiers. OpenAPI specs can use path parameter names containing dashes (e.g. "addressing-identifier"), which causes a panic when registering routes with ServeMux. Fix by sanitizing parameter names in the stdhttp code path: - SwaggerUriToStdHttpUri now sanitizes param names via SanitizeGoIdentity so route patterns use valid Go identifiers (e.g. {addressing_identifier}) - stdhttp middleware template uses new SanitizedParamName for r.PathValue() calls to match the sanitized route pattern, while keeping the original ParamName for error messages - Add SanitizedParamName() method to ParameterDefinition for use by templates that need the sanitized form Add server-specific test directory with per-router integration tests exercising dashed path parameter names. Right now, only stdhttp has a test in this directory, but we'll do router specific tests in there in the future. Fixes #2278 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a13ef41 commit e8cbb61

File tree

17 files changed

+700
-6
lines changed

17 files changed

+700
-6
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# yaml-language-server: $schema=../../../../configuration-schema.json
2+
package: chi
3+
generate:
4+
chi-server: true
5+
models: true
6+
output: server.gen.go
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package chi
2+
3+
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml ../spec.yaml

internal/test/server-specific/chi/server.gen.go

Lines changed: 182 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package chi
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
type testServer struct {
13+
receivedParam string
14+
}
15+
16+
func (s *testServer) GetResource(w http.ResponseWriter, r *http.Request, addressingIdentifier string) {
17+
s.receivedParam = addressingIdentifier
18+
_, _ = fmt.Fprint(w, addressingIdentifier)
19+
}
20+
21+
func TestDashedPathParam(t *testing.T) {
22+
server := &testServer{}
23+
handler := Handler(server)
24+
25+
req := httptest.NewRequest(http.MethodGet, "/resources/my-value", nil)
26+
rec := httptest.NewRecorder()
27+
handler.ServeHTTP(rec, req)
28+
29+
assert.Equal(t, http.StatusOK, rec.Code, "expected 200 OK, got %d; body: %s", rec.Code, rec.Body.String())
30+
assert.Equal(t, "my-value", server.receivedParam, "path parameter was not correctly extracted")
31+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# yaml-language-server: $schema=../../../../configuration-schema.json
2+
package: gorilla
3+
generate:
4+
gorilla-server: true
5+
models: true
6+
output: server.gen.go
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package gorilla
2+
3+
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml ../spec.yaml

0 commit comments

Comments
 (0)