-
-
Notifications
You must be signed in to change notification settings - Fork 1k
sanitize param names for http.ServeMux #2279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mromaszewicz
wants to merge
1
commit into
oapi-codegen:main
Choose a base branch
from
mromaszewicz:fix/issue-2278
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+284
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| openapi: "3.0.0" | ||
| info: | ||
| version: 1.0.0 | ||
| title: Server-specific tests | ||
| paths: | ||
| # The dashed path parameter name "addressing-identifier" is not a valid Go | ||
| # identifier. This exercises GitHub issue #2278: stdhttp's ServeMux requires | ||
| # wildcard names to be valid Go identifiers. | ||
| /resources/{addressing-identifier}: | ||
| get: | ||
| operationId: getResource | ||
| parameters: | ||
| - name: addressing-identifier | ||
| in: path | ||
| required: true | ||
| schema: | ||
| type: string | ||
| responses: | ||
| "200": | ||
| description: OK | ||
| content: | ||
| text/plain: | ||
| schema: | ||
| type: string |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # yaml-language-server: $schema=../../../../configuration-schema.json | ||
| package: stdhttp | ||
| generate: | ||
| std-http-server: true | ||
| models: true | ||
| output: server.gen.go |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package stdhttp | ||
|
|
||
| //go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml ../spec.yaml |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| //go:build go1.22 | ||
|
|
||
| package stdhttp | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| type testServer struct { | ||
| receivedParam string | ||
| } | ||
|
|
||
| func (s *testServer) GetResource(w http.ResponseWriter, r *http.Request, addressingIdentifier string) { | ||
| s.receivedParam = addressingIdentifier | ||
| _, _ = fmt.Fprint(w, addressingIdentifier) | ||
| } | ||
|
|
||
| func TestDashedPathParam(t *testing.T) { | ||
| server := &testServer{} | ||
| handler := Handler(server) | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/resources/my-value", nil) | ||
| rec := httptest.NewRecorder() | ||
| handler.ServeHTTP(rec, req) | ||
|
|
||
| assert.Equal(t, http.StatusOK, rec.Code, "expected 200 OK, got %d; body: %s", rec.Code, rec.Body.String()) | ||
| assert.Equal(t, "my-value", server.receivedParam, "path parameter was not correctly extracted") | ||
| } |
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sanitization collision: two distinct parameter names can map to the same wildcard
SanitizeGoIdentifierreplaces every illegal rune with_, so two different OpenAPI parameter names can produce the same sanitized output. For example,my-paramandmy_paramboth producemy_param.If a spec defines both in the same path (or if two separate parameters accidentally collide), the route registered with ServeMux would contain a duplicate wildcard segment, e.g.:
Go's
net/httpServeMux panics when it encounters a pattern with two wildcards that have the same name (see Go source). This would cause a runtime panic at server startup rather than a compile-time error.Consider detecting this collision during code generation (e.g. in
SwaggerUriToStdHttpUrior the calling site) and returning an error, or at minimum documenting the limitation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I am ok with the possibility to rename something into a new conflict in this very limited case. This is at such a lower level than our name conflict resolution code, that combining the two would be incredibly messy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we feel that given #2220 / #1574 we'd not get into this case? Should we at least add a test to capture "we don't support this"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, PR #2220 handles a different scenario. It addresses intentional duplicates in the spec (like
/clients/{client-uuid}/roles/{role-name}/composites/clients/{client-uuid}whereclient-uuidgenuinely appears twice), deduplicating them at theParameterDefinitionstruct level.The collision risk here is about accidental duplicates created by sanitization: two originally distinct parameter names (e.g.,
{my-param}and{my_param}) both becoming{my_param}in the route pattern string that ServeMux sees, causing a panic when registering the route.Since #2220's deduplication operates on parameter definitions (before URI transformation), it wouldn't prevent ServeMux from seeing duplicate wildcards in the pattern string after sanitization.
Adding a test that documents "we don't support specs where sanitization creates collisions" would be valuable - it makes the limitation explicit and prevents confusion if someone hits this edge case. Something like:
This also helps future maintainers understand the behavior is intentional, not an overlooked bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#2220 is a different case - it's when path parameters are used more than once in a path. Given the order of evaluation, that fix doesn't help here.