-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Anchor trailing-slash stdhttp routes with {$} to match OpenAPI semantics #2460
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
Merged
Merged
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,6 @@ | ||
| # yaml-language-server: $schema=../../../../../configuration-schema.json | ||
| package: serversrouterstrailingslash | ||
| generate: | ||
| std-http-server: true | ||
| models: true | ||
| output: trailing_slash.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,8 @@ | ||
| // Package serversrouterstrailingslash exercises stdhttp route registration | ||
| // for OpenAPI paths with trailing slashes: they must register as | ||
| // {$}-anchored exact-match patterns, both to honor OpenAPI's exact-path | ||
| // semantics and to avoid ServeMux registration panics when subtree | ||
| // patterns from independent paths overlap ambiguously (issue #2065). | ||
| package serversrouterstrailingslash | ||
|
|
||
| //go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml spec.yaml |
79 changes: 79 additions & 0 deletions
79
internal/test/servers/routers/trailing_slash/server_test.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,79 @@ | ||
| package serversrouterstrailingslash | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type server struct { | ||
| lastOp string | ||
| lastID string | ||
| } | ||
|
|
||
| func (s *server) Test1(w http.ResponseWriter, r *http.Request, id string) { | ||
| s.lastOp, s.lastID = "test1", id | ||
| w.WriteHeader(http.StatusOK) | ||
| } | ||
|
|
||
| func (s *server) Test2(w http.ResponseWriter, r *http.Request) { | ||
| s.lastOp = "test2" | ||
| w.WriteHeader(http.StatusOK) | ||
| } | ||
|
|
||
| // TestTrailingSlashRoutes covers issue #2065: the two spec paths overlap | ||
| // ambiguously as ServeMux subtree patterns and used to panic inside | ||
| // HandlerWithOptions; as {$}-anchored patterns they register and route by | ||
| // OpenAPI's exact-path semantics. | ||
| func TestTrailingSlashRoutes(t *testing.T) { | ||
| s := &server{} | ||
|
|
||
| // Registration itself was the panic in the issue. | ||
| var h http.Handler | ||
| require.NotPanics(t, func() { | ||
| h = Handler(s) | ||
| }) | ||
|
|
||
| do := func(path string) (*server, *httptest.ResponseRecorder) { | ||
| s.lastOp, s.lastID = "", "" | ||
| req := httptest.NewRequest(http.MethodGet, path, nil) | ||
| rec := httptest.NewRecorder() | ||
| h.ServeHTTP(rec, req) | ||
| return s, rec | ||
| } | ||
|
|
||
| // Exact paths route to their handlers. | ||
| got, rec := do("/api/test/42/test2/") | ||
| assert.Equal(t, http.StatusOK, rec.Code) | ||
| assert.Equal(t, "test1", got.lastOp) | ||
| assert.Equal(t, "42", got.lastID) | ||
|
|
||
| got, rec = do("/api/test/test3/") | ||
| assert.Equal(t, http.StatusOK, rec.Code) | ||
| assert.Equal(t, "test2", got.lastOp) | ||
|
|
||
| // The ambiguous path from the issue's panic message matches the | ||
| // parameterized route exactly (id="test3"), not both. | ||
| got, rec = do("/api/test/test3/test2/") | ||
| assert.Equal(t, http.StatusOK, rec.Code) | ||
| assert.Equal(t, "test1", got.lastOp) | ||
| assert.Equal(t, "test3", got.lastID) | ||
|
|
||
| // {$}-anchored patterns are exact matches, not subtrees: paths below a | ||
| // trailing-slash route are 404, not swallowed by it. | ||
| _, rec = do("/api/test/test3/deeper") | ||
| assert.Equal(t, http.StatusNotFound, rec.Code) | ||
|
|
||
| // ServeMux still issues its trailing-slash redirect for the anchored | ||
| // pattern: a request without the trailing slash is redirected to the | ||
| // canonical path rather than 404ing. The redirect status differs | ||
| // across Go releases (301 on older toolchains, 307 on newer), so pin | ||
| // the redirect and its target rather than the exact code. | ||
| _, rec = do("/api/test/test3") | ||
| assert.True(t, rec.Code == http.StatusMovedPermanently || rec.Code == http.StatusTemporaryRedirect, | ||
| "expected a trailing-slash redirect, got %d", rec.Code) | ||
| assert.Equal(t, "/api/test/test3/", rec.Header().Get("Location")) | ||
| } | ||
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,35 @@ | ||
| # ========================================================================== | ||
| # Category: servers/routers/trailing_slash | ||
| # Tests: OpenAPI paths with trailing slashes register as exact-match | ||
| # ServeMux patterns ({$}-anchored) instead of subtree patterns. | ||
| # | ||
| # Folds in (provenance): | ||
| # - issues/issue-2065 (subtree pattern conflict panic at registration) | ||
| # ========================================================================== | ||
| openapi: "3.0.3" | ||
| info: | ||
| title: servers/routers/trailing_slash | ||
| version: 1.0.0 | ||
| paths: | ||
| # This pair of paths is the issue-2065 repro: as ServeMux subtree | ||
| # patterns they overlap ambiguously ("/api/test/test3/test2/" matches | ||
| # both) and registration panics; as {$}-anchored exact patterns they | ||
| # coexist. | ||
| /api/test/{id}/test2/: | ||
| get: | ||
| operationId: test1 | ||
| parameters: | ||
| - name: id | ||
| in: path | ||
| required: true | ||
| schema: | ||
| type: string | ||
| responses: | ||
| '200': | ||
| description: ok | ||
| /api/test/test3/: | ||
| get: | ||
| operationId: test2 | ||
| responses: | ||
| '200': | ||
| description: ok |
198 changes: 198 additions & 0 deletions
198
internal/test/servers/routers/trailing_slash/trailing_slash.gen.go
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
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.
Uh oh!
There was an error while loading. Please reload this page.