sanitize param names for http.ServeMux by mromaszewicz · Pull Request #2279 · oapi-codegen/oapi-codegen
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 oapi-codegen#2278
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>