sanitize param names for http.ServeMux since they must be valid Go identifiers#2279
Merged
Merged
Conversation
e8cbb61 to
cb9c19d
Compare
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>
cb9c19d to
730c94f
Compare
Member
Author
Contributor
Greptile SummaryThis PR fixes a runtime panic caused by OpenAPI path parameters with dashes (e.g. Key changes:
The core fix is correct and well-structured. Two things worth attention:
Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| pkg/codegen/utils.go | Introduces SanitizeGoIdentifier (no keyword-prefixing) extracted from SanitizeGoIdentity, and updates SwaggerUriToStdHttpUri to sanitize wildcard names via a double-regex pattern; logic is correct but an edge-case name collision risk exists. |
| pkg/codegen/operations.go | Adds SanitizedParamName() method to ParameterDefinition, correctly delegating to SanitizeGoIdentifier; straightforward and safe. |
| pkg/codegen/templates/stdhttp/std-http-middleware.tmpl | Updates the three path-param extraction paths (PassThrough, Json, Styled) to call r.PathValue with .SanitizedParamName while keeping .ParamName in error messages; correct. |
| pkg/codegen/utils_test.go | New assertions cover the dashed-param sanitization and the Go-keyword pass-through case for SwaggerUriToStdHttpUri; SanitizeGoIdentifier itself has no direct unit tests. |
| internal/test/server-specific/stdhttp/server_test.go | Integration test exercises the dashed-param scenario end-to-end; correctly asserts HTTP 200 and proper param extraction. |
Last reviewed commit: 730c94f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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